Skip to content

Commit 839e74a

Browse files
authored
Rollup merge of rust-lang#67566 - Mark-Simulacrum:thread-id-u64, r=alexcrichton
Add an unstable conversion from thread ID to u64 We see multiple cases inside rustc and ecosystem code where ThreadId is transmuted to u64, exploiting the underlying detail. This is suboptimal (can break unexpectedly if we change things in std). It is unlikely that ThreadId will ever need to be larger than u64 -- creating even 2^32 threads over the course of a program is quite hard, 2^64 is even harder. As such, we do not choose to return a larger sized type (e.g. u128). If we choose to shrink ThreadId in the future, or otherwise change its internals, it is likely that a mapping to u64 will still be applicable (though may become more complex). I will file a tracking issue as soon as this is loosely approved.
2 parents ebbb2bf + d9a7db9 commit 839e74a

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/libstd/thread/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,19 @@ impl ThreadId {
10721072
ThreadId(NonZeroU64::new(id).unwrap())
10731073
}
10741074
}
1075+
1076+
/// This returns a numeric identifier for the thread identified by this
1077+
/// `ThreadId`.
1078+
///
1079+
/// As noted in the documentation for the type itself, it is essentially an
1080+
/// opaque ID, but is guaranteed to be unique for each thread. The returned
1081+
/// value is entirely opaque -- only equality testing is stable. Note that
1082+
/// it is not guaranteed which values new threads will return, and this may
1083+
/// change across Rust versions.
1084+
#[unstable(feature = "thread_id_value", issue = "67939")]
1085+
pub fn as_u64(&self) -> u64 {
1086+
self.0.get()
1087+
}
10751088
}
10761089

10771090
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)