@@ -203,7 +203,7 @@ use std::{fmt, mem};
203203#[ cfg_attr( not( tokio_unstable) , allow( unreachable_pub) ) ]
204204// TODO(eliza): there's almost certainly no reason not to make this `Copy` as well...
205205#[ derive( Clone , Debug , Hash , Eq , PartialEq ) ]
206- pub struct Id ( std :: num :: NonZeroUsize ) ;
206+ pub struct Id ( usize ) ;
207207
208208/// An owned handle to the task, tracked by ref count.
209209#[ repr( transparent) ]
@@ -278,7 +278,8 @@ cfg_rt! {
278278 T : Future + ' static ,
279279 T :: Output : ' static ,
280280 {
281- let raw = RawTask :: new:: <T , S >( task, scheduler) ;
281+ let id = Id :: next( ) ;
282+ let raw = RawTask :: new:: <T , S >( task, scheduler, id. clone( ) ) ;
282283 let task = Task {
283284 raw,
284285 _p: PhantomData ,
@@ -287,7 +288,7 @@ cfg_rt! {
287288 raw,
288289 _p: PhantomData ,
289290 } ) ;
290- let join = JoinHandle :: new( raw) ;
291+ let join = JoinHandle :: new( raw, id ) ;
291292
292293 ( task, notified, join)
293294 }
@@ -477,25 +478,11 @@ impl fmt::Display for Id {
477478 self . 0 . fmt ( f)
478479 }
479480}
480- impl Id {
481- #[ inline]
482- fn from_raw ( ptr : NonNull < Header > ) -> Self {
483- use std:: num:: NonZeroUsize ;
484- let addr = ptr. as_ptr ( ) as usize ;
485-
486- #[ cfg( debug_assertions) ]
487- let inner = NonZeroUsize :: new ( addr)
488- . expect ( "a `NonNull` pointer will never be 0 when cast to `usize`" ) ;
489-
490- #[ cfg( not( debug_assertions) ) ]
491- let inner = unsafe {
492- // Safety: `addr` was cast from a `NonNull` pointer, which must
493- // never be null (0). Since the pointer is not null, the integer
494- // will never be zero, so this is safe as long as the `NonNull` was
495- // constructed safely.
496- NonZeroUsize :: new_unchecked ( addr)
497- } ;
498481
499- Self ( inner)
482+ impl Id {
483+ fn next ( ) -> Self {
484+ use std:: sync:: atomic:: { AtomicUsize , Ordering :: Relaxed } ;
485+ static NEXT_ID : AtomicUsize = AtomicUsize :: new ( 1 ) ;
486+ Self ( NEXT_ID . fetch_add ( 1 , Relaxed ) )
500487 }
501488}
0 commit comments