@@ -762,6 +762,17 @@ pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T {
762762/// let slice = ptr::slice_from_raw_parts(raw_pointer, 3);
763763/// assert_eq!(unsafe { &*slice }[2], 7);
764764/// ```
765+ ///
766+ /// You must ensure that the pointer is valid and not null before dereferencing
767+ /// the raw slice. A slice reference must never have a null pointer, even if it's empty.
768+ ///
769+ /// ```rust,should_panic
770+ /// use std::ptr;
771+ /// let danger: *const [u8] = ptr::slice_from_raw_parts(ptr::null(), 0);
772+ /// unsafe {
773+ /// danger.as_ref().expect("references must not be null");
774+ /// }
775+ /// ```
765776#[ inline]
766777#[ stable( feature = "slice_from_raw_parts" , since = "1.42.0" ) ]
767778#[ rustc_const_stable( feature = "const_slice_from_raw_parts" , since = "1.64.0" ) ]
@@ -771,11 +782,13 @@ pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
771782 from_raw_parts ( data. cast ( ) , len)
772783}
773784
785+ /// Forms a raw mutable slice from a pointer and a length.
786+ ///
787+ /// The `len` argument is the number of **elements**, not the number of bytes.
788+ ///
774789/// Performs the same functionality as [`slice_from_raw_parts`], except that a
775790/// raw mutable slice is returned, as opposed to a raw immutable slice.
776791///
777- /// See the documentation of [`slice_from_raw_parts`] for more details.
778- ///
779792/// This function is safe, but actually using the return value is unsafe.
780793/// See the documentation of [`slice::from_raw_parts_mut`] for slice safety requirements.
781794///
@@ -796,6 +809,17 @@ pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
796809///
797810/// assert_eq!(unsafe { &*slice }[2], 99);
798811/// ```
812+ ///
813+ /// You must ensure that the pointer is valid and not null before dereferencing
814+ /// the raw slice. A slice reference must never have a null pointer, even if it's empty.
815+ ///
816+ /// ```rust,should_panic
817+ /// use std::ptr;
818+ /// let danger: *mut [u8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 0);
819+ /// unsafe {
820+ /// danger.as_mut().expect("references must not be null");
821+ /// }
822+ /// ```
799823#[ inline]
800824#[ stable( feature = "slice_from_raw_parts" , since = "1.42.0" ) ]
801825#[ rustc_const_unstable( feature = "const_slice_from_raw_parts_mut" , issue = "67456" ) ]
0 commit comments