Skip to content

Commit

Permalink
extend from_raw_parts docs for slices and strs to mention alignment r…
Browse files Browse the repository at this point in the history
…equirement
  • Loading branch information
RalfJung committed May 28, 2018
1 parent 2b1d69d commit 3ee9d89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/libcore/slice/mod.rs
Expand Up @@ -3839,10 +3839,9 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
/// valid for `len` elements, nor whether the lifetime inferred is a suitable
/// lifetime for the returned slice.
///
/// `p` must be non-null, even for zero-length slices, because non-zero bits
/// are required to distinguish between a zero-length slice within `Some()`
/// from `None`. `p` can be a bogus non-dereferencable pointer, such as `0x1`,
/// for zero-length slices, though.
/// `p` must be non-null and aligned, even for zero-length slices, as is
/// required for all references. However, for zero-length slices, `p` can be
/// a bogus non-dereferencable pointer such as [`NonNull::dangling()`].
///
/// # Caveat
///
Expand All @@ -3864,6 +3863,8 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
/// let slice = slice::from_raw_parts(ptr, amt);
/// }
/// ```
///
/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
Expand All @@ -3875,7 +3876,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
///
/// This function is unsafe for the same reasons as `from_raw_parts`, as well
/// as not being able to provide a non-aliasing guarantee of the returned
/// mutable slice. `p` must be non-null even for zero-length slices as with
/// mutable slice. `p` must be non-null and aligned even for zero-length slices as with
/// `from_raw_parts`.
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
10 changes: 4 additions & 6 deletions src/libcore/str/mod.rs
Expand Up @@ -388,10 +388,9 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
///
/// The data must be valid UTF-8
///
/// `p` must be non-null, even for zero-length strs, because non-zero bits
/// are required to distinguish between a zero-length str within `Some()`
/// from `None`. `p` can be a bogus non-dereferencable pointer, such as `0x1`,
/// for zero-length strs, though.
/// `p` must be non-null and aligned, even for zero-length strs, as is
/// required for all references. However, for zero-length strs, `p` can be
/// a bogus non-dereferencable pointer such as [`NonNull::dangling()`].
///
/// # Caveat
///
Expand All @@ -400,9 +399,8 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
/// source lifetime is safe in the context, such as by providing a helper
/// function taking the lifetime of a host value for the str, or by explicit
/// annotation.
/// Performs the same functionality as `from_raw_parts`, except that a mutable
/// str is returned.
///
/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
unsafe fn from_raw_parts_mut<'a>(p: *mut u8, len: usize) -> &'a mut str {
from_utf8_unchecked_mut(slice::from_raw_parts_mut(p, len))
}
Expand Down

0 comments on commit 3ee9d89

Please sign in to comment.