Skip to content

Commit 21b5950

Browse files
committed
Unique/NonNull::from: make sure we convert to raw pointers ASAP
By going through a shared reference, we share the destination as read-only, meaning we can read but not write with the raw pointers
1 parent a2fb99b commit 21b5950

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/libcore/ptr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2815,14 +2815,14 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
28152815
#[unstable(feature = "ptr_internals", issue = "0")]
28162816
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
28172817
fn from(reference: &'a mut T) -> Self {
2818-
Unique { pointer: unsafe { NonZero(reference as _) }, _marker: PhantomData }
2818+
Unique { pointer: unsafe { NonZero(reference as *mut T) }, _marker: PhantomData }
28192819
}
28202820
}
28212821

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

@@ -3025,14 +3025,14 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
30253025
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
30263026
#[inline]
30273027
fn from(reference: &'a mut T) -> Self {
3028-
NonNull { pointer: unsafe { NonZero(reference as _) } }
3028+
NonNull { pointer: unsafe { NonZero(reference as *mut T) } }
30293029
}
30303030
}
30313031

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

0 commit comments

Comments
 (0)