Skip to content

Commit

Permalink
Unique/NonNull::from: make sure we convert to raw pointers ASAP
Browse files Browse the repository at this point in the history
By going through a shared reference, we share the destination as read-only, meaning we can read but not write with the raw pointers
  • Loading branch information
RalfJung committed Dec 7, 2018
1 parent a2fb99b commit 21b5950
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2815,14 +2815,14 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
#[unstable(feature = "ptr_internals", issue = "0")]
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
fn from(reference: &'a mut T) -> Self {
Unique { pointer: unsafe { NonZero(reference as _) }, _marker: PhantomData }
Unique { pointer: unsafe { NonZero(reference as *mut T) }, _marker: PhantomData }
}
}

#[unstable(feature = "ptr_internals", issue = "0")]
impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
fn from(reference: &'a T) -> Self {
Unique { pointer: unsafe { NonZero(reference as _) }, _marker: PhantomData }
Unique { pointer: unsafe { NonZero(reference as *const T) }, _marker: PhantomData }
}
}

Expand Down Expand Up @@ -3025,14 +3025,14 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
#[inline]
fn from(reference: &'a mut T) -> Self {
NonNull { pointer: unsafe { NonZero(reference as _) } }
NonNull { pointer: unsafe { NonZero(reference as *mut T) } }
}
}

#[stable(feature = "nonnull", since = "1.25.0")]
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
#[inline]
fn from(reference: &'a T) -> Self {
NonNull { pointer: unsafe { NonZero(reference as _) } }
NonNull { pointer: unsafe { NonZero(reference as *const T) } }
}
}

0 comments on commit 21b5950

Please sign in to comment.