Skip to content

Commit 256721e

Browse files
committed
Auto merge of #92553 - m-ou-se:thread-join-simplify, r=Mark-Simulacrum
Simpilfy thread::JoinInner. `JoinInner`'s `native` field was an `Option`, but that's unnecessary. Also, thanks to `Arc::get_mut`, there's no unsafety needed in `JoinInner::join()`.
2 parents e916815 + a45b3ac commit 256721e

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

library/std/src/thread/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,12 @@ impl Builder {
498498
// exist after the thread has terminated, which is signaled by `Thread::join`
499499
// returning.
500500
native: unsafe {
501-
Some(imp::Thread::new(
501+
imp::Thread::new(
502502
stack_size,
503503
mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>(
504504
Box::new(main),
505505
),
506-
)?)
506+
)?
507507
},
508508
thread: my_thread,
509509
packet: Packet(my_packet),
@@ -1261,15 +1261,15 @@ unsafe impl<T: Sync> Sync for Packet<T> {}
12611261

12621262
/// Inner representation for JoinHandle
12631263
struct JoinInner<T> {
1264-
native: Option<imp::Thread>,
1264+
native: imp::Thread,
12651265
thread: Thread,
12661266
packet: Packet<T>,
12671267
}
12681268

12691269
impl<T> JoinInner<T> {
1270-
fn join(&mut self) -> Result<T> {
1271-
self.native.take().unwrap().join();
1272-
unsafe { (*self.packet.0.get()).take().unwrap() }
1270+
fn join(mut self) -> Result<T> {
1271+
self.native.join();
1272+
Arc::get_mut(&mut self.packet.0).unwrap().get_mut().take().unwrap()
12731273
}
12741274
}
12751275

@@ -1400,7 +1400,7 @@ impl<T> JoinHandle<T> {
14001400
/// join_handle.join().expect("Couldn't join on the associated thread");
14011401
/// ```
14021402
#[stable(feature = "rust1", since = "1.0.0")]
1403-
pub fn join(mut self) -> Result<T> {
1403+
pub fn join(self) -> Result<T> {
14041404
self.0.join()
14051405
}
14061406

@@ -1416,13 +1416,13 @@ impl<T> JoinHandle<T> {
14161416

14171417
impl<T> AsInner<imp::Thread> for JoinHandle<T> {
14181418
fn as_inner(&self) -> &imp::Thread {
1419-
self.0.native.as_ref().unwrap()
1419+
&self.0.native
14201420
}
14211421
}
14221422

14231423
impl<T> IntoInner<imp::Thread> for JoinHandle<T> {
14241424
fn into_inner(self) -> imp::Thread {
1425-
self.0.native.unwrap()
1425+
self.0.native
14261426
}
14271427
}
14281428

0 commit comments

Comments
 (0)