1
Fork 0

fallout from NonZero/Unique/Shared changes

This commit is contained in:
Alexis Beingessner 2017-04-04 12:31:38 -04:00
parent 6e2efe3aa4
commit 4ff583b116
16 changed files with 123 additions and 120 deletions

View file

@ -277,8 +277,7 @@ impl<T> Arc<T> {
atomic::fence(Acquire); atomic::fence(Acquire);
unsafe { unsafe {
let ptr = *this.ptr; let elem = ptr::read(&this.ptr.as_ref().data);
let elem = ptr::read(&(*ptr).data);
// Make a weak pointer to clean up the implicit strong-weak reference // Make a weak pointer to clean up the implicit strong-weak reference
let _weak = Weak { ptr: this.ptr }; let _weak = Weak { ptr: this.ptr };
@ -306,7 +305,7 @@ impl<T> Arc<T> {
/// ``` /// ```
#[stable(feature = "rc_raw", since = "1.17.0")] #[stable(feature = "rc_raw", since = "1.17.0")]
pub fn into_raw(this: Self) -> *const T { pub fn into_raw(this: Self) -> *const T {
let ptr = unsafe { &(**this.ptr).data as *const _ }; let ptr: *const T = &*this;
mem::forget(this); mem::forget(this);
ptr ptr
} }
@ -345,7 +344,7 @@ impl<T> Arc<T> {
// `data` field from the pointer. // `data` field from the pointer.
let ptr = (ptr as *const u8).offset(-offset_of!(ArcInner<T>, data)); let ptr = (ptr as *const u8).offset(-offset_of!(ArcInner<T>, data));
Arc { Arc {
ptr: Shared::new(ptr as *const _), ptr: Shared::new(ptr as *mut u8 as *mut _),
} }
} }
} }
@ -452,17 +451,17 @@ impl<T: ?Sized> Arc<T> {
// `ArcInner` structure itself is `Sync` because the inner data is // `ArcInner` structure itself is `Sync` because the inner data is
// `Sync` as well, so we're ok loaning out an immutable pointer to these // `Sync` as well, so we're ok loaning out an immutable pointer to these
// contents. // contents.
unsafe { &**self.ptr } unsafe { self.ptr.as_ref() }
} }
// Non-inlined part of `drop`. // Non-inlined part of `drop`.
#[inline(never)] #[inline(never)]
unsafe fn drop_slow(&mut self) { unsafe fn drop_slow(&mut self) {
let ptr = self.ptr.as_mut_ptr(); let ptr = self.ptr.as_ptr();
// Destroy the data at this time, even though we may not free the box // Destroy the data at this time, even though we may not free the box
// allocation itself (there may still be weak pointers lying around). // allocation itself (there may still be weak pointers lying around).
ptr::drop_in_place(&mut (*ptr).data); ptr::drop_in_place(&mut self.ptr.as_mut().data);
if self.inner().weak.fetch_sub(1, Release) == 1 { if self.inner().weak.fetch_sub(1, Release) == 1 {
atomic::fence(Acquire); atomic::fence(Acquire);
@ -488,9 +487,7 @@ impl<T: ?Sized> Arc<T> {
/// assert!(!Arc::ptr_eq(&five, &other_five)); /// assert!(!Arc::ptr_eq(&five, &other_five));
/// ``` /// ```
pub fn ptr_eq(this: &Self, other: &Self) -> bool { pub fn ptr_eq(this: &Self, other: &Self) -> bool {
let this_ptr: *const ArcInner<T> = *this.ptr; this.ptr.as_ptr() == other.ptr.as_ptr()
let other_ptr: *const ArcInner<T> = *other.ptr;
this_ptr == other_ptr
} }
} }
@ -621,7 +618,7 @@ impl<T: Clone> Arc<T> {
// here (due to zeroing) because data is no longer accessed by // here (due to zeroing) because data is no longer accessed by
// other threads (due to there being no more strong refs at this // other threads (due to there being no more strong refs at this
// point). // point).
let mut swap = Arc::new(ptr::read(&(**weak.ptr).data)); let mut swap = Arc::new(ptr::read(&weak.ptr.as_ref().data));
mem::swap(this, &mut swap); mem::swap(this, &mut swap);
mem::forget(swap); mem::forget(swap);
} }
@ -634,8 +631,7 @@ impl<T: Clone> Arc<T> {
// As with `get_mut()`, the unsafety is ok because our reference was // As with `get_mut()`, the unsafety is ok because our reference was
// either unique to begin with, or became one upon cloning the contents. // either unique to begin with, or became one upon cloning the contents.
unsafe { unsafe {
let inner = &mut *this.ptr.as_mut_ptr(); &mut this.ptr.as_mut().data
&mut inner.data
} }
} }
} }
@ -677,8 +673,7 @@ impl<T: ?Sized> Arc<T> {
// the Arc itself to be `mut`, so we're returning the only possible // the Arc itself to be `mut`, so we're returning the only possible
// reference to the inner data. // reference to the inner data.
unsafe { unsafe {
let inner = &mut *this.ptr.as_mut_ptr(); Some(&mut this.ptr.as_mut().data)
Some(&mut inner.data)
} }
} else { } else {
None None
@ -867,7 +862,7 @@ impl<T: ?Sized> Weak<T> {
#[inline] #[inline]
fn inner(&self) -> &ArcInner<T> { fn inner(&self) -> &ArcInner<T> {
// See comments above for why this is "safe" // See comments above for why this is "safe"
unsafe { &**self.ptr } unsafe { self.ptr.as_ref() }
} }
} }
@ -951,7 +946,7 @@ impl<T: ?Sized> Drop for Weak<T> {
/// assert!(other_weak_foo.upgrade().is_none()); /// assert!(other_weak_foo.upgrade().is_none());
/// ``` /// ```
fn drop(&mut self) { fn drop(&mut self) {
let ptr = *self.ptr; let ptr = self.ptr.as_ptr();
// If we find out that we were the last weak pointer, then its time to // If we find out that we were the last weak pointer, then its time to
// deallocate the data entirely. See the discussion in Arc::drop() about // 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> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> fmt::Pointer for Arc<T> { impl<T: ?Sized> fmt::Pointer for Arc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Pointer::fmt(&*self.ptr, f) fmt::Pointer::fmt(&self.ptr, f)
} }
} }

