From d9a7db901e33940cb2ccda6afe21b9916e66d9d2 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Mon, 23 Dec 2019 12:54:54 -0500 Subject: 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). --- src/libstd/thread/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index b1741891138..0dc43c7e651 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -1072,6 +1072,19 @@ impl ThreadId { ThreadId(NonZeroU64::new(id).unwrap()) } } + + /// This returns a numeric identifier for the thread identified by this + /// `ThreadId`. + /// + /// As noted in the documentation for the type itself, it is essentially an + /// opaque ID, but is guaranteed to be unique for each thread. The returned + /// value is entirely opaque -- only equality testing is stable. Note that + /// it is not guaranteed which values new threads will return, and this may + /// change across Rust versions. + #[unstable(feature = "thread_id_value", issue = "67939")] + pub fn as_u64(&self) -> u64 { + self.0.get() + } } //////////////////////////////////////////////////////////////////////////////// -- cgit 1.4.1-3-g733a5 From 0113cacda2d08c135603bcb83da393252317e6a8 Mon Sep 17 00:00:00 2001 From: Strømberg Date: Mon, 6 Jan 2020 17:25:17 +0100 Subject: Missing module std in example. --- src/libstd/net/tcp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/net/tcp.rs b/src/libstd/net/tcp.rs index c33f98bdd83..dd25d66658c 100644 --- a/src/libstd/net/tcp.rs +++ b/src/libstd/net/tcp.rs @@ -70,7 +70,7 @@ pub struct TcpStream(net_imp::TcpStream); /// // ... /// } /// -/// fn main() -> io::Result<()> { +/// fn main() -> std::io::Result<()> { /// let listener = TcpListener::bind("127.0.0.1:80")?; /// /// // accept connections and process them serially -- cgit 1.4.1-3-g733a5 From a852941829dc835e9fb0f59b2c1e19aa1439211d Mon Sep 17 00:00:00 2001 From: Strømberg Date: Mon, 6 Jan 2020 17:38:41 +0100 Subject: Removed module usage. --- src/libstd/net/tcp.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/net/tcp.rs b/src/libstd/net/tcp.rs index dd25d66658c..5023d692408 100644 --- a/src/libstd/net/tcp.rs +++ b/src/libstd/net/tcp.rs @@ -63,7 +63,6 @@ pub struct TcpStream(net_imp::TcpStream); /// # Examples /// /// ```no_run -/// # use std::io; /// use std::net::{TcpListener, TcpStream}; /// /// fn handle_client(stream: TcpStream) { -- cgit 1.4.1-3-g733a5