Skip to content

Commit 117c3db

Browse files
committed
Use hashing hacks to get the value of a ThreadId
1 parent 85888c5 commit 117c3db

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

vm/src/stdlib/thread.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,26 @@ pub(crate) mod _thread {
209209
}
210210

211211
fn thread_to_id(t: &thread::Thread) -> u64 {
212+
use std::hash::{Hash, Hasher};
213+
struct U64Hash {
214+
v: Option<u64>,
215+
}
216+
impl Hasher for U64Hash {
217+
fn write(&mut self, _: &[u8]) {
218+
unreachable!()
219+
}
220+
fn write_u64(&mut self, i: u64) {
221+
self.v = Some(i);
222+
}
223+
fn finish(&self) -> u64 {
224+
self.v.expect("should have written a u64")
225+
}
226+
}
212227
// TODO: use id.as_u64() once it's stable, until then, ThreadId is just a wrapper
213-
// around NonZeroU64, so this is safe
214-
unsafe { std::mem::transmute(t.id()) }
228+
// around NonZeroU64, so this should work (?)
229+
let mut h = U64Hash { v: None };
230+
t.id().hash(&mut h);
231+
h.finish()
215232
}
216233

217234
#[pyfunction]

0 commit comments

Comments
 (0)