Make From implementations non-failing.

This commit is contained in:
Vytautas Astrauskas 2020-04-27 12:32:57 -07:00
parent c56ef31780
commit df2ca53b69
2 changed files with 11 additions and 7 deletions

View file

@ -1,3 +1,5 @@
use std::convert::TryInto;
use crate::*;
use rustc_target::abi::LayoutOf;
@ -63,7 +65,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
let thread_id = this.read_scalar(thread)?.to_machine_usize(this)?;
this.join_thread(thread_id.into())?;
this.join_thread(thread_id.try_into().expect("thread ID should fit in u32"))?;
Ok(0)
}
@ -72,7 +74,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let this = self.eval_context_mut();
let thread_id = this.read_scalar(thread)?.to_machine_usize(this)?;
this.detach_thread(thread_id.into())?;
this.detach_thread(thread_id.try_into().expect("thread ID should fit in u32"))?;
Ok(0)
}

View file

@ -3,7 +3,7 @@
use std::cell::RefCell;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::num::NonZeroU32;
use std::num::{NonZeroU32, TryFromIntError};
use log::trace;
@ -45,20 +45,22 @@ impl Idx for ThreadId {
fn new(idx: usize) -> Self {
ThreadId(u32::try_from(idx).unwrap())
}
fn index(self) -> usize {
usize::try_from(self.0).unwrap()
}
}
impl From<u64> for ThreadId {
fn from(id: u64) -> Self {
Self(u32::try_from(id).unwrap())
impl TryFrom<u64> for ThreadId {
type Error = TryFromIntError;
fn try_from(id: u64) -> Result<Self, Self::Error> {
u32::try_from(id).map(|id_u32| Self(id_u32))
}
}
impl From<u32> for ThreadId {
fn from(id: u32) -> Self {
Self(u32::try_from(id).unwrap())
Self(id)
}
}