@@ -286,6 +286,19 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Rc<U>> for Rc<T> {}
286
286
#[ unstable( feature = "dispatch_from_dyn" , issue = "0" ) ]
287
287
impl < T : ?Sized + Unsize < U > , U : ?Sized > DispatchFromDyn < Rc < U > > for Rc < T > { }
288
288
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
+
289
302
impl < T > Rc < T > {
290
303
/// Constructs a new `Rc<T>`.
291
304
///
@@ -298,18 +311,15 @@ impl<T> Rc<T> {
298
311
/// ```
299
312
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
300
313
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
+ } ) )
313
323
}
314
324
315
325
/// Constructs a new `Pin<Rc<T>>`. If `T` does not implement `Unpin`, then
@@ -422,10 +432,7 @@ impl<T: ?Sized> Rc<T> {
422
432
let fake_ptr = ptr as * mut RcBox < T > ;
423
433
let rc_ptr = set_data_ptr ( fake_ptr, ( ptr as * mut u8 ) . offset ( -offset) ) ;
424
434
425
- Rc {
426
- ptr : NonNull :: new_unchecked ( rc_ptr) ,
427
- phantom : PhantomData ,
428
- }
435
+ Self :: from_ptr ( rc_ptr)
429
436
}
430
437
431
438
/// Consumes the `Rc`, returning the wrapped pointer as `NonNull<T>`.
@@ -683,7 +690,7 @@ impl Rc<dyn Any> {
683
690
if ( * self ) . is :: < T > ( ) {
684
691
let ptr = self . ptr . cast :: < RcBox < T > > ( ) ;
685
692
forget ( self ) ;
686
- Ok ( Rc { ptr , phantom : PhantomData } )
693
+ Ok ( Rc :: from_inner ( ptr ) )
687
694
} else {
688
695
Err ( self )
689
696
}
@@ -731,7 +738,7 @@ impl<T: ?Sized> Rc<T> {
731
738
// Free the allocation without dropping its contents
732
739
box_free ( box_unique) ;
733
740
734
- Rc { ptr : NonNull :: new_unchecked ( ptr) , phantom : PhantomData }
741
+ Self :: from_ptr ( ptr)
735
742
}
736
743
}
737
744
}
@@ -758,7 +765,7 @@ impl<T> Rc<[T]> {
758
765
& mut ( * ptr) . value as * mut [ T ] as * mut T ,
759
766
v. len ( ) ) ;
760
767
761
- Rc { ptr : NonNull :: new_unchecked ( ptr) , phantom : PhantomData }
768
+ Self :: from_ptr ( ptr)
762
769
}
763
770
}
764
771
@@ -800,7 +807,7 @@ impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
800
807
// Pointer to first element
801
808
let elems = & mut ( * ptr) . value as * mut [ T ] as * mut T ;
802
809
803
- let mut guard = Guard {
810
+ let mut guard = Guard {
804
811
mem : NonNull :: new_unchecked ( mem) ,
805
812
elems : elems,
806
813
layout : layout,
@@ -815,7 +822,7 @@ impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
815
822
// All clear. Forget the guard so it doesn't free the new RcBox.
816
823
forget ( guard) ;
817
824
818
- Rc { ptr : NonNull :: new_unchecked ( ptr) , phantom : PhantomData }
825
+ Self :: from_ptr ( ptr)
819
826
}
820
827
}
821
828
}
@@ -907,7 +914,7 @@ impl<T: ?Sized> Clone for Rc<T> {
907
914
#[ inline]
908
915
fn clone ( & self ) -> Rc < T > {
909
916
self . inc_strong ( ) ;
910
- Rc { ptr : self . ptr , phantom : PhantomData }
917
+ Self :: from_inner ( self . ptr )
911
918
}
912
919
}
913
920
@@ -1463,7 +1470,7 @@ impl<T: ?Sized> Weak<T> {
1463
1470
None
1464
1471
} else {
1465
1472
inner. inc_strong ( ) ;
1466
- Some ( Rc { ptr : self . ptr , phantom : PhantomData } )
1473
+ Some ( Rc :: from_inner ( self . ptr ) )
1467
1474
}
1468
1475
}
1469
1476
0 commit comments