From 282029526646fc93cd8bc098191c4e110a4c4938 Mon Sep 17 00:00:00 2001 From: "Stephen M. Coakley" Date: Sat, 1 Apr 2017 22:17:59 -0500 Subject: [PATCH 1/2] Derive Hash for ThreadId + better example --- src/libstd/thread/mod.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index edf928d61063e..21f9757ad116b 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -652,8 +652,8 @@ pub fn park_timeout(dur: Duration) { /// A unique identifier for a running thread. /// /// A `ThreadId` is an opaque object that has a unique value for each thread -/// that creates one. `ThreadId`s do not correspond to a thread's system- -/// designated identifier. +/// that creates one. `ThreadId`s are not guaranteed to correspond to a thread's +/// system-designated identifier. /// /// # Examples /// @@ -662,17 +662,15 @@ pub fn park_timeout(dur: Duration) { /// /// use std::thread; /// -/// let handler = thread::Builder::new() -/// .spawn(|| { -/// let thread = thread::current(); -/// let thread_id = thread.id(); -/// }) -/// .unwrap(); +/// let other_thread = thread::spawn(|| { +/// thread::current().id() +/// }); /// -/// handler.join().unwrap(); +/// let other_thread_id = other_thread.join().unwrap(); +/// assert!(thread::current().id() != other_thread_id); /// ``` #[unstable(feature = "thread_id", issue = "21507")] -#[derive(Eq, PartialEq, Copy, Clone)] +#[derive(Clone, Copy, Eq, PartialEq, Hash)] pub struct ThreadId(u64); impl ThreadId { @@ -795,14 +793,12 @@ impl Thread { /// /// use std::thread; /// - /// let handler = thread::Builder::new() - /// .spawn(|| { - /// let thread = thread::current(); - /// println!("thread id: {:?}", thread.id()); - /// }) - /// .unwrap(); + /// let other_thread = thread::spawn(|| { + /// thread::current().id() + /// }); /// - /// handler.join().unwrap(); + /// let other_thread_id = other_thread.join().unwrap(); + /// assert!(thread::current().id() != other_thread_id); /// ``` #[unstable(feature = "thread_id", issue = "21507")] pub fn id(&self) -> ThreadId { From cd14a323f42cf57695e713a4f4fd00fddc10efd5 Mon Sep 17 00:00:00 2001 From: "Stephen M. Coakley" Date: Tue, 4 Apr 2017 10:44:57 -0500 Subject: [PATCH 2/2] Use derived Debug for ThreadId --- src/libstd/thread/mod.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 21f9757ad116b..9116f54173650 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -670,7 +670,7 @@ pub fn park_timeout(dur: Duration) { /// assert!(thread::current().id() != other_thread_id); /// ``` #[unstable(feature = "thread_id", issue = "21507")] -#[derive(Clone, Copy, Eq, PartialEq, Hash)] +#[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)] pub struct ThreadId(u64); impl ThreadId { @@ -699,13 +699,6 @@ impl ThreadId { } } -#[unstable(feature = "thread_id", issue = "21507")] -impl fmt::Debug for ThreadId { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.pad("ThreadId { .. }") - } -} - //////////////////////////////////////////////////////////////////////////////// // Thread ////////////////////////////////////////////////////////////////////////////////