Skip to content

Commit df2ca53

Browse files
author
Vytautas Astrauskas
committed
Make From implementations non-failing.
1 parent c56ef31 commit df2ca53

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/shims/thread.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::TryInto;
2+
13
use crate::*;
24
use rustc_target::abi::LayoutOf;
35

@@ -63,7 +65,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6365
}
6466

6567
let thread_id = this.read_scalar(thread)?.to_machine_usize(this)?;
66-
this.join_thread(thread_id.into())?;
68+
this.join_thread(thread_id.try_into().expect("thread ID should fit in u32"))?;
6769

6870
Ok(0)
6971
}
@@ -72,7 +74,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7274
let this = self.eval_context_mut();
7375

7476
let thread_id = this.read_scalar(thread)?.to_machine_usize(this)?;
75-
this.detach_thread(thread_id.into())?;
77+
this.detach_thread(thread_id.try_into().expect("thread ID should fit in u32"))?;
7678

7779
Ok(0)
7880
}

src/thread.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::cell::RefCell;
44
use std::convert::TryFrom;
55
use std::convert::TryInto;
6-
use std::num::NonZeroU32;
6+
use std::num::{NonZeroU32, TryFromIntError};
77

88
use log::trace;
99

@@ -45,20 +45,22 @@ impl Idx for ThreadId {
4545
fn new(idx: usize) -> Self {
4646
ThreadId(u32::try_from(idx).unwrap())
4747
}
48+
4849
fn index(self) -> usize {
4950
usize::try_from(self.0).unwrap()
5051
}
5152
}
5253

53-
impl From<u64> for ThreadId {
54-
fn from(id: u64) -> Self {
55-
Self(u32::try_from(id).unwrap())
54+
impl TryFrom<u64> for ThreadId {
55+
type Error = TryFromIntError;
56+
fn try_from(id: u64) -> Result<Self, Self::Error> {
57+
u32::try_from(id).map(|id_u32| Self(id_u32))
5658
}
5759
}
5860

5961
impl From<u32> for ThreadId {
6062
fn from(id: u32) -> Self {
61-
Self(u32::try_from(id).unwrap())
63+
Self(id)
6264
}
6365
}
6466

0 commit comments

Comments
 (0)