View file

@ -151,7 +151,7 @@ impl<T> RawVec<T> {
/// heap::EMPTY if `cap = 0` or T is zero-sized. In the former case, you must /// heap::EMPTY if `cap = 0` or T is zero-sized. In the former case, you must
/// be careful. /// be careful.
pub fn ptr(&self) -> *mut T { pub fn ptr(&self) -> *mut T {
*self.ptr self.ptr.ptr()
} }
/// Gets the capacity of the allocation. /// Gets the capacity of the allocation.
@ -563,7 +563,7 @@ unsafe impl<#[may_dangle] T> Drop for RawVec<T> {
let num_bytes = elem_size * self.cap; let num_bytes = elem_size * self.cap;
unsafe { unsafe {
heap::deallocate(*self.ptr as *mut _, num_bytes, align); heap::deallocate(self.ptr() as *mut u8, num_bytes, align);
} }
} }
} }

View file

@ -230,7 +230,7 @@ use core::cell::Cell;
use core::cmp::Ordering; use core::cmp::Ordering;
use core::fmt; use core::fmt;
use core::hash::{Hash, Hasher}; use core::hash::{Hash, Hasher};
use core::intrinsics::{abort, assume}; use core::intrinsics::abort;
use core::marker; use core::marker;
use core::marker::Unsize; use core::marker::Unsize;
use core::mem::{self, align_of_val, forget, size_of, size_of_val, uninitialized}; use core::mem::{self, align_of_val, forget, size_of, size_of_val, uninitialized};
@ -358,7 +358,7 @@ impl<T> Rc<T> {
/// ``` /// ```
#[stable(feature = "rc_raw", since = "1.17.0")] #[stable(feature = "rc_raw", since = "1.17.0")]
pub fn into_raw(this: Self) -> *const T { pub fn into_raw(this: Self) -> *const T {
let ptr = unsafe { &mut (*this.ptr.as_mut_ptr()).value as *const _ }; let ptr: *const T = &*this;
mem::forget(this); mem::forget(this);
ptr ptr
} }
@ -395,7 +395,11 @@ impl<T> Rc<T> {
pub unsafe fn from_raw(ptr: *const T) -> Self { pub unsafe fn from_raw(ptr: *const T) -> Self {
// To find the corresponding pointer to the `RcBox` we need to subtract the offset of the // To find the corresponding pointer to the `RcBox` we need to subtract the offset of the
// `value` field from the pointer. // `value` field from the pointer.
Rc { ptr: Shared::new((ptr as *const u8).offset(-offset_of!(RcBox<T>, value)) as *const _) }
let ptr = (ptr as *const u8).offset(-offset_of!(RcBox<T>, value));
Rc {
ptr: Shared::new(ptr as *mut u8 as *mut _)
}
} }
} }
@ -451,7 +455,7 @@ impl<T> Rc<[T]> {
// Free the original allocation without freeing its (moved) contents. // Free the original allocation without freeing its (moved) contents.
box_free(Box::into_raw(value)); box_free(Box::into_raw(value));
Rc { ptr: Shared::new(ptr as *const _) } Rc { ptr: Shared::new(ptr as *mut _) }
} }
} }
} }
@ -553,8 +557,9 @@ impl<T: ?Sized> Rc<T> {
#[stable(feature = "rc_unique", since = "1.4.0")] #[stable(feature = "rc_unique", since = "1.4.0")]
pub fn get_mut(this: &mut Self) -> Option<&mut T> { pub fn get_mut(this: &mut Self) -> Option<&mut T> {
if Rc::is_unique(this) { if Rc::is_unique(this) {
let inner = unsafe { &mut *this.ptr.as_mut_ptr() }; unsafe {
Some(&mut inner.value) Some(&mut this.ptr.as_mut().value)
}
} else { } else {
None None
} }
@ -578,9 +583,7 @@ impl<T: ?Sized> Rc<T> {
/// assert!(!Rc::ptr_eq(&five, &other_five)); /// assert!(!Rc::ptr_eq(&five, &other_five));
/// ``` /// ```
pub fn ptr_eq(this: &Self, other: &Self) -> bool { pub fn ptr_eq(this: &Self, other: &Self) -> bool {
let this_ptr: *const RcBox<T> = *this.ptr; this.ptr.as_ptr() == other.ptr.as_ptr()
let other_ptr: *const RcBox<T> = *other.ptr;
this_ptr == other_ptr
} }
} }
@ -623,7 +626,7 @@ impl<T: Clone> Rc<T> {
} else if Rc::weak_count(this) != 0 { } else if Rc::weak_count(this) != 0 {
// Can just steal the data, all that's left is Weaks // Can just steal the data, all that's left is Weaks
unsafe { unsafe {
let mut swap = Rc::new(ptr::read(&(**this.ptr).value)); let mut swap = Rc::new(ptr::read(&this.ptr.as_ref().value));
mem::swap(this, &mut swap); mem::swap(this, &mut swap);
swap.dec_strong(); swap.dec_strong();
// Remove implicit strong-weak ref (no need to craft a fake // Remove implicit strong-weak ref (no need to craft a fake
@ -637,8 +640,9 @@ impl<T: Clone> Rc<T> {
// reference count is guaranteed to be 1 at this point, and we required // reference count is guaranteed to be 1 at this point, and we required
// the `Rc<T>` itself to be `mut`, so we're returning the only possible // the `Rc<T>` itself to be `mut`, so we're returning the only possible
// reference to the inner value. // reference to the inner value.
let inner = unsafe { &mut *this.ptr.as_mut_ptr() }; unsafe {
&mut inner.value &mut this.ptr.as_mut().value
}
} }
} }
@ -683,12 +687,12 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
/// ``` /// ```
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
let ptr = self.ptr.as_mut_ptr(); let ptr = self.ptr.as_ptr();
self.dec_strong(); self.dec_strong();
if self.strong() == 0 { if self.strong() == 0 {
// destroy the contained object // destroy the contained object
ptr::drop_in_place(&mut (*ptr).value); ptr::drop_in_place(self.ptr.as_mut());
// remove the implicit "strong weak" pointer now that we've // remove the implicit "strong weak" pointer now that we've
// destroyed the contents. // destroyed the contents.
@ -925,7 +929,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Rc<T> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> fmt::Pointer for Rc<T> { impl<T: ?Sized> fmt::Pointer for Rc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Pointer::fmt(&*self.ptr, f) fmt::Pointer::fmt(&self.ptr, f)
} }
} }
@ -1067,7 +1071,7 @@ impl<T: ?Sized> Drop for Weak<T> {
/// ``` /// ```
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
let ptr = *self.ptr; let ptr = self.ptr.as_ptr();
self.dec_weak(); self.dec_weak();
// the weak count starts at 1, and will only go to zero if all // 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> {
#[inline(always)] #[inline(always)]
fn inner(&self) -> &RcBox<T> { fn inner(&self) -> &RcBox<T> {
unsafe { unsafe {
// Safe to assume this here, as if it weren't true, we'd be breaking self.ptr.as_ref()
// the contract anyway.
// This allows the null check to be elided in the destructor if we
// manipulated the reference count in the same function.
assume(!(*(&self.ptr as *const _ as *const *const ())).is_null());
&(**self.ptr)
} }
} }
} }
@ -1189,12 +1188,7 @@ impl<T: ?Sized> RcBoxPtr<T> for Weak<T> {
#[inline(always)] #[inline(always)]
fn inner(&self) -> &RcBox<T> { fn inner(&self) -> &RcBox<T> {
unsafe { unsafe {
// Safe to assume this here, as if it weren't true, we'd be breaking self.ptr.as_ref()
// the contract anyway.
// This allows the null check to be elided in the destructor if we
// manipulated the reference count in the same function.
assume(!(*(&self.ptr as *const _ as *const *const ())).is_null());
&(**self.ptr)
} }
} }
} }

