@@ -286,6 +286,19 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Rc<U>> for Rc<T> {}
286286#[ unstable( feature = "dispatch_from_dyn" , issue = "0" ) ]
287287impl < T : ?Sized + Unsize < U > , U : ?Sized > DispatchFromDyn < Rc < U > > for Rc < T > { }
288288
289+ impl < T : ?Sized > Rc < T > {
290+ fn from_inner ( ptr : NonNull < RcBox < T > > ) -> Self {
291+ Self {
292+ ptr,
293+ phantom : PhantomData ,
294+ }
295+ }
296+
297+ unsafe fn from_ptr ( ptr : * mut RcBox < T > ) -> Self {
298+ Self :: from_inner ( NonNull :: new_unchecked ( ptr) )
299+ }
300+ }
301+
289302impl < T > Rc < T > {
290303 /// Constructs a new `Rc<T>`.
291304 ///
@@ -298,18 +311,15 @@ impl<T> Rc<T> {
298311 /// ```
299312 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
300313 pub fn new ( value : T ) -> Rc < T > {
301- Rc {
302- // there is an implicit weak pointer owned by all the strong
303- // pointers, which ensures that the weak destructor never frees
304- // the allocation while the strong destructor is running, even
305- // if the weak pointer is stored inside the strong one.
306- ptr : Box :: into_raw_non_null ( box RcBox {
307- strong : Cell :: new ( 1 ) ,
308- weak : Cell :: new ( 1 ) ,
309- value,
310- } ) ,
311- phantom : PhantomData ,
312- }
314+ // There is an implicit weak pointer owned by all the strong
315+ // pointers, which ensures that the weak destructor never frees
316+ // the allocation while the strong destructor is running, even
317+ // if the weak pointer is stored inside the strong one.
318+ Self :: from_inner ( Box :: into_raw_non_null ( box RcBox {
319+ strong : Cell :: new ( 1 ) ,
320+ weak : Cell :: new ( 1 ) ,
321+ value,
322+ } ) )
313323 }
314324
315325 /// Constructs a new `Pin<Rc<T>>`. If `T` does not implement `Unpin`, then
@@ -422,10 +432,7 @@ impl<T: ?Sized> Rc<T> {
422432 let fake_ptr = ptr as * mut RcBox < T > ;
423433 let rc_ptr = set_data_ptr ( fake_ptr, ( ptr as * mut u8 ) . offset ( -offset) ) ;
424434
425- Rc {
426- ptr : NonNull :: new_unchecked ( rc_ptr) ,
427- phantom : PhantomData ,
428- }
435+ Self :: from_ptr ( rc_ptr)
429436 }
430437
431438 /// Consumes the `Rc`, returning the wrapped pointer as `NonNull<T>`.
@@ -683,7 +690,7 @@ impl Rc<dyn Any> {
683690 if ( * self ) . is :: < T > ( ) {
684691 let ptr = self . ptr . cast :: < RcBox < T > > ( ) ;
685692 forget ( self ) ;
686- Ok ( Rc { ptr , phantom : PhantomData } )
693+ Ok ( Rc :: from_inner ( ptr ) )
687694 } else {
688695 Err ( self )
689696 }
@@ -731,7 +738,7 @@ impl<T: ?Sized> Rc<T> {
731738 // Free the allocation without dropping its contents
732739 box_free ( box_unique) ;
733740
734- Rc { ptr : NonNull :: new_unchecked ( ptr) , phantom : PhantomData }
741+ Self :: from_ptr ( ptr)
735742 }
736743 }
737744}
@@ -758,7 +765,7 @@ impl<T> Rc<[T]> {
758765 & mut ( * ptr) . value as * mut [ T ] as * mut T ,
759766 v. len ( ) ) ;
760767
761- Rc { ptr : NonNull :: new_unchecked ( ptr) , phantom : PhantomData }
768+ Self :: from_ptr ( ptr)
762769 }
763770}
764771
@@ -800,7 +807,7 @@ impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
800807 // Pointer to first element
801808 let elems = & mut ( * ptr) . value as * mut [ T ] as * mut T ;
802809
803- let mut guard = Guard {
810+ let mut guard = Guard {
804811 mem : NonNull :: new_unchecked ( mem) ,
805812 elems : elems,
806813 layout : layout,
@@ -815,7 +822,7 @@ impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
815822 // All clear. Forget the guard so it doesn't free the new RcBox.
816823 forget ( guard) ;
817824
818- Rc { ptr : NonNull :: new_unchecked ( ptr) , phantom : PhantomData }
825+ Self :: from_ptr ( ptr)
819826 }
820827 }
821828}
@@ -907,7 +914,7 @@ impl<T: ?Sized> Clone for Rc<T> {
907914 #[ inline]
908915 fn clone ( & self ) -> Rc < T > {
909916 self . inc_strong ( ) ;
910- Rc { ptr : self . ptr , phantom : PhantomData }
917+ Self :: from_inner ( self . ptr )
911918 }
912919}
913920
@@ -1463,7 +1470,7 @@ impl<T: ?Sized> Weak<T> {
14631470 None
14641471 } else {
14651472 inner. inc_strong ( ) ;
1466- Some ( Rc { ptr : self . ptr , phantom : PhantomData } )
1473+ Some ( Rc :: from_inner ( self . ptr ) )
14671474 }
14681475 }
14691476
0 commit comments