@@ -470,9 +470,11 @@ impl Builder {
470
470
471
471
let stack_size = stack_size. unwrap_or_else ( thread:: min_stack) ;
472
472
473
- let my_thread = Thread :: new ( name. map ( |name| {
474
- CString :: new ( name) . expect ( "thread name may not contain interior null bytes" )
475
- } ) ) ;
473
+ let my_thread = name. map_or_else ( Thread :: new_unnamed, |name| unsafe {
474
+ Thread :: new (
475
+ CString :: new ( name) . expect ( "thread name may not contain interior null bytes" ) ,
476
+ )
477
+ } ) ;
476
478
let their_thread = my_thread. clone ( ) ;
477
479
478
480
let my_packet: Arc < Packet < ' scope , T > > = Arc :: new ( Packet {
@@ -694,7 +696,7 @@ pub(crate) fn set_current(thread: Thread) {
694
696
/// In contrast to the public `current` function, this will not panic if called
695
697
/// from inside a TLS destructor.
696
698
pub ( crate ) fn try_current ( ) -> Option < Thread > {
697
- CURRENT . try_with ( |current| current. get_or_init ( || Thread :: new ( None ) ) . clone ( ) ) . ok ( )
699
+ CURRENT . try_with ( |current| current. get_or_init ( || Thread :: new_unnamed ( ) ) . clone ( ) ) . ok ( )
698
700
}
699
701
700
702
/// Gets a handle to the thread that invokes it.
@@ -1290,21 +1292,26 @@ pub struct Thread {
1290
1292
}
1291
1293
1292
1294
impl Thread {
1293
- // Used only internally to construct a thread object without spawning
1294
- pub ( crate ) fn new ( name : Option < CString > ) -> Thread {
1295
- if let Some ( name) = name {
1296
- Self :: new_inner ( ThreadName :: Other ( name) )
1297
- } else {
1298
- Self :: new_inner ( ThreadName :: Unnamed )
1299
- }
1295
+ /// Used only internally to construct a thread object without spawning.
1296
+ ///
1297
+ /// # Safety
1298
+ /// `name` must be valid UTF-8.
1299
+ pub ( crate ) unsafe fn new ( name : CString ) -> Thread {
1300
+ unsafe { Self :: new_inner ( ThreadName :: Other ( name) ) }
1301
+ }
1302
+
1303
+ pub ( crate ) fn new_unnamed ( ) -> Thread {
1304
+ unsafe { Self :: new_inner ( ThreadName :: Unnamed ) }
1300
1305
}
1301
1306
1302
1307
// Used in runtime to construct main thread
1303
1308
pub ( crate ) fn new_main ( ) -> Thread {
1304
- Self :: new_inner ( ThreadName :: Main )
1309
+ unsafe { Self :: new_inner ( ThreadName :: Main ) }
1305
1310
}
1306
1311
1307
- fn new_inner ( name : ThreadName ) -> Thread {
1312
+ /// # Safety
1313
+ /// If `name` is `ThreadName::Other(_)`, the contained string must be valid UTF-8.
1314
+ unsafe fn new_inner ( name : ThreadName ) -> Thread {
1308
1315
// We have to use `unsafe` here to construct the `Parker` in-place,
1309
1316
// which is required for the UNIX implementation.
1310
1317
//
0 commit comments