Skip to content

Commit a32e979

Browse files
committed
Auto merge of #51423 - RalfJung:from_raw_parts, r=sfackler
Improve docs for slice::from_raw_parts Explain why the pointer must be non-null and aligned. Cc @hsivonen
2 parents 7dc61f3 + 426f06f commit a32e979

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/libcore/slice/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3873,9 +3873,11 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
38733873
/// valid for `len` elements, nor whether the lifetime inferred is a suitable
38743874
/// lifetime for the returned slice.
38753875
///
3876-
/// `p` must be non-null and aligned, even for zero-length slices, as is
3877-
/// required for all references. However, for zero-length slices, `p` can be
3878-
/// a bogus non-dereferencable pointer such as [`NonNull::dangling()`].
3876+
/// `data` must be non-null and aligned, even for zero-length slices. One
3877+
/// reason for this is that enum layout optimizations may rely on references
3878+
/// (including slices of any length) being aligned and non-null to distinguish
3879+
/// them from other data. You can obtain a pointer that is usable as `data`
3880+
/// for zero-length slices using [`NonNull::dangling()`].
38793881
///
38803882
/// # Caveat
38813883
///
@@ -3910,8 +3912,8 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
39103912
///
39113913
/// This function is unsafe for the same reasons as `from_raw_parts`, as well
39123914
/// as not being able to provide a non-aliasing guarantee of the returned
3913-
/// mutable slice. `p` must be non-null and aligned even for zero-length slices as with
3914-
/// `from_raw_parts`.
3915+
/// mutable slice. `data` must be non-null and aligned even for zero-length
3916+
/// slices as with `from_raw_parts`.
39153917
#[inline]
39163918
#[stable(feature = "rust1", since = "1.0.0")]
39173919
pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {

0 commit comments

Comments
 (0)