mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-02 11:44:28 +00:00
Auto merge of #51134 - RalfJung:from_raw_parts, r=SimonSapin
extend from_raw_parts docs for slices and strs to mention alignment requirement The documentation for `str::from_raw_parts_mut` seems to not be visible because that method is private, bit I figured it could still be fixed. I also removed the reference to the no-longer-existing `str::from_raw_parts` while I was at it. Alternatively, should I remove `str::from_raw_parts_mut` completely? it is only used in `str::split_at_mut`, where it might as well be inlined.
This commit is contained in:
commit
889d8dcaa7
@ -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
|
/// valid for `len` elements, nor whether the lifetime inferred is a suitable
|
||||||
/// lifetime for the returned slice.
|
/// lifetime for the returned slice.
|
||||||
///
|
///
|
||||||
/// `p` must be non-null, even for zero-length slices, because non-zero bits
|
/// `p` must be non-null and aligned, even for zero-length slices, as is
|
||||||
/// are required to distinguish between a zero-length slice within `Some()`
|
/// required for all references. However, for zero-length slices, `p` can be
|
||||||
/// from `None`. `p` can be a bogus non-dereferencable pointer, such as `0x1`,
|
/// a bogus non-dereferencable pointer such as [`NonNull::dangling()`].
|
||||||
/// for zero-length slices, though.
|
|
||||||
///
|
///
|
||||||
/// # Caveat
|
/// # Caveat
|
||||||
///
|
///
|
||||||
@ -3864,6 +3863,8 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
|
|||||||
/// let slice = slice::from_raw_parts(ptr, amt);
|
/// let slice = slice::from_raw_parts(ptr, amt);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
|
||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
|
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] {
|
|||||||
///
|
///
|
||||||
/// This function is unsafe for the same reasons as `from_raw_parts`, as well
|
/// 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
|
/// 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`.
|
/// `from_raw_parts`.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
@ -376,37 +376,6 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
|
|||||||
Ok(unsafe { from_utf8_unchecked_mut(v) })
|
Ok(unsafe { from_utf8_unchecked_mut(v) })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Forms a str from a pointer and a length.
|
|
||||||
///
|
|
||||||
/// The `len` argument is the number of bytes in the string.
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// This function is unsafe as there is no guarantee that the given pointer is
|
|
||||||
/// valid for `len` bytes, nor whether the lifetime inferred is a suitable
|
|
||||||
/// lifetime for the returned str.
|
|
||||||
///
|
|
||||||
/// 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.
|
|
||||||
///
|
|
||||||
/// # Caveat
|
|
||||||
///
|
|
||||||
/// The lifetime for the returned str is inferred from its usage. To
|
|
||||||
/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
|
|
||||||
/// 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.
|
|
||||||
///
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Converts a slice of bytes to a string slice without checking
|
/// Converts a slice of bytes to a string slice without checking
|
||||||
/// that the string contains valid UTF-8.
|
/// that the string contains valid UTF-8.
|
||||||
///
|
///
|
||||||
@ -2602,8 +2571,11 @@ impl str {
|
|||||||
let len = self.len();
|
let len = self.len();
|
||||||
let ptr = self.as_ptr() as *mut u8;
|
let ptr = self.as_ptr() as *mut u8;
|
||||||
unsafe {
|
unsafe {
|
||||||
(from_raw_parts_mut(ptr, mid),
|
(from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, mid)),
|
||||||
from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
|
from_utf8_unchecked_mut(slice::from_raw_parts_mut(
|
||||||
|
ptr.offset(mid as isize),
|
||||||
|
len - mid
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
slice_error_fail(self, 0, mid)
|
slice_error_fail(self, 0, mid)
|
||||||
|
Loading…
Reference in New Issue
Block a user