@@ -153,8 +153,7 @@ use core::mem::{transmute, min_align_of, size_of, forget};
153
153
use core:: ops:: { Deref , Drop } ;
154
154
use core:: option:: Option ;
155
155
use core:: option:: Option :: { Some , None } ;
156
- use core:: ptr;
157
- use core:: ptr:: RawPtr ;
156
+ use core:: ptr:: { mod, NonZero , RawPtr } ;
158
157
use core:: result:: Result ;
159
158
use core:: result:: Result :: { Ok , Err } ;
160
159
@@ -174,7 +173,7 @@ struct RcBox<T> {
174
173
pub struct Rc < T > {
175
174
// FIXME #12808: strange names to try to avoid interfering with field accesses of the contained
176
175
// type via Deref
177
- _ptr : * mut RcBox < T > ,
176
+ _ptr : NonZero < * mut RcBox < T > > ,
178
177
_nosend : marker:: NoSend ,
179
178
_noshare : marker:: NoSync
180
179
}
@@ -196,11 +195,11 @@ impl<T> Rc<T> {
196
195
// there is an implicit weak pointer owned by all the strong pointers, which
197
196
// ensures that the weak destructor never frees the allocation while the strong
198
197
// destructor is running, even if the weak pointer is stored inside the strong one.
199
- _ptr : transmute ( box RcBox {
198
+ _ptr : NonZero ( transmute ( box RcBox {
200
199
value : value,
201
200
strong : Cell :: new ( 1 ) ,
202
201
weak : Cell :: new ( 1 )
203
- } ) ,
202
+ } ) ) ,
204
203
_nosend : marker:: NoSend ,
205
204
_noshare : marker:: NoSync
206
205
}
@@ -281,7 +280,8 @@ pub fn try_unwrap<T>(rc: Rc<T>) -> Result<T, Rc<T>> {
281
280
let val = ptr:: read ( & * rc) ; // copy the contained object
282
281
// destruct the box and skip our Drop
283
282
// we can ignore the refcounts because we know we're unique
284
- deallocate ( rc. _ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
283
+ let NonZero ( ptr) = rc. _ptr ;
284
+ deallocate ( ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
285
285
min_align_of :: < RcBox < T > > ( ) ) ;
286
286
forget ( rc) ;
287
287
Ok ( val)
@@ -311,7 +311,10 @@ pub fn try_unwrap<T>(rc: Rc<T>) -> Result<T, Rc<T>> {
311
311
#[ experimental]
312
312
pub fn get_mut < ' a , T > ( rc : & ' a mut Rc < T > ) -> Option < & ' a mut T > {
313
313
if is_unique ( rc) {
314
- let inner = unsafe { & mut * rc. _ptr } ;
314
+ let inner = unsafe {
315
+ let NonZero ( ptr) = rc. _ptr ;
316
+ & mut * ptr
317
+ } ;
315
318
Some ( & mut inner. value )
316
319
} else {
317
320
None
@@ -343,7 +346,10 @@ impl<T: Clone> Rc<T> {
343
346
// pointer that will ever be returned to T. Our reference count is guaranteed to be 1 at
344
347
// this point, and we required the `Rc<T>` itself to be `mut`, so we're returning the only
345
348
// possible reference to the inner value.
346
- let inner = unsafe { & mut * self . _ptr } ;
349
+ let inner = unsafe {
350
+ let NonZero ( ptr) = self . _ptr ;
351
+ & mut * ptr
352
+ } ;
347
353
& mut inner. value
348
354
}
349
355
}
@@ -391,7 +397,8 @@ impl<T> Drop for Rc<T> {
391
397
/// ```
392
398
fn drop ( & mut self ) {
393
399
unsafe {
394
- if !self . _ptr . is_null ( ) {
400
+ let NonZero ( ptr) = self . _ptr ;
401
+ if !ptr. is_null ( ) {
395
402
self . dec_strong ( ) ;
396
403
if self . strong ( ) == 0 {
397
404
ptr:: read ( & * * self ) ; // destroy the contained object
@@ -401,7 +408,7 @@ impl<T> Drop for Rc<T> {
401
408
self . dec_weak ( ) ;
402
409
403
410
if self . weak ( ) == 0 {
404
- deallocate ( self . _ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
411
+ deallocate ( ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
405
412
min_align_of :: < RcBox < T > > ( ) )
406
413
}
407
414
}
@@ -618,7 +625,7 @@ impl<T: fmt::Show> fmt::Show for Rc<T> {
618
625
pub struct Weak < T > {
619
626
// FIXME #12808: strange names to try to avoid interfering with
620
627
// field accesses of the contained type via Deref
621
- _ptr : * mut RcBox < T > ,
628
+ _ptr : NonZero < * mut RcBox < T > > ,
622
629
_nosend : marker:: NoSend ,
623
630
_noshare : marker:: NoSync
624
631
}
@@ -682,12 +689,13 @@ impl<T> Drop for Weak<T> {
682
689
/// ```
683
690
fn drop ( & mut self ) {
684
691
unsafe {
685
- if !self . _ptr . is_null ( ) {
692
+ let NonZero ( ptr) = self . _ptr ;
693
+ if !ptr. is_null ( ) {
686
694
self . dec_weak ( ) ;
687
695
// the weak count starts at 1, and will only go to zero if all the strong pointers
688
696
// have disappeared.
689
697
if self . weak ( ) == 0 {
690
- deallocate ( self . _ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
698
+ deallocate ( ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
691
699
min_align_of :: < RcBox < T > > ( ) )
692
700
}
693
701
}
@@ -742,12 +750,18 @@ trait RcBoxPtr<T> {
742
750
743
751
impl < T > RcBoxPtr < T > for Rc < T > {
744
752
#[ inline( always) ]
745
- fn inner ( & self ) -> & RcBox < T > { unsafe { & ( * self . _ptr ) } }
753
+ fn inner ( & self ) -> & RcBox < T > {
754
+ let NonZero ( ptr) = self . _ptr ;
755
+ unsafe { & ( * ptr) }
756
+ }
746
757
}
747
758
748
759
impl < T > RcBoxPtr < T > for Weak < T > {
749
760
#[ inline( always) ]
750
- fn inner ( & self ) -> & RcBox < T > { unsafe { & ( * self . _ptr ) } }
761
+ fn inner ( & self ) -> & RcBox < T > {
762
+ let NonZero ( ptr) = self . _ptr ;
763
+ unsafe { & ( * ptr) }
764
+ }
751
765
}
752
766
753
767
#[ cfg( test) ]
0 commit comments