@@ -762,6 +762,17 @@ pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T {
762
762
/// let slice = ptr::slice_from_raw_parts(raw_pointer, 3);
763
763
/// assert_eq!(unsafe { &*slice }[2], 7);
764
764
/// ```
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
+ /// ```
765
776
#[ inline]
766
777
#[ stable( feature = "slice_from_raw_parts" , since = "1.42.0" ) ]
767
778
#[ 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] {
771
782
from_raw_parts ( data. cast ( ) , len)
772
783
}
773
784
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
+ ///
774
789
/// Performs the same functionality as [`slice_from_raw_parts`], except that a
775
790
/// raw mutable slice is returned, as opposed to a raw immutable slice.
776
791
///
777
- /// See the documentation of [`slice_from_raw_parts`] for more details.
778
- ///
779
792
/// This function is safe, but actually using the return value is unsafe.
780
793
/// See the documentation of [`slice::from_raw_parts_mut`] for slice safety requirements.
781
794
///
@@ -796,6 +809,17 @@ pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
796
809
///
797
810
/// assert_eq!(unsafe { &*slice }[2], 99);
798
811
/// ```
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
+ /// ```
799
823
#[ inline]
800
824
#[ stable( feature = "slice_from_raw_parts" , since = "1.42.0" ) ]
801
825
#[ rustc_const_unstable( feature = "const_slice_from_raw_parts_mut" , issue = "67456" ) ]
0 commit comments