View file

@ -152,12 +152,12 @@ impl<K, V> BoxedNode<K, V> {
} }
unsafe fn from_ptr(ptr: NonZero<*const LeafNode<K, V>>) -> Self { unsafe fn from_ptr(ptr: NonZero<*const LeafNode<K, V>>) -> Self {
BoxedNode { ptr: Unique::new(*ptr as *mut LeafNode<K, V>) } BoxedNode { ptr: Unique::new(ptr.get() as *mut LeafNode<K, V>) }
} }
fn as_ptr(&self) -> NonZero<*const LeafNode<K, V>> { fn as_ptr(&self) -> NonZero<*const LeafNode<K, V>> {
unsafe { unsafe {
NonZero::new(*self.ptr as *const LeafNode<K, V>) NonZero::new(self.ptr.as_ptr())
} }
} }
} }
@ -241,7 +241,7 @@ impl<K, V> Root<K, V> {
pub fn pop_level(&mut self) { pub fn pop_level(&mut self) {
debug_assert!(self.height > 0); debug_assert!(self.height > 0);
let top = *self.node.ptr as *mut u8; let top = self.node.ptr.as_ptr() as *mut u8;
self.node = unsafe { self.node = unsafe {
BoxedNode::from_ptr(self.as_mut() BoxedNode::from_ptr(self.as_mut()
@ -308,7 +308,7 @@ unsafe impl<K: Send, V: Send, Type> Send
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> { impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
fn as_internal(&self) -> &InternalNode<K, V> { fn as_internal(&self) -> &InternalNode<K, V> {
unsafe { unsafe {
&*(*self.node as *const InternalNode<K, V>) &*(self.node.get() as *const InternalNode<K, V>)
} }
} }
} }
@ -316,7 +316,7 @@ impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> { impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
fn as_internal_mut(&mut self) -> &mut InternalNode<K, V> { fn as_internal_mut(&mut self) -> &mut InternalNode<K, V> {
unsafe { unsafe {
&mut *(*self.node as *mut InternalNode<K, V>) &mut *(self.node.get() as *mut InternalNode<K, V>)
} }
} }
} }
@ -358,7 +358,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
fn as_leaf(&self) -> &LeafNode<K, V> { fn as_leaf(&self) -> &LeafNode<K, V> {
unsafe { unsafe {
&**self.node &*self.node.get()
} }
} }
@ -510,7 +510,7 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
fn as_leaf_mut(&mut self) -> &mut LeafNode<K, V> { fn as_leaf_mut(&mut self) -> &mut LeafNode<K, V> {
unsafe { unsafe {
&mut *(*self.node as *mut LeafNode<K, V>) &mut *(self.node.get() as *mut LeafNode<K, V>)
} }
} }
@ -1253,13 +1253,13 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
} }
heap::deallocate( heap::deallocate(
*right_node.node as *mut u8, right_node.node.get() as *mut u8,
mem::size_of::<InternalNode<K, V>>(), mem::size_of::<InternalNode<K, V>>(),
mem::align_of::<InternalNode<K, V>>() mem::align_of::<InternalNode<K, V>>()
); );
} else { } else {
heap::deallocate( heap::deallocate(
*right_node.node as *mut u8, right_node.node.get() as *mut u8,
mem::size_of::<LeafNode<K, V>>(), mem::size_of::<LeafNode<K, V>>(),
mem::align_of::<LeafNode<K, V>>() mem::align_of::<LeafNode<K, V>>()
); );

View file

@ -161,7 +161,7 @@ impl<T> LinkedList<T> {
match self.head { match self.head {
None => self.tail = node, None => self.tail = node,
Some(head) => (*head.as_mut_ptr()).prev = node, Some(mut head) => head.as_mut().prev = node,
} }
self.head = node; self.head = node;
@ -173,12 +173,12 @@ impl<T> LinkedList<T> {
#[inline] #[inline]
fn pop_front_node(&mut self) -> Option<Box<Node<T>>> { fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
self.head.map(|node| unsafe { self.head.map(|node| unsafe {
let node = Box::from_raw(node.as_mut_ptr()); let node = Box::from_raw(node.as_ptr());
self.head = node.next; self.head = node.next;
match self.head { match self.head {
None => self.tail = None, None => self.tail = None,
Some(head) => (*head.as_mut_ptr()).prev = None, Some(mut head) => head.as_mut().prev = None,
} }
self.len -= 1; self.len -= 1;
@ -196,7 +196,7 @@ impl<T> LinkedList<T> {
match self.tail { match self.tail {
None => self.head = node, None => self.head = node,
Some(tail) => (*tail.as_mut_ptr()).next = node, Some(mut tail) => tail.as_mut().next = node,
} }
self.tail = node; self.tail = node;
@ -208,12 +208,12 @@ impl<T> LinkedList<T> {
#[inline] #[inline]
fn pop_back_node(&mut self) -> Option<Box<Node<T>>> { fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
self.tail.map(|node| unsafe { self.tail.map(|node| unsafe {
let node = Box::from_raw(node.as_mut_ptr()); let node = Box::from_raw(node.as_ptr());
self.tail = node.prev; self.tail = node.prev;
match self.tail { match self.tail {
None => self.head = None, None => self.head = None,
Some(tail) => (*tail.as_mut_ptr()).next = None, Some(mut tail) => tail.as_mut().next = None,
} }
self.len -= 1; self.len -= 1;
@ -285,11 +285,11 @@ impl<T> LinkedList<T> {
pub fn append(&mut self, other: &mut Self) { pub fn append(&mut self, other: &mut Self) {
match self.tail { match self.tail {
None => mem::swap(self, other), None => mem::swap(self, other),
Some(tail) => { Some(mut tail) => {
if let Some(other_head) = other.head.take() { if let Some(mut other_head) = other.head.take() {
unsafe { unsafe {
(*tail.as_mut_ptr()).next = Some(other_head); tail.as_mut().next = Some(other_head);
(*other_head.as_mut_ptr()).prev = Some(tail); other_head.as_mut().prev = Some(tail);
} }
self.tail = other.tail.take(); self.tail = other.tail.take();
@ -477,7 +477,9 @@ impl<T> LinkedList<T> {
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn front(&self) -> Option<&T> { pub fn front(&self) -> Option<&T> {
self.head.map(|node| unsafe { &(**node).element }) unsafe {
self.head.as_ref().map(|node| &node.as_ref().element)
}
} }
/// Provides a mutable reference to the front element, or `None` if the list /// Provides a mutable reference to the front element, or `None` if the list
@ -503,7 +505,9 @@ impl<T> LinkedList<T> {
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn front_mut(&mut self) -> Option<&mut T> { pub fn front_mut(&mut self) -> Option<&mut T> {
self.head.map(|node| unsafe { &mut (*node.as_mut_ptr()).element }) unsafe {
self.head.as_mut().map(|node| &mut node.as_mut().element)
}
} }
/// Provides a reference to the back element, or `None` if the list is /// Provides a reference to the back element, or `None` if the list is
@ -523,7 +527,9 @@ impl<T> LinkedList<T> {
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn back(&self) -> Option<&T> { pub fn back(&self) -> Option<&T> {
self.tail.map(|node| unsafe { &(**node).element }) unsafe {
self.tail.as_ref().map(|node| &node.as_ref().element)
}
} }
/// Provides a mutable reference to the back element, or `None` if the list /// Provides a mutable reference to the back element, or `None` if the list
@ -549,7 +555,9 @@ impl<T> LinkedList<T> {
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn back_mut(&mut self) -> Option<&mut T> { pub fn back_mut(&mut self) -> Option<&mut T> {
self.tail.map(|node| unsafe { &mut (*node.as_mut_ptr()).element }) unsafe {
self.tail.as_mut().map(|node| &mut node.as_mut().element)
}
} }
/// Adds an element first in the list. /// Adds an element first in the list.
@ -694,9 +702,9 @@ impl<T> LinkedList<T> {
let second_part_head; let second_part_head;
unsafe { unsafe {
second_part_head = (*split_node.unwrap().as_mut_ptr()).next.take(); second_part_head = split_node.unwrap().as_mut().next.take();
if let Some(head) = second_part_head { if let Some(mut head) = second_part_head {
(*head.as_mut_ptr()).prev = None; head.as_mut().prev = None;
} }
} }
@ -788,7 +796,8 @@ impl<'a, T> Iterator for Iter<'a, T> {
None None
} else { } else {
self.head.map(|node| unsafe { self.head.map(|node| unsafe {
let node = &**node; // Need an unbound lifetime to get 'a
let node = &*node.as_ptr();
self.len -= 1; self.len -= 1;
self.head = node.next; self.head = node.next;
&node.element &node.element
@ -810,7 +819,8 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
None None
} else { } else {
self.tail.map(|node| unsafe { self.tail.map(|node| unsafe {
let node = &**node; // Need an unbound lifetime to get 'a
let node = &*node.as_ptr();
self.len -= 1; self.len -= 1;
self.tail = node.prev; self.tail = node.prev;
&node.element &node.element
@ -835,7 +845,8 @@ impl<'a, T> Iterator for IterMut<'a, T> {
None None
} else { } else {
self.head.map(|node| unsafe { self.head.map(|node| unsafe {
let node = &mut *node.as_mut_ptr(); // Need an unbound lifetime to get 'a
let node = &mut *node.as_ptr();
self.len -= 1; self.len -= 1;
self.head = node.next; self.head = node.next;
&mut node.element &mut node.element
@ -857,7 +868,8 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
None None
} else { } else {
self.tail.map(|node| unsafe { self.tail.map(|node| unsafe {
let node = &mut *node.as_mut_ptr(); // Need an unbound lifetime to get 'a
let node = &mut *node.as_ptr();
self.len -= 1; self.len -= 1;
self.tail = node.prev; self.tail = node.prev;
&mut node.element &mut node.element
@ -903,8 +915,8 @@ impl<'a, T> IterMut<'a, T> {
pub fn insert_next(&mut self, element: T) { pub fn insert_next(&mut self, element: T) {
match self.head { match self.head {
None => self.list.push_back(element), None => self.list.push_back(element),
Some(head) => unsafe { Some(mut head) => unsafe {
let prev = match (**head).prev { let mut prev = match head.as_ref().prev {
None => return self.list.push_front(element), None => return self.list.push_front(element),
Some(prev) => prev, Some(prev) => prev,
}; };
@ -915,8 +927,8 @@ impl<'a, T> IterMut<'a, T> {
element: element, element: element,
}))); })));
(*prev.as_mut_ptr()).next = node; prev.as_mut().next = node;
(*head.as_mut_ptr()).prev = node; head.as_mut().prev = node;
self.list.len += 1; self.list.len += 1;
}, },
@ -948,7 +960,9 @@ impl<'a, T> IterMut<'a, T> {
if self.len == 0 { if self.len == 0 {
None None
} else { } else {
self.head.map(|node| unsafe { &mut (*node.as_mut_ptr()).element }) unsafe {
self.head.as_mut().map(|node| &mut node.as_mut().element)
}
} }
} }
} }
@ -1276,21 +1290,21 @@ mod tests {
assert_eq!(0, list.len); assert_eq!(0, list.len);
return; return;
} }
Some(node) => node_ptr = &**node, Some(node) => node_ptr = &*node.as_ptr(),
} }
loop { loop {
match (last_ptr, node_ptr.prev) { match (last_ptr, node_ptr.prev) {
(None, None) => {} (None, None) => {}
(None, _) => panic!("prev link for head"), (None, _) => panic!("prev link for head"),
(Some(p), Some(pptr)) => { (Some(p), Some(pptr)) => {
assert_eq!(p as *const Node<T>, *pptr as *const Node<T>); assert_eq!(p as *const Node<T>, pptr.as_ptr() as *const Node<T>);
} }
_ => panic!("prev link is none, not good"), _ => panic!("prev link is none, not good"),
} }
match node_ptr.next { match node_ptr.next {
Some(next) => { Some(next) => {
last_ptr = Some(node_ptr); last_ptr = Some(node_ptr);
node_ptr = &**next; node_ptr = &*next.as_ptr();
len += 1; len += 1;
} }
None => { None => {

View file

@ -1776,9 +1776,9 @@ impl<T> SpecExtend<T, IntoIter<T>> for Vec<T> {
// A common case is passing a vector into a function which immediately // A common case is passing a vector into a function which immediately
// re-collects into a vector. We can short circuit this if the IntoIter // re-collects into a vector. We can short circuit this if the IntoIter
// has not been advanced at all. // has not been advanced at all.
if *iterator.buf == iterator.ptr as *mut T { if iterator.buf.as_ptr() as *const _ == iterator.ptr {
unsafe { unsafe {
let vec = Vec::from_raw_parts(*iterator.buf as *mut T, let vec = Vec::from_raw_parts(iterator.buf.as_ptr(),
iterator.len(), iterator.len(),
iterator.cap); iterator.cap);
mem::forget(iterator); mem::forget(iterator);
@ -2269,7 +2269,7 @@ unsafe impl<#[may_dangle] T> Drop for IntoIter<T> {
for _x in self.by_ref() {} for _x in self.by_ref() {}
// RawVec handles deallocation // RawVec handles deallocation
let _ = unsafe { RawVec::from_raw_parts(self.buf.as_mut_ptr(), self.cap) }; let _ = unsafe { RawVec::from_raw_parts(self.buf.as_ptr(), self.cap) };
} }
} }
@ -2334,7 +2334,7 @@ impl<'a, T> Drop for Drain<'a, T> {
if self.tail_len > 0 { if self.tail_len > 0 {
unsafe { unsafe {
let source_vec = &mut *self.vec.as_mut_ptr(); let source_vec = self.vec.as_mut();
// memmove back untouched tail, update to new length // memmove back untouched tail, update to new length
let start = source_vec.len(); let start = source_vec.len();
let tail = self.tail_start; let tail = self.tail_start;
@ -2456,8 +2456,7 @@ impl<'a, I: Iterator> Drop for Splice<'a, I> {
unsafe { unsafe {
if self.drain.tail_len == 0 { if self.drain.tail_len == 0 {
let vec = &mut *self.drain.vec.as_mut_ptr(); self.drain.vec.as_mut().extend(self.replace_with.by_ref());
vec.extend(self.replace_with.by_ref());
return return
} }
@ -2498,7 +2497,7 @@ impl<'a, T> Drain<'a, T> {
/// Fill that range as much as possible with new elements from the `replace_with` iterator. /// Fill that range as much as possible with new elements from the `replace_with` iterator.
/// Return whether we filled the entire range. (`replace_with.next()` didnt return `None`.) /// Return whether we filled the entire range. (`replace_with.next()` didnt return `None`.)
unsafe fn fill<I: Iterator<Item=T>>(&mut self, replace_with: &mut I) -> bool { unsafe fn fill<I: Iterator<Item=T>>(&mut self, replace_with: &mut I) -> bool {
let vec = &mut *self.vec.as_mut_ptr(); let vec = self.vec.as_mut();
let range_start = vec.len; let range_start = vec.len;
let range_end = self.tail_start; let range_end = self.tail_start;
let range_slice = slice::from_raw_parts_mut( let range_slice = slice::from_raw_parts_mut(
@ -2518,7 +2517,7 @@ impl<'a, T> Drain<'a, T> {
/// Make room for inserting more elements before the tail. /// Make room for inserting more elements before the tail.
unsafe fn move_tail(&mut self, extra_capacity: usize) { unsafe fn move_tail(&mut self, extra_capacity: usize) {
let vec = &mut *self.vec.as_mut_ptr(); let vec = self.vec.as_mut();
let used_capacity = self.tail_start + self.tail_len; let used_capacity = self.tail_start + self.tail_len;
vec.buf.reserve(used_capacity, extra_capacity); vec.buf.reserve(used_capacity, extra_capacity);

View file

@ -2160,7 +2160,7 @@ impl<'a, T: 'a> Drop for Drain<'a, T> {
fn drop(&mut self) { fn drop(&mut self) {
for _ in self.by_ref() {} for _ in self.by_ref() {}
let source_deque = unsafe { &mut *self.deque.as_mut_ptr() }; let source_deque = unsafe { self.deque.as_mut() };
// T = source_deque_tail; H = source_deque_head; t = drain_tail; h = drain_head // T = source_deque_tail; H = source_deque_head; t = drain_tail; h = drain_head
// //

View file

@ -132,7 +132,6 @@
//! use std::cell::Cell; //! use std::cell::Cell;
//! use std::ptr::Shared; //! use std::ptr::Shared;
//! use std::intrinsics::abort; //! use std::intrinsics::abort;
//! use std::intrinsics::assume;
//! //!
//! struct Rc<T: ?Sized> { //! struct Rc<T: ?Sized> {
//! ptr: Shared<RcBox<T>> //! ptr: Shared<RcBox<T>>
@ -171,8 +170,7 @@
//! impl<T: ?Sized> RcBoxPtr<T> for Rc<T> { //! impl<T: ?Sized> RcBoxPtr<T> for Rc<T> {
//! fn inner(&self) -> &RcBox<T> { //! fn inner(&self) -> &RcBox<T> {
//! unsafe { //! unsafe {
//! assume(!(*(&self.ptr as *const _ as *const *const ())).is_null()); //! self.ptr.as_ref()
//! &(**self.ptr)
//! } //! }
//! } //! }
//! } //! }

View file

@ -31,12 +31,12 @@ fn test_match_on_nonzero_option() {
NonZero::new(42) NonZero::new(42)
}); });
match a { match a {
Some(val) => assert_eq!(*val, 42), Some(val) => assert_eq!(val.get(), 42),
None => panic!("unexpected None while matching on Some(NonZero(_))") None => panic!("unexpected None while matching on Some(NonZero(_))")
} }
match unsafe { Some(NonZero::new(43)) } { match unsafe { Some(NonZero::new(43)) } {
Some(val) => assert_eq!(*val, 43), Some(val) => assert_eq!(val.get(), 43),
None => panic!("unexpected None while matching on Some(NonZero(_))") None => panic!("unexpected None while matching on Some(NonZero(_))")
} }
} }

View file

@ -166,10 +166,10 @@ fn test_set_memory() {
#[test] #[test]
fn test_unsized_unique() { fn test_unsized_unique() {
let xs: &mut [i32] = &mut [1, 2, 3]; let xs: &[i32] = &[1, 2, 3];
let ptr = unsafe { Unique::new(xs as *mut [i32]) }; let ptr = unsafe { Unique::new(xs as *const [i32] as *mut [i32]) };
let ys = unsafe { &mut **ptr }; let ys = unsafe { ptr.as_ref() };
let zs: &mut [i32] = &mut [1, 2, 3]; let zs: &[i32] = &[1, 2, 3];
assert!(ys == zs); assert!(ys == zs);
} }

View file

@ -62,14 +62,14 @@ pub struct Bytes {
impl Deref for Bytes { impl Deref for Bytes {
type Target = [u8]; type Target = [u8];
fn deref(&self) -> &[u8] { fn deref(&self) -> &[u8] {
unsafe { slice::from_raw_parts(*self.ptr, self.len) } unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
} }
} }
impl Drop for Bytes { impl Drop for Bytes {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
libc::free(*self.ptr as *mut _); libc::free(self.ptr.as_ptr() as *mut _);
} }
} }
} }

View file

@ -72,7 +72,7 @@ impl<'tcx> From<ty::Region<'tcx>> for Kind<'tcx> {
impl<'tcx> Kind<'tcx> { impl<'tcx> Kind<'tcx> {
#[inline] #[inline]
unsafe fn downcast<T>(self, tag: usize) -> Option<&'tcx T> { unsafe fn downcast<T>(self, tag: usize) -> Option<&'tcx T> {
let ptr = *self.ptr; let ptr = self.ptr.get();
if ptr & TAG_MASK == tag { if ptr & TAG_MASK == tag {
Some(&*((ptr & !TAG_MASK) as *const _)) Some(&*((ptr & !TAG_MASK) as *const _))
} else { } else {
@ -102,7 +102,7 @@ impl<'tcx> fmt::Debug for Kind<'tcx> {
} else if let Some(r) = self.as_region() { } else if let Some(r) = self.as_region() {
write!(f, "{:?}", r) write!(f, "{:?}", r)
} else { } else {
write!(f, "<unknwon @ {:p}>", *self.ptr as *const ()) write!(f, "<unknwon @ {:p}>", self.ptr.get() as *const ())
} }
} }
} }

View file

@ -43,7 +43,7 @@ mod indexes {
unsafe { $Index(NonZero::new(idx + 1)) } unsafe { $Index(NonZero::new(idx + 1)) }
} }
fn index(self) -> usize { fn index(self) -> usize {
*self.0 - 1 self.0.get() - 1
} }
} }

View file

@ -255,7 +255,7 @@ impl<'a, A: Array> Drop for Drain<'a, A> {
if self.tail_len > 0 { if self.tail_len > 0 {
unsafe { unsafe {
let source_array_vec = &mut *self.array_vec.as_mut_ptr(); let source_array_vec = self.array_vec.as_mut();
// memmove back untouched tail, update to new length // memmove back untouched tail, update to new length
let start = source_array_vec.len(); let start = source_array_vec.len();
let tail = self.tail_start; let tail = self.tail_start;

View file

@ -23,6 +23,6 @@ impl NodeIndex {
} }
pub fn get(self) -> usize { pub fn get(self) -> usize {
(*self.index - 1) as usize (self.index.get() - 1) as usize
} }
} }

View file

@ -49,24 +49,25 @@ impl TaggedHashUintPtr {
#[inline] #[inline]
fn set_tag(&mut self, value: bool) { fn set_tag(&mut self, value: bool) {
let usize_ptr = &*self.0 as *const *mut HashUint as *mut usize; let mut usize_ptr = self.0.as_ptr() as usize;
unsafe { unsafe {
if value { if value {
*usize_ptr |= 1; usize_ptr |= 1;
} else { } else {
*usize_ptr &= !1; usize_ptr &= !1;
} }
self.0 = Unique::new(usize_ptr as *mut HashUint)
} }
} }
#[inline] #[inline]
fn tag(&self) -> bool { fn tag(&self) -> bool {
(*self.0 as usize) & 1 == 1 (self.0.as_ptr() as usize) & 1 == 1
} }
#[inline] #[inline]
fn ptr(&self) -> *mut HashUint { fn ptr(&self) -> *mut HashUint {
(*self.0 as usize & !1) as *mut HashUint (self.0.as_ptr() as usize & !1) as *mut HashUint
} }
} }
@ -1112,10 +1113,12 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> {
#[inline] #[inline]
fn next(&mut self) -> Option<(SafeHash, K, V)> { fn next(&mut self) -> Option<(SafeHash, K, V)> {
self.iter.next().map(|raw| unsafe { self.iter.next().map(|raw| {
(*self.table.as_mut_ptr()).size -= 1; unsafe {
let (k, v) = ptr::read(raw.pair()); self.table.as_mut().size -= 1;
(SafeHash { hash: ptr::replace(&mut *raw.hash(), EMPTY_BUCKET) }, k, v) let (k, v) = ptr::read(raw.pair());
(SafeHash { hash: ptr::replace(&mut *raw.hash(), EMPTY_BUCKET) }, k, v)
}
}) })
} }