@@ -151,8 +151,7 @@ use core::mem::{transmute, min_align_of, size_of, forget};
151
151
use core:: ops:: { Deref , Drop } ;
152
152
use core:: option:: Option ;
153
153
use core:: option:: Option :: { Some , None } ;
154
- use core:: ptr;
155
- use core:: ptr:: RawPtr ;
154
+ use core:: ptr:: { mod, NonZero , RawPtr } ;
156
155
use core:: result:: Result ;
157
156
use core:: result:: Result :: { Ok , Err } ;
158
157
@@ -172,7 +171,7 @@ struct RcBox<T> {
172
171
pub struct Rc < T > {
173
172
// FIXME #12808: strange names to try to avoid interfering with
174
173
// field accesses of the contained type via Deref
175
- _ptr : * mut RcBox < T > ,
174
+ _ptr : NonZero < * mut RcBox < T > > ,
176
175
_nosend : marker:: NoSend ,
177
176
_noshare : marker:: NoSync
178
177
}
@@ -196,11 +195,11 @@ impl<T> Rc<T> {
196
195
// destructor never frees the allocation while the
197
196
// strong destructor is running, even if the weak
198
197
// 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
@@ -344,7 +347,10 @@ impl<T: Clone> Rc<T> {
344
347
// reference count is guaranteed to be 1 at this point, and we required
345
348
// the `Rc<T>` itself to be `mut`, so we're returning the only possible
346
349
// reference to the inner value.
347
- let inner = unsafe { & mut * self . _ptr } ;
350
+ let inner = unsafe {
351
+ let NonZero ( ptr) = self . _ptr ;
352
+ & mut * ptr
353
+ } ;
348
354
& mut inner. value
349
355
}
350
356
}
@@ -386,7 +392,8 @@ impl<T> Drop for Rc<T> {
386
392
/// ```
387
393
fn drop ( & mut self ) {
388
394
unsafe {
389
- if !self . _ptr . is_null ( ) {
395
+ let NonZero ( ptr) = self . _ptr ;
396
+ if !ptr. is_null ( ) {
390
397
self . dec_strong ( ) ;
391
398
if self . strong ( ) == 0 {
392
399
ptr:: read ( & * * self ) ; // destroy the contained object
@@ -396,7 +403,7 @@ impl<T> Drop for Rc<T> {
396
403
self . dec_weak ( ) ;
397
404
398
405
if self . weak ( ) == 0 {
399
- deallocate ( self . _ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
406
+ deallocate ( ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
400
407
min_align_of :: < RcBox < T > > ( ) )
401
408
}
402
409
}
@@ -604,7 +611,7 @@ impl<T: fmt::Show> fmt::Show for Rc<T> {
604
611
pub struct Weak < T > {
605
612
// FIXME #12808: strange names to try to avoid interfering with
606
613
// field accesses of the contained type via Deref
607
- _ptr : * mut RcBox < T > ,
614
+ _ptr : NonZero < * mut RcBox < T > > ,
608
615
_nosend : marker:: NoSend ,
609
616
_noshare : marker:: NoSync
610
617
}
@@ -668,12 +675,13 @@ impl<T> Drop for Weak<T> {
668
675
/// ```
669
676
fn drop ( & mut self ) {
670
677
unsafe {
671
- if !self . _ptr . is_null ( ) {
678
+ let NonZero ( ptr) = self . _ptr ;
679
+ if !ptr. is_null ( ) {
672
680
self . dec_weak ( ) ;
673
681
// the weak count starts at 1, and will only go to
674
682
// zero if all the strong pointers have disappeared.
675
683
if self . weak ( ) == 0 {
676
- deallocate ( self . _ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
684
+ deallocate ( ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
677
685
min_align_of :: < RcBox < T > > ( ) )
678
686
}
679
687
}
@@ -728,12 +736,18 @@ trait RcBoxPtr<T> {
728
736
729
737
impl < T > RcBoxPtr < T > for Rc < T > {
730
738
#[ inline( always) ]
731
- fn inner ( & self ) -> & RcBox < T > { unsafe { & ( * self . _ptr ) } }
739
+ fn inner ( & self ) -> & RcBox < T > {
740
+ let NonZero ( ptr) = self . _ptr ;
741
+ unsafe { & ( * ptr) }
742
+ }
732
743
}
733
744
734
745
impl < T > RcBoxPtr < T > for Weak < T > {
735
746
#[ inline( always) ]
736
- fn inner ( & self ) -> & RcBox < T > { unsafe { & ( * self . _ptr ) } }
747
+ fn inner ( & self ) -> & RcBox < T > {
748
+ let NonZero ( ptr) = self . _ptr ;
749
+ unsafe { & ( * ptr) }
750
+ }
737
751
}
738
752
739
753
#[ cfg( test) ]
0 commit comments