Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive Hash for ThreadId + better example #41008

Merged
merged 2 commits into from
Apr 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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(Eq, PartialEq, Clone, Copy, Hash, Debug)]
pub struct ThreadId(u64);

impl ThreadId {
Expand Down Expand Up @@ -701,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
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -795,14 +786,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 {
Expand Down