#![ stable ( feature = "rust1" , since = "1.0.0" )] use core :: any :: Any ; use core :: array :: LengthAtMost32 ; use core :: borrow ; use core :: cmp :: Ordering ; use core :: convert ::{ From , TryFrom }; use core :: fmt ; use core :: hash ::{ Hash , Hasher }; use core :: intrinsics :: abort ; use core :: iter ; use core :: marker ::{ PhantomData , Unpin , Unsize }; use core :: mem ::{ self , align_of_val , size_of_val }; use core :: ops ::{ CoerceUnsized , Deref , DispatchFromDyn , Receiver }; use core :: pin :: Pin ; use core :: ptr ::{ self , NonNull }; use core :: slice :: from_raw_parts_mut ; use core :: sync :: atomic ; use core :: sync :: atomic :: Ordering ::{ Acquire , Relaxed , Release , SeqCst }; use crate :: alloc ::{ box_free , handle_alloc_error , AllocInit , AllocRef , Global , Layout }; use crate :: borrow ::{ Cow , ToOwned }; use crate :: boxed :: Box ; use crate :: rc :: is_dangling ; use crate :: string :: String ; use crate :: vec :: Vec ; #[ cfg ( test )] mod tests ; const MAX_REFCOUNT : usize = ( isize :: MAX ) as usize ; #[ cfg ( not ( sanitize = "thread" ))] macro_rules ! acquire { ( $ x : expr ) = > { atomic :: fence ( Acquire ) }; } #[ cfg ( sanitize = "thread" )] macro_rules ! acquire { ( $ x : expr ) = > { $ x . load ( Acquire ) }; } #[ cfg_attr ( not ( test ), rustc_diagnostic_item = "Arc" )] #[ stable ( feature = "rust1" , since = "1.0.0" )] pub struct Arc < T : ? Sized > { ptr : NonNull < ArcInner < T > > , phantom : PhantomData < ArcInner < T > > , } #[ stable ( feature = "rust1" , since = "1.0.0" )] unsafe impl < T : ? Sized + Sync + Send > Send for Arc < T > {} #[ stable ( feature = "rust1" , since = "1.0.0" )] unsafe impl < T : ? Sized + Sync + Send > Sync for Arc < T > {} #[ unstable ( feature = "coerce_unsized" , issue = "27732" )] impl < T : ? Sized + Unsize < U > , U : ? Sized > CoerceUnsized < Arc < U > > for Arc < T > {} #[ unstable ( feature = "dispatch_from_dyn" , issue = "none" )] impl < T : ? Sized + Unsize < U > , U : ? Sized > DispatchFromDyn < Arc < U > > for Arc < T > {} impl < T : ? Sized > Arc < T > { fn from_inner ( ptr : NonNull < ArcInner < T > > ) - > Self { Self { ptr , phantom : PhantomData } } unsafe fn from_ptr ( ptr : * mut ArcInner < T > ) - > Self { unsafe { Self :: from_inner ( NonNull :: new_unchecked ( ptr )) } } } #[ stable ( feature = "arc_weak" , since = "1.4.0" )] pub struct Weak < T : ? Sized > { ptr : NonNull < ArcInner < T > > , } #[ stable ( feature = "arc_weak" , since = "1.4.0" )] unsafe impl < T : ? Sized + Sync + Send > Send for Weak < T > {} #[ stable ( feature = "arc_weak" , since = "1.4.0" )] unsafe impl < T : ? Sized + Sync + Send > Sync for Weak < T > {} #[ unstable ( feature = "coerce_unsized" , issue = "27732" )] impl < T : ? Sized + Unsize < U > , U : ? Sized > CoerceUnsized < Weak < U > > for Weak < T > {} #[ unstable ( feature = "dispatch_from_dyn" , issue = "none" )] impl < T : ? Sized + Unsize < U > , U : ? Sized > DispatchFromDyn < Weak < U > > for Weak < T > {} #[ stable ( feature = "arc_weak" , since = "1.4.0" )] impl < T : ? Sized + fmt :: Debug > fmt :: Debug for Weak < T > { fn fmt ( & self , f : & mut fmt :: Formatter < '_ > ) - > fmt :: Result { write ! ( f , "(Weak)" ) } } #[ repr ( C )] struct ArcInner < T : ? Sized > { strong : atomic :: AtomicUsize , weak : atomic :: AtomicUsize , data : T , } unsafe impl < T : ? Sized + Sync + Send > Send for ArcInner < T > {} unsafe impl < T : ? Sized + Sync + Send > Sync for ArcInner < T > {} impl < T > Arc < T > { #[ inline ] #[ stable ( feature = "rust1" , since = "1.0.0" )] pub fn new ( data : T ) - > Arc < T > { let x : Box < _ > = box ArcInner { strong : atomic :: AtomicUsize :: new ( 1 ), weak : atomic :: AtomicUsize :: new ( 1 ), data , }; Self :: from_inner ( Box :: leak ( x ). into ()) } #[ unstable ( feature = "new_uninit" , issue = "63291" )] pub fn new_uninit () - > Arc < mem :: MaybeUninit < T > > { unsafe { Arc :: from_ptr ( Arc :: allocate_for_layout ( Layout :: new :: < T > (), | mem | { mem as * mut ArcInner < mem :: MaybeUninit < T > > })) } } #[ unstable ( feature = "new_uninit" , issue = "63291" )] pub fn new_zeroed () - > Arc < mem :: MaybeUninit < T > > { unsafe { let mut uninit = Self :: new_uninit (); ptr :: write_bytes :: < T > ( Arc :: get_mut_unchecked ( & mut uninit ). as_mut_ptr (), 0 , 1 ); uninit } } #[ stable ( feature = "pin" , since = "1.33.0" )] pub fn pin ( data : T ) - > Pin < Arc < T > > { unsafe { Pin :: new_unchecked ( Arc :: new ( data )) } } #[ inline ] #[ stable ( feature = "arc_unique" , since = "1.4.0" )] pub fn try_unwrap ( this : Self ) - > Result < T , Self > { if this . inner (). strong . compare_exchange ( 1 , 0 , Relaxed , Relaxed ). is_err () { return Err ( this ); } acquire ! ( this . inner (). strong ); unsafe { let elem = ptr :: read ( & this . ptr . as_ref (). data ); let _weak = Weak { ptr : this . ptr }; mem :: forget ( this ); Ok ( elem ) } } } impl < T > Arc < [ T ] > { #[ unstable ( feature = "new_uninit" , issue = "63291" )] pub fn new_uninit_slice ( len : usize ) - > Arc < [ mem :: MaybeUninit < T > ] > { unsafe { Arc :: from_ptr ( Arc :: allocate_for_slice ( len )) } } } impl < T > Arc < mem :: MaybeUninit < T > > { #[ unstable ( feature = "new_uninit" , issue = "63291" )] #[ inline ] pub unsafe fn assume_init ( self ) - > Arc < T > { Arc :: from_inner ( mem :: ManuallyDrop :: new ( self ). ptr . cast ()) } } impl < T > Arc < [ mem :: MaybeUninit < T > ] > { #[ unstable ( feature = "new_uninit" , issue = "63291" )] #[ inline ] pub unsafe fn assume_init ( self ) - > Arc < [ T ] > { unsafe { Arc :: from_ptr ( mem :: ManuallyDrop :: new ( self ). ptr . as_ptr () as _ ) } } } impl < T : ? Sized > Arc < T > { #[ stable ( feature = "rc_raw" , since = "1.17.0" )] pub fn into_raw ( this : Self ) - > * const T { let ptr = Self :: as_ptr ( & this ); mem :: forget ( this ); ptr } #[ stable ( feature = "rc_as_ptr" , since = "1.45.0" )] pub fn as_ptr ( this : & Self ) - > * const T { let ptr : * mut ArcInner < T > = NonNull :: as_ptr ( this . ptr ); unsafe { & raw const ( * ptr ). data } } #[ stable ( feature = "rc_raw" , since = "1.17.0" )] pub unsafe fn from_raw ( ptr : * const T ) - > Self { unsafe { let offset = data_offset ( ptr ); let fake_ptr = ptr as * mut ArcInner < T > ; let arc_ptr = set_data_ptr ( fake_ptr , ( ptr as * mut u8 ). offset ( - offset )); Self :: from_ptr ( arc_ptr ) } } #[ unstable ( feature = "rc_into_raw_non_null" , issue = "47336" )] #[ rustc_deprecated ( since = "1.44.0" , reason = "use `Arc::into_raw` instead" )] #[ inline ] pub fn into_raw_non_null ( this : Self ) - > NonNull < T > { unsafe { NonNull :: new_unchecked ( Arc :: into_raw ( this ) as * mut _ ) } } #[ stable ( feature = "arc_weak" , since = "1.4.0" )] pub fn downgrade ( this : & Self ) - > Weak < T > { let mut cur = this . inner (). weak . load ( Relaxed ); loop { if cur = = usize :: MAX { cur = this . inner (). weak . load ( Relaxed ); continue ; } match this . inner (). weak . compare_exchange_weak ( cur , cur + 1 , Acquire , Relaxed ) { Ok ( _ ) = > { debug_assert ! ( ! is_dangling ( this . ptr )); return Weak { ptr : this . ptr }; } Err ( old ) = > cur = old , } } } #[ inline ] #[ stable ( feature = "arc_counts" , since = "1.15.0" )] pub fn weak_count ( this : & Self ) - > usize { let cnt = this . inner (). weak . load ( SeqCst ); if cnt = = usize :: MAX { 0 } else { cnt - 1 } } #[ inline ] #[ stable ( feature = "arc_counts" , since = "1.15.0" )] pub fn strong_count ( this : & Self ) - > usize { this . inner (). strong . load ( SeqCst ) } #[ inline ] #[ unstable ( feature = "arc_mutate_strong_count" , issue = "71983" )] pub unsafe fn incr_strong_count ( ptr : * const T ) { let arc = unsafe { mem :: ManuallyDrop :: new ( Arc :: < T > :: from_raw ( ptr )) }; let _arc_clone : mem :: ManuallyDrop < _ > = arc . clone (); } #[ inline ] #[ unstable ( feature = "arc_mutate_strong_count" , issue = "71983" )] pub unsafe fn decr_strong_count ( ptr : * const T ) { unsafe { mem :: drop ( Arc :: from_raw ( ptr )) }; } #[ inline ] fn inner ( & self ) - > & ArcInner < T > { unsafe { self . ptr . as_ref () } } #[ inline ( never )] unsafe fn drop_slow ( & mut self ) { unsafe { ptr :: drop_in_place ( Self :: get_mut_unchecked ( self )) }; drop ( Weak { ptr : self . ptr }); } #[ inline ] #[ stable ( feature = "ptr_eq" , since = "1.17.0" )] pub fn ptr_eq ( this : & Self , other : & Self ) - > bool { this . ptr . as_ptr () = = other . ptr . as_ptr () } } impl < T : ? Sized > Arc < T > { unsafe fn allocate_for_layout ( value_layout : Layout , mem_to_arcinner : impl FnOnce ( * mut u8 ) - > * mut ArcInner < T > , ) - > * mut ArcInner < T > { let layout = Layout :: new :: < ArcInner < () > > (). extend ( value_layout ). unwrap (). 0 . pad_to_align (); let mem = Global . alloc ( layout , AllocInit :: Uninitialized ) . unwrap_or_else ( | _ | handle_alloc_error ( layout )); let inner = mem_to_arcinner ( mem . ptr . as_ptr ()); debug_assert_eq ! ( unsafe { Layout :: for_value ( & * inner ) }, layout ); unsafe { ptr :: write ( & mut ( * inner ). strong , atomic :: AtomicUsize :: new ( 1 )); ptr :: write ( & mut ( * inner ). weak , atomic :: AtomicUsize :: new ( 1 )); } inner } unsafe fn allocate_for_ptr ( ptr : * const T ) - > * mut ArcInner < T > { unsafe { Self :: allocate_for_layout ( Layout :: for_value ( & * ptr ), | mem | { set_data_ptr ( ptr as * mut T , mem ) as * mut ArcInner < T > }) } } fn from_box ( v : Box < T > ) - > Arc < T > { unsafe { let box_unique = Box :: into_unique ( v ); let bptr = box_unique . as_ptr (); let value_size = size_of_val ( & * bptr ); let ptr = Self :: allocate_for_ptr ( bptr ); ptr :: copy_nonoverlapping ( bptr as * const T as * const u8 , & mut ( * ptr ). data as * mut _ as * mut u8 , value_size , ); box_free ( box_unique ); Self :: from_ptr ( ptr ) } } } impl < T > Arc < [ T ] > { unsafe fn allocate_for_slice ( len : usize ) - > * mut ArcInner < [ T ] > { unsafe { Self :: allocate_for_layout ( Layout :: array :: < T > ( len ). unwrap (), | mem | { ptr :: slice_from_raw_parts_mut ( mem as * mut T , len ) as * mut ArcInner < [ T ] > }) } } } unsafe fn set_data_ptr < T : ? Sized , U > ( mut ptr : * mut T , data : * mut U ) - > * mut T { unsafe { ptr :: write ( & mut ptr as * mut _ as * mut * mut u8 , data as * mut u8 ); } ptr } impl < T > Arc < [ T ] > { unsafe fn copy_from_slice ( v : & [ T ]) - > Arc < [ T ] > { unsafe { let ptr = Self :: allocate_for_slice ( v . len ()); ptr :: copy_nonoverlapping ( v . as_ptr (), & mut ( * ptr ). data as * mut [ T ] as * mut T , v . len ()); Self :: from_ptr ( ptr ) } } unsafe fn from_iter_exact ( iter : impl iter :: Iterator < Item = T > , len : usize ) - > Arc < [ T ] > { struct Guard < T > { mem : NonNull < u8 > , elems : * mut T , layout : Layout , n_elems : usize , } impl < T > Drop for Guard < T > { fn drop ( & mut self ) { unsafe { let slice = from_raw_parts_mut ( self . elems , self . n_elems ); ptr :: drop_in_place ( slice ); Global . dealloc ( self . mem . cast (), self . layout ); } } } unsafe { let ptr = Self :: allocate_for_slice ( len ); let mem = ptr as * mut _ as * mut u8 ; let layout = Layout :: for_value ( & * ptr ); let elems = & mut ( * ptr ). data as * mut [ T ] as * mut T ; let mut guard = Guard { mem : NonNull :: new_unchecked ( mem ), elems , layout , n_elems : 0 }; for ( i , item ) in iter . enumerate () { ptr :: write ( elems . add ( i ), item ); guard . n_elems + = 1 ; } mem :: forget ( guard ); Self :: from_ptr ( ptr ) } } } trait ArcFromSlice < T > { fn from_slice ( slice : & [ T ]) - > Self ; } impl < T : Clone > ArcFromSlice < T > for Arc < [ T ] > { #[ inline ] default fn from_slice ( v : & [ T ]) - > Self { unsafe { Self :: from_iter_exact ( v . iter (). cloned (), v . len ()) } } } impl < T : Copy > ArcFromSlice < T > for Arc < [ T ] > { #[ inline ] fn from_slice ( v : & [ T ]) - > Self { unsafe { Arc :: copy_from_slice ( v ) } } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized > Clone for Arc < T > { #[ inline ] fn clone ( & self ) - > Arc < T > { let old_size = self . inner (). strong . fetch_add ( 1 , Relaxed ); if old_size > MAX_REFCOUNT { abort (); } Self :: from_inner ( self . ptr ) } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized > Deref for Arc < T > { type Target = T ; #[ inline ] fn deref ( & self ) - > & T { & self . inner (). data } } #[ unstable ( feature = "receiver_trait" , issue = "none" )] impl < T : ? Sized > Receiver for Arc < T > {} impl < T : Clone > Arc < T > { #[ inline ] #[ stable ( feature = "arc_unique" , since = "1.4.0" )] pub fn make_mut ( this : & mut Self ) - > & mut T { if this . inner (). strong . compare_exchange ( 1 , 0 , Acquire , Relaxed ). is_err () { * this = Arc :: new (( * * this ). clone ()); } else if this . inner (). weak . load ( Relaxed ) ! = 1 { let weak = Weak { ptr : this . ptr }; unsafe { let mut swap = Arc :: new ( ptr :: read ( & weak . ptr . as_ref (). data )); mem :: swap ( this , & mut swap ); mem :: forget ( swap ); } } else { this . inner (). strong . store ( 1 , Release ); } unsafe { Self :: get_mut_unchecked ( this ) } } } impl < T : ? Sized > Arc < T > { #[ inline ] #[ stable ( feature = "arc_unique" , since = "1.4.0" )] pub fn get_mut ( this : & mut Self ) - > Option < & mut T > { if this . is_unique () { unsafe { Some ( Arc :: get_mut_unchecked ( this )) } } else { None } } #[ inline ] #[ unstable ( feature = "get_mut_unchecked" , issue = "63292" )] pub unsafe fn get_mut_unchecked ( this : & mut Self ) - > & mut T { unsafe { & mut ( * this . ptr . as_ptr ()). data } } fn is_unique ( & mut self ) - > bool { if self . inner (). weak . compare_exchange ( 1 , usize :: MAX , Acquire , Relaxed ). is_ok () { let unique = self . inner (). strong . load ( Acquire ) = = 1 ; self . inner (). weak . store ( 1 , Release ); unique } else { false } } } #[ stable ( feature = "rust1" , since = "1.0.0" )] unsafe impl < #[ may_dangle ] T : ? Sized > Drop for Arc < T > { #[ inline ] fn drop ( & mut self ) { if self . inner (). strong . fetch_sub ( 1 , Release ) ! = 1 { return ; } acquire ! ( self . inner (). strong ); unsafe { self . drop_slow (); } } } impl Arc < dyn Any + Send + Sync > { #[ inline ] #[ stable ( feature = "rc_downcast" , since = "1.29.0" )] pub fn downcast < T > ( self ) - > Result < Arc < T > , Self > where T : Any + Send + Sync + 'static , { if ( * self ). is :: < T > () { let ptr = self . ptr . cast :: < ArcInner < T > > (); mem :: forget ( self ); Ok ( Arc :: from_inner ( ptr )) } else { Err ( self ) } } } impl < T > Weak < T > { #[ stable ( feature = "downgraded_weak" , since = "1.10.0" )] pub fn new () - > Weak < T > { Weak { ptr : NonNull :: new ( usize :: MAX as * mut ArcInner < T > ). expect ( "MAX is not 0" ) } } #[ stable ( feature = "weak_into_raw" , since = "1.45.0" )] pub fn as_ptr ( & self ) - > * const T { let ptr : * mut ArcInner < T > = NonNull :: as_ptr ( self . ptr ); unsafe { let offset = data_offset ( ptr as * mut T ); set_data_ptr ( ptr as * mut T , ( ptr as * mut u8 ). wrapping_offset ( offset )) } } #[ stable ( feature = "weak_into_raw" , since = "1.45.0" )] pub fn into_raw ( self ) - > * const T { let result = self . as_ptr (); mem :: forget ( self ); result } #[ stable ( feature = "weak_into_raw" , since = "1.45.0" )] pub unsafe fn from_raw ( ptr : * const T ) - > Self { if ptr . is_null () { Self :: new () } else { unsafe { let offset = data_offset ( ptr ); let fake_ptr = ptr as * mut ArcInner < T > ; let ptr = set_data_ptr ( fake_ptr , ( ptr as * mut u8 ). offset ( - offset )); Weak { ptr : NonNull :: new ( ptr ). expect ( "Invalid pointer passed to from_raw" ) } } } } } struct WeakInner < 'a > { weak : & 'a atomic :: AtomicUsize , strong : & 'a atomic :: AtomicUsize , } impl < T : ? Sized > Weak < T > { #[ stable ( feature = "arc_weak" , since = "1.4.0" )] pub fn upgrade ( & self ) - > Option < Arc < T > > { let inner = self . inner () ? ; let mut n = inner . strong . load ( Relaxed ); loop { if n = = 0 { return None ; } if n > MAX_REFCOUNT { abort (); } match inner . strong . compare_exchange_weak ( n , n + 1 , Relaxed , Relaxed ) { Ok ( _ ) = > return Some ( Arc :: from_inner ( self . ptr )), Err ( old ) = > n = old , } } } #[ stable ( feature = "weak_counts" , since = "1.41.0" )] pub fn strong_count ( & self ) - > usize { if let Some ( inner ) = self . inner () { inner . strong . load ( SeqCst ) } else { 0 } } #[ stable ( feature = "weak_counts" , since = "1.41.0" )] pub fn weak_count ( & self ) - > usize { self . inner () . map ( | inner | { let weak = inner . weak . load ( SeqCst ); let strong = inner . strong . load ( SeqCst ); if strong = = 0 { 0 } else { weak - 1 } }) . unwrap_or ( 0 ) } #[ inline ] fn inner ( & self ) - > Option < WeakInner < '_ > > { if is_dangling ( self . ptr ) { None } else { Some ( unsafe { let ptr = self . ptr . as_ptr (); WeakInner { strong : & ( * ptr ). strong , weak : & ( * ptr ). weak } }) } } #[ inline ] #[ stable ( feature = "weak_ptr_eq" , since = "1.39.0" )] pub fn ptr_eq ( & self , other : & Self ) - > bool { self . ptr . as_ptr () = = other . ptr . as_ptr () } } #[ stable ( feature = "arc_weak" , since = "1.4.0" )] impl < T : ? Sized > Clone for Weak < T > { #[ inline ] fn clone ( & self ) - > Weak < T > { let inner = if let Some ( inner ) = self . inner () { inner } else { return Weak { ptr : self . ptr }; }; let old_size = inner . weak . fetch_add ( 1 , Relaxed ); if old_size > MAX_REFCOUNT { abort (); } Weak { ptr : self . ptr } } } #[ stable ( feature = "downgraded_weak" , since = "1.10.0" )] impl < T > Default for Weak < T > { fn default () - > Weak < T > { Weak :: new () } } #[ stable ( feature = "arc_weak" , since = "1.4.0" )] impl < T : ? Sized > Drop for Weak < T > { fn drop ( & mut self ) { let inner = if let Some ( inner ) = self . inner () { inner } else { return }; if inner . weak . fetch_sub ( 1 , Release ) = = 1 { acquire ! ( inner . weak ); unsafe { Global . dealloc ( self . ptr . cast (), Layout :: for_value ( self . ptr . as_ref ())) } } } } #[ stable ( feature = "rust1" , since = "1.0.0" )] trait ArcEqIdent < T : ? Sized + PartialEq > { fn eq ( & self , other : & Arc < T > ) - > bool ; fn ne ( & self , other : & Arc < T > ) - > bool ; } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized + PartialEq > ArcEqIdent < T > for Arc < T > { #[ inline ] default fn eq ( & self , other : & Arc < T > ) - > bool { * * self = = * * other } #[ inline ] default fn ne ( & self , other : & Arc < T > ) - > bool { * * self ! = * * other } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized + crate :: rc :: MarkerEq > ArcEqIdent < T > for Arc < T > { #[ inline ] fn eq ( & self , other : & Arc < T > ) - > bool { Arc :: ptr_eq ( self , other ) | | * * self = = * * other } #[ inline ] fn ne ( & self , other : & Arc < T > ) - > bool { ! Arc :: ptr_eq ( self , other ) & & * * self ! = * * other } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized + PartialEq > PartialEq for Arc < T > { #[ inline ] fn eq ( & self , other : & Arc < T > ) - > bool { ArcEqIdent :: eq ( self , other ) } #[ inline ] fn ne ( & self , other : & Arc < T > ) - > bool { ArcEqIdent :: ne ( self , other ) } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized + PartialOrd > PartialOrd for Arc < T > { fn partial_cmp ( & self , other : & Arc < T > ) - > Option < Ordering > { ( * * self ). partial_cmp ( & * * other ) } fn lt ( & self , other : & Arc < T > ) - > bool { * ( * self ) < * ( * other ) } fn le ( & self , other : & Arc < T > ) - > bool { * ( * self ) < = * ( * other ) } fn gt ( & self , other : & Arc < T > ) - > bool { * ( * self ) > * ( * other ) } fn ge ( & self , other : & Arc < T > ) - > bool { * ( * self ) > = * ( * other ) } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized + Ord > Ord for Arc < T > { fn cmp ( & self , other : & Arc < T > ) - > Ordering { ( * * self ). cmp ( & * * other ) } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized + Eq > Eq for Arc < T > {} #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized + fmt :: Display > fmt :: Display for Arc < T > { fn fmt ( & self , f : & mut fmt :: Formatter < '_ > ) - > fmt :: Result { fmt :: Display :: fmt ( & * * self , f ) } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized + fmt :: Debug > fmt :: Debug for Arc < T > { fn fmt ( & self , f : & mut fmt :: Formatter < '_ > ) - > fmt :: Result { fmt :: Debug :: fmt ( & * * self , f ) } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized > fmt :: Pointer for Arc < T > { fn fmt ( & self , f : & mut fmt :: Formatter < '_ > ) - > fmt :: Result { fmt :: Pointer :: fmt ( & ( & * * self as * const T ), f ) } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : Default > Default for Arc < T > { fn default () - > Arc < T > { Arc :: new ( Default :: default ()) } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized + Hash > Hash for Arc < T > { fn hash < H : Hasher > ( & self , state : & mut H ) { ( * * self ). hash ( state ) } } #[ stable ( feature = "from_for_ptrs" , since = "1.6.0" )] impl < T > From < T > for Arc < T > { fn from ( t : T ) - > Self { Arc :: new ( t ) } } #[ stable ( feature = "shared_from_slice" , since = "1.21.0" )] impl < T : Clone > From < & [ T ] > for Arc < [ T ] > { #[ inline ] fn from ( v : & [ T ]) - > Arc < [ T ] > { < Self as ArcFromSlice < T > > :: from_slice ( v ) } } #[ stable ( feature = "shared_from_slice" , since = "1.21.0" )] impl From < & str > for Arc < str > { #[ inline ] fn from ( v : & str ) - > Arc < str > { let arc = Arc :: < [ u8 ] > :: from ( v . as_bytes ()); unsafe { Arc :: from_raw ( Arc :: into_raw ( arc ) as * const str ) } } } #[ stable ( feature = "shared_from_slice" , since = "1.21.0" )] impl From < String > for Arc < str > { #[ inline ] fn from ( v : String ) - > Arc < str > { Arc :: from ( & v [..]) } } #[ stable ( feature = "shared_from_slice" , since = "1.21.0" )] impl < T : ? Sized > From < Box < T > > for Arc < T > { #[ inline ] fn from ( v : Box < T > ) - > Arc < T > { Arc :: from_box ( v ) } } #[ stable ( feature = "shared_from_slice" , since = "1.21.0" )] impl < T > From < Vec < T > > for Arc < [ T ] > { #[ inline ] fn from ( mut v : Vec < T > ) - > Arc < [ T ] > { unsafe { let arc = Arc :: copy_from_slice ( & v ); v . set_len ( 0 ); arc } } } #[ stable ( feature = "shared_from_cow" , since = "1.45.0" )] impl < 'a , B > From < Cow < 'a , B > > for Arc < B > where B : ToOwned + ? Sized , Arc < B > : From < & 'a B > + From < B :: Owned > , { #[ inline ] fn from ( cow : Cow < 'a , B > ) - > Arc < B > { match cow { Cow :: Borrowed ( s ) = > Arc :: from ( s ), Cow :: Owned ( s ) = > Arc :: from ( s ), } } } #[ stable ( feature = "boxed_slice_try_from" , since = "1.43.0" )] impl < T , const N : usize > TryFrom < Arc < [ T ] > > for Arc < [ T ; N ] > where [ T ; N ]: LengthAtMost32 , { type Error = Arc < [ T ] > ; fn try_from ( boxed_slice : Arc < [ T ] > ) - > Result < Self , Self :: Error > { if boxed_slice . len () = = N { Ok ( unsafe { Arc :: from_raw ( Arc :: into_raw ( boxed_slice ) as * mut [ T ; N ]) }) } else { Err ( boxed_slice ) } } } #[ stable ( feature = "shared_from_iter" , since = "1.37.0" )] impl < T > iter :: FromIterator < T > for Arc < [ T ] > { fn from_iter < I : iter :: IntoIterator < Item = T > > ( iter : I ) - > Self { ToArcSlice :: to_arc_slice ( iter . into_iter ()) } } trait ToArcSlice < T > : Iterator < Item = T > + Sized { fn to_arc_slice ( self ) - > Arc < [ T ] > ; } impl < T , I : Iterator < Item = T > > ToArcSlice < T > for I { default fn to_arc_slice ( self ) - > Arc < [ T ] > { self . collect :: < Vec < T > > (). into () } } impl < T , I : iter :: TrustedLen < Item = T > > ToArcSlice < T > for I { fn to_arc_slice ( self ) - > Arc < [ T ] > { let ( low , high ) = self . size_hint (); if let Some ( high ) = high { debug_assert_eq ! ( low , high , "TrustedLen iterator's size hint is not exact: {:?}" , ( low , high ) ); unsafe { Arc :: from_iter_exact ( self , low ) } } else { self . collect :: < Vec < T > > (). into () } } } #[ stable ( feature = "rust1" , since = "1.0.0" )] impl < T : ? Sized > borrow :: Borrow < T > for Arc < T > { fn borrow ( & self ) - > & T { & * * self } } #[ stable ( since = "1.5.0" , feature = "smart_ptr_as_ref" )] impl < T : ? Sized > AsRef < T > for Arc < T > { fn as_ref ( & self ) - > & T { & * * self } } #[ stable ( feature = "pin" , since = "1.33.0" )] impl < T : ? Sized > Unpin for Arc < T > {} unsafe fn data_offset < T : ? Sized > ( ptr : * const T ) - > isize { unsafe { data_offset_align ( align_of_val ( & * ptr )) } } #[ inline ] fn data_offset_align ( align : usize ) - > isize { let layout = Layout :: new :: < ArcInner < () > > (); ( layout . size () + layout . padding_needed_for ( align )) as isize }