Skip to content

Commit 3ee9d89

Browse files
committed
extend from_raw_parts docs for slices and strs to mention alignment requirement
1 parent 2b1d69d commit 3ee9d89

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/libcore/slice/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -3839,10 +3839,9 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
38393839
/// valid for `len` elements, nor whether the lifetime inferred is a suitable
38403840
/// lifetime for the returned slice.
38413841
///
3842-
/// `p` must be non-null, even for zero-length slices, because non-zero bits
3843-
/// are required to distinguish between a zero-length slice within `Some()`
3844-
/// from `None`. `p` can be a bogus non-dereferencable pointer, such as `0x1`,
3845-
/// for zero-length slices, though.
3842+
/// `p` must be non-null and aligned, even for zero-length slices, as is
3843+
/// required for all references. However, for zero-length slices, `p` can be
3844+
/// a bogus non-dereferencable pointer such as [`NonNull::dangling()`].
38463845
///
38473846
/// # Caveat
38483847
///
@@ -3864,6 +3863,8 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
38643863
/// let slice = slice::from_raw_parts(ptr, amt);
38653864
/// }
38663865
/// ```
3866+
///
3867+
/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
38673868
#[inline]
38683869
#[stable(feature = "rust1", since = "1.0.0")]
38693870
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
@@ -3875,7 +3876,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
38753876
///
38763877
/// This function is unsafe for the same reasons as `from_raw_parts`, as well
38773878
/// as not being able to provide a non-aliasing guarantee of the returned
3878-
/// mutable slice. `p` must be non-null even for zero-length slices as with
3879+
/// mutable slice. `p` must be non-null and aligned even for zero-length slices as with
38793880
/// `from_raw_parts`.
38803881
#[inline]
38813882
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/str/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,9 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
388388
///
389389
/// The data must be valid UTF-8
390390
///
391-
/// `p` must be non-null, even for zero-length strs, because non-zero bits
392-
/// are required to distinguish between a zero-length str within `Some()`
393-
/// from `None`. `p` can be a bogus non-dereferencable pointer, such as `0x1`,
394-
/// for zero-length strs, though.
391+
/// `p` must be non-null and aligned, even for zero-length strs, as is
392+
/// required for all references. However, for zero-length strs, `p` can be
393+
/// a bogus non-dereferencable pointer such as [`NonNull::dangling()`].
395394
///
396395
/// # Caveat
397396
///
@@ -400,9 +399,8 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
400399
/// source lifetime is safe in the context, such as by providing a helper
401400
/// function taking the lifetime of a host value for the str, or by explicit
402401
/// annotation.
403-
/// Performs the same functionality as `from_raw_parts`, except that a mutable
404-
/// str is returned.
405402
///
403+
/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
406404
unsafe fn from_raw_parts_mut<'a>(p: *mut u8, len: usize) -> &'a mut str {
407405
from_utf8_unchecked_mut(slice::from_raw_parts_mut(p, len))
408406
}

0 commit comments

Comments
 (0)