Skip to content

Commit 4ff583b

Browse files
committed
fallout from NonZero/Unique/Shared changes
1 parent 6e2efe3 commit 4ff583b

File tree

16 files changed

+123
-120
lines changed

16 files changed

+123
-120
lines changed

src/liballoc/arc.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ impl<T> Arc<T> {
277277
atomic::fence(Acquire);
278278

279279
unsafe {
280-
let ptr = *this.ptr;
281-
let elem = ptr::read(&(*ptr).data);
280+
let elem = ptr::read(&this.ptr.as_ref().data);
282281

283282
// Make a weak pointer to clean up the implicit strong-weak reference
284283
let _weak = Weak { ptr: this.ptr };
@@ -306,7 +305,7 @@ impl<T> Arc<T> {
306305
/// ```
307306
#[stable(feature = "rc_raw", since = "1.17.0")]
308307
pub fn into_raw(this: Self) -> *const T {
309-
let ptr = unsafe { &(**this.ptr).data as *const _ };
308+
let ptr: *const T = &*this;
310309
mem::forget(this);
311310
ptr
312311
}
@@ -345,7 +344,7 @@ impl<T> Arc<T> {
345344
// `data` field from the pointer.
346345
let ptr = (ptr as *const u8).offset(-offset_of!(ArcInner<T>, data));
347346
Arc {
348-
ptr: Shared::new(ptr as *const _),
347+
ptr: Shared::new(ptr as *mut u8 as *mut _),
349348
}
350349
}
351350
}
@@ -452,17 +451,17 @@ impl<T: ?Sized> Arc<T> {
452451
// `ArcInner` structure itself is `Sync` because the inner data is
453452
// `Sync` as well, so we're ok loaning out an immutable pointer to these
454453
// contents.
455-
unsafe { &**self.ptr }
454+
unsafe { self.ptr.as_ref() }
456455
}
457456

458457
// Non-inlined part of `drop`.
459458
#[inline(never)]
460459
unsafe fn drop_slow(&mut self) {
461-
let ptr = self.ptr.as_mut_ptr();
460+
let ptr = self.ptr.as_ptr();
462461

463462
// Destroy the data at this time, even though we may not free the box
464463
// allocation itself (there may still be weak pointers lying around).
465-
ptr::drop_in_place(&mut (*ptr).data);
464+
ptr::drop_in_place(&mut self.ptr.as_mut().data);
466465

467466
if self.inner().weak.fetch_sub(1, Release) == 1 {
468467
atomic::fence(Acquire);
@@ -488,9 +487,7 @@ impl<T: ?Sized> Arc<T> {
488487
/// assert!(!Arc::ptr_eq(&five, &other_five));
489488
/// ```
490489
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
491-
let this_ptr: *const ArcInner<T> = *this.ptr;
492-
let other_ptr: *const ArcInner<T> = *other.ptr;
493-
this_ptr == other_ptr
490+
this.ptr.as_ptr() == other.ptr.as_ptr()
494491
}
495492
}
496493

@@ -621,7 +618,7 @@ impl<T: Clone> Arc<T> {
621618
// here (due to zeroing) because data is no longer accessed by
622619
// other threads (due to there being no more strong refs at this
623620
// point).
624-
let mut swap = Arc::new(ptr::read(&(**weak.ptr).data));
621+
let mut swap = Arc::new(ptr::read(&weak.ptr.as_ref().data));
625622
mem::swap(this, &mut swap);
626623
mem::forget(swap);
627624
}
@@ -634,8 +631,7 @@ impl<T: Clone> Arc<T> {
634631
// As with `get_mut()`, the unsafety is ok because our reference was
635632
// either unique to begin with, or became one upon cloning the contents.
636633
unsafe {
637-
let inner = &mut *this.ptr.as_mut_ptr();
638-
&mut inner.data
634+
&mut this.ptr.as_mut().data
639635
}
640636
}
641637
}
@@ -677,8 +673,7 @@ impl<T: ?Sized> Arc<T> {
677673
// the Arc itself to be `mut`, so we're returning the only possible
678674
// reference to the inner data.
679675
unsafe {
680-
let inner = &mut *this.ptr.as_mut_ptr();
681-
Some(&mut inner.data)
676+
Some(&mut this.ptr.as_mut().data)
682677
}
683678
} else {
684679
None
@@ -867,7 +862,7 @@ impl<T: ?Sized> Weak<T> {
867862
#[inline]
868863
fn inner(&self) -> &ArcInner<T> {
869864
// See comments above for why this is "safe"
870-
unsafe { &**self.ptr }
865+
unsafe { self.ptr.as_ref() }
871866
}
872867
}
873868

@@ -951,7 +946,7 @@ impl<T: ?Sized> Drop for Weak<T> {
951946
/// assert!(other_weak_foo.upgrade().is_none());
952947
/// ```
953948
fn drop(&mut self) {
954-
let ptr = *self.ptr;
949+
let ptr = self.ptr.as_ptr();
955950

956951
// If we find out that we were the last weak pointer, then its time to
957952
// deallocate the data entirely. See the discussion in Arc::drop() about
@@ -1132,7 +1127,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Arc<T> {
11321127
#[stable(feature = "rust1", since = "1.0.0")]
11331128
impl<T: ?Sized> fmt::Pointer for Arc<T> {
11341129
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1135-
fmt::Pointer::fmt(&*self.ptr, f)
1130+
fmt::Pointer::fmt(&self.ptr, f)
11361131
}
11371132
}
11381133

src/liballoc/raw_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<T> RawVec<T> {
151151
/// heap::EMPTY if `cap = 0` or T is zero-sized. In the former case, you must
152152
/// be careful.
153153
pub fn ptr(&self) -> *mut T {
154-
*self.ptr
154+
self.ptr.ptr()
155155
}
156156

157157
/// Gets the capacity of the allocation.
@@ -563,7 +563,7 @@ unsafe impl<#[may_dangle] T> Drop for RawVec<T> {
563563

564564
let num_bytes = elem_size * self.cap;
565565
unsafe {
566-
heap::deallocate(*self.ptr as *mut _, num_bytes, align);
566+
heap::deallocate(self.ptr() as *mut u8, num_bytes, align);
567567
}
568568
}
569569
}

src/liballoc/rc.rs

+22-28
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ use core::cell::Cell;
230230
use core::cmp::Ordering;
231231
use core::fmt;
232232
use core::hash::{Hash, Hasher};
233-
use core::intrinsics::{abort, assume};
233+
use core::intrinsics::abort;
234234
use core::marker;
235235
use core::marker::Unsize;
236236
use core::mem::{self, align_of_val, forget, size_of, size_of_val, uninitialized};
@@ -358,7 +358,7 @@ impl<T> Rc<T> {
358358
/// ```
359359
#[stable(feature = "rc_raw", since = "1.17.0")]
360360
pub fn into_raw(this: Self) -> *const T {
361-
let ptr = unsafe { &mut (*this.ptr.as_mut_ptr()).value as *const _ };
361+
let ptr: *const T = &*this;
362362
mem::forget(this);
363363
ptr
364364
}
@@ -395,7 +395,11 @@ impl<T> Rc<T> {
395395
pub unsafe fn from_raw(ptr: *const T) -> Self {
396396
// To find the corresponding pointer to the `RcBox` we need to subtract the offset of the
397397
// `value` field from the pointer.
398-
Rc { ptr: Shared::new((ptr as *const u8).offset(-offset_of!(RcBox<T>, value)) as *const _) }
398+
399+
let ptr = (ptr as *const u8).offset(-offset_of!(RcBox<T>, value));
400+
Rc {
401+
ptr: Shared::new(ptr as *mut u8 as *mut _)
402+
}
399403
}
400404
}
401405

@@ -451,7 +455,7 @@ impl<T> Rc<[T]> {
451455
// Free the original allocation without freeing its (moved) contents.
452456
box_free(Box::into_raw(value));
453457

454-
Rc { ptr: Shared::new(ptr as *const _) }
458+
Rc { ptr: Shared::new(ptr as *mut _) }
455459
}
456460
}
457461
}
@@ -553,8 +557,9 @@ impl<T: ?Sized> Rc<T> {
553557
#[stable(feature = "rc_unique", since = "1.4.0")]
554558
pub fn get_mut(this: &mut Self) -> Option<&mut T> {
555559
if Rc::is_unique(this) {
556-
let inner = unsafe { &mut *this.ptr.as_mut_ptr() };
557-
Some(&mut inner.value)
560+
unsafe {
561+
Some(&mut this.ptr.as_mut().value)
562+
}
558563
} else {
559564
None
560565
}
@@ -578,9 +583,7 @@ impl<T: ?Sized> Rc<T> {
578583
/// assert!(!Rc::ptr_eq(&five, &other_five));
579584
/// ```
580585
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
581-
let this_ptr: *const RcBox<T> = *this.ptr;
582-
let other_ptr: *const RcBox<T> = *other.ptr;
583-
this_ptr == other_ptr
586+
this.ptr.as_ptr() == other.ptr.as_ptr()
584587
}
585588
}
586589

@@ -623,7 +626,7 @@ impl<T: Clone> Rc<T> {
623626
} else if Rc::weak_count(this) != 0 {
624627
// Can just steal the data, all that's left is Weaks
625628
unsafe {
626-
let mut swap = Rc::new(ptr::read(&(**this.ptr).value));
629+
let mut swap = Rc::new(ptr::read(&this.ptr.as_ref().value));
627630
mem::swap(this, &mut swap);
628631
swap.dec_strong();
629632
// Remove implicit strong-weak ref (no need to craft a fake
@@ -637,8 +640,9 @@ impl<T: Clone> Rc<T> {
637640
// reference count is guaranteed to be 1 at this point, and we required
638641
// the `Rc<T>` itself to be `mut`, so we're returning the only possible
639642
// reference to the inner value.
640-
let inner = unsafe { &mut *this.ptr.as_mut_ptr() };
641-
&mut inner.value
643+
unsafe {
644+
&mut this.ptr.as_mut().value
645+
}
642646
}
643647
}
644648

@@ -683,12 +687,12 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
683687
/// ```
684688
fn drop(&mut self) {
685689
unsafe {
686-
let ptr = self.ptr.as_mut_ptr();
690+
let ptr = self.ptr.as_ptr();
687691

688692
self.dec_strong();
689693
if self.strong() == 0 {
690694
// destroy the contained object
691-
ptr::drop_in_place(&mut (*ptr).value);
695+
ptr::drop_in_place(self.ptr.as_mut());
692696

693697
// remove the implicit "strong weak" pointer now that we've
694698
// destroyed the contents.
@@ -925,7 +929,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Rc<T> {
925929
#[stable(feature = "rust1", since = "1.0.0")]
926930
impl<T: ?Sized> fmt::Pointer for Rc<T> {
927931
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
928-
fmt::Pointer::fmt(&*self.ptr, f)
932+
fmt::Pointer::fmt(&self.ptr, f)
929933
}
930934
}
931935

@@ -1067,7 +1071,7 @@ impl<T: ?Sized> Drop for Weak<T> {
10671071
/// ```
10681072
fn drop(&mut self) {
10691073
unsafe {
1070-
let ptr = *self.ptr;
1074+
let ptr = self.ptr.as_ptr();
10711075

10721076
self.dec_weak();
10731077
// the weak count starts at 1, and will only go to zero if all
@@ -1175,12 +1179,7 @@ impl<T: ?Sized> RcBoxPtr<T> for Rc<T> {
11751179
#[inline(always)]
11761180
fn inner(&self) -> &RcBox<T> {
11771181
unsafe {
1178-
// Safe to assume this here, as if it weren't true, we'd be breaking
1179-
// the contract anyway.
1180-
// This allows the null check to be elided in the destructor if we
1181-
// manipulated the reference count in the same function.
1182-
assume(!(*(&self.ptr as *const _ as *const *const ())).is_null());
1183-
&(**self.ptr)
1182+
self.ptr.as_ref()
11841183
}
11851184
}
11861185
}
@@ -1189,12 +1188,7 @@ impl<T: ?Sized> RcBoxPtr<T> for Weak<T> {
11891188
#[inline(always)]
11901189
fn inner(&self) -> &RcBox<T> {
11911190
unsafe {
1192-
// Safe to assume this here, as if it weren't true, we'd be breaking
1193-
// the contract anyway.
1194-
// This allows the null check to be elided in the destructor if we
1195-
// manipulated the reference count in the same function.
1196-
assume(!(*(&self.ptr as *const _ as *const *const ())).is_null());
1197-
&(**self.ptr)
1191+
self.ptr.as_ref()
11981192
}
11991193
}
12001194
}

src/libcollections/btree/node.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ impl<K, V> BoxedNode<K, V> {
152152
}
153153

154154
unsafe fn from_ptr(ptr: NonZero<*const LeafNode<K, V>>) -> Self {
155-
BoxedNode { ptr: Unique::new(*ptr as *mut LeafNode<K, V>) }
155+
BoxedNode { ptr: Unique::new(ptr.get() as *mut LeafNode<K, V>) }
156156
}
157157

158158
fn as_ptr(&self) -> NonZero<*const LeafNode<K, V>> {
159159
unsafe {
160-
NonZero::new(*self.ptr as *const LeafNode<K, V>)
160+
NonZero::new(self.ptr.as_ptr())
161161
}
162162
}
163163
}
@@ -241,7 +241,7 @@ impl<K, V> Root<K, V> {
241241
pub fn pop_level(&mut self) {
242242
debug_assert!(self.height > 0);
243243

244-
let top = *self.node.ptr as *mut u8;
244+
let top = self.node.ptr.as_ptr() as *mut u8;
245245

246246
self.node = unsafe {
247247
BoxedNode::from_ptr(self.as_mut()
@@ -308,15 +308,15 @@ unsafe impl<K: Send, V: Send, Type> Send
308308
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
309309
fn as_internal(&self) -> &InternalNode<K, V> {
310310
unsafe {
311-
&*(*self.node as *const InternalNode<K, V>)
311+
&*(self.node.get() as *const InternalNode<K, V>)
312312
}
313313
}
314314
}
315315

316316
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
317317
fn as_internal_mut(&mut self) -> &mut InternalNode<K, V> {
318318
unsafe {
319-
&mut *(*self.node as *mut InternalNode<K, V>)
319+
&mut *(self.node.get() as *mut InternalNode<K, V>)
320320
}
321321
}
322322
}
@@ -358,7 +358,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
358358

359359
fn as_leaf(&self) -> &LeafNode<K, V> {
360360
unsafe {
361-
&**self.node
361+
&*self.node.get()
362362
}
363363
}
364364

@@ -510,7 +510,7 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
510510

511511
fn as_leaf_mut(&mut self) -> &mut LeafNode<K, V> {
512512
unsafe {
513-
&mut *(*self.node as *mut LeafNode<K, V>)
513+
&mut *(self.node.get() as *mut LeafNode<K, V>)
514514
}
515515
}
516516

@@ -1253,13 +1253,13 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12531253
}
12541254

12551255
heap::deallocate(
1256-
*right_node.node as *mut u8,
1256+
right_node.node.get() as *mut u8,
12571257
mem::size_of::<InternalNode<K, V>>(),
12581258
mem::align_of::<InternalNode<K, V>>()
12591259
);
12601260
} else {
12611261
heap::deallocate(
1262-
*right_node.node as *mut u8,
1262+
right_node.node.get() as *mut u8,
12631263
mem::size_of::<LeafNode<K, V>>(),
12641264
mem::align_of::<LeafNode<K, V>>()
12651265
);

0 commit comments

Comments
 (0)