Skip to content

Commit 8cf720b

Browse files
committed
Make Option<ThreadId> no larger than ThreadId, with NonZeroU64
1 parent 0f88167 commit 8cf720b

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/libstd/thread/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ use crate::ffi::{CStr, CString};
163163
use crate::fmt;
164164
use crate::io;
165165
use crate::mem;
166+
use crate::num::NonZeroU64;
166167
use crate::panic;
167168
use crate::panicking;
168169
use crate::str;
@@ -1036,15 +1037,15 @@ pub fn park_timeout(dur: Duration) {
10361037
/// [`Thread`]: ../../std/thread/struct.Thread.html
10371038
#[stable(feature = "thread_id", since = "1.19.0")]
10381039
#[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
1039-
pub struct ThreadId(u64);
1040+
pub struct ThreadId(NonZeroU64);
10401041

10411042
impl ThreadId {
10421043
// Generate a new unique thread ID.
10431044
fn new() -> ThreadId {
10441045
// We never call `GUARD.init()`, so it is UB to attempt to
10451046
// acquire this mutex reentrantly!
10461047
static GUARD: mutex::Mutex = mutex::Mutex::new();
1047-
static mut COUNTER: u64 = 0;
1048+
static mut COUNTER: u64 = 1;
10481049

10491050
unsafe {
10501051
let _guard = GUARD.lock();
@@ -1058,7 +1059,7 @@ impl ThreadId {
10581059
let id = COUNTER;
10591060
COUNTER += 1;
10601061

1061-
ThreadId(id)
1062+
ThreadId(NonZeroU64::new(id).unwrap())
10621063
}
10631064
}
10641065
}

0 commit comments

Comments
 (0)