diff --git a/src/libcore/ptr/non_null.rs b/src/libcore/ptr/non_null.rs index 626e58d49306e..48c84e3a2ff12 100644 --- a/src/libcore/ptr/non_null.rs +++ b/src/libcore/ptr/non_null.rs @@ -27,8 +27,8 @@ use crate::ptr::Unique; /// such as `Box`, `Rc`, `Arc`, `Vec`, and `LinkedList`. This is the case because they /// provide a public API that follows the normal shared XOR mutable rules of Rust. /// -/// Notice that `NonNull` has a `From` instance for `&T`. However, this does -/// not change the fact that mutating through a (pointer derived from a) shared +/// Notice that `NonNull` has `From` instances for `&T` and `&[T]`. However, this +/// does not change the fact that mutating through a (pointer derived from a) shared /// reference is undefined behavior unless the mutation happens inside an /// [`UnsafeCell`]. The same goes for creating a mutable reference from a shared /// reference. When using this `From` instance without an `UnsafeCell`, @@ -224,3 +224,19 @@ impl From<&T> for NonNull { unsafe { NonNull { pointer: reference as *const T } } } } + +#[stable(feature = "nonnull_from_slice", since = "1.43.0")] +impl From<&mut [T]> for NonNull { + #[inline] + fn from(slice: &mut [T]) -> Self { + unsafe { NonNull { pointer: slice.as_mut_ptr() } } + } +} + +#[stable(feature = "nonnull_from_slice", since = "1.43.0")] +impl From<&[T]> for NonNull { + #[inline] + fn from(slice: &[T]) -> Self { + unsafe { NonNull { pointer: slice.as_ptr() } } + } +}