Skip to content

Commit 889d8dc

Browse files
committed
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.
2 parents 61f35e5 + b30aaf2 commit 889d8dc

File tree

2 files changed

+11
-38
lines changed

2 files changed

+11
-38
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

+5-33
Original file line numberDiff line numberDiff line change
@@ -376,37 +376,6 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
376376
Ok(unsafe { from_utf8_unchecked_mut(v) })
377377
}
378378

379-
/// Forms a str from a pointer and a length.
380-
///
381-
/// The `len` argument is the number of bytes in the string.
382-
///
383-
/// # Safety
384-
///
385-
/// This function is unsafe as there is no guarantee that the given pointer is
386-
/// valid for `len` bytes, nor whether the lifetime inferred is a suitable
387-
/// lifetime for the returned str.
388-
///
389-
/// The data must be valid UTF-8
390-
///
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.
395-
///
396-
/// # Caveat
397-
///
398-
/// The lifetime for the returned str is inferred from its usage. To
399-
/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
400-
/// source lifetime is safe in the context, such as by providing a helper
401-
/// function taking the lifetime of a host value for the str, or by explicit
402-
/// annotation.
403-
/// Performs the same functionality as `from_raw_parts`, except that a mutable
404-
/// str is returned.
405-
///
406-
unsafe fn from_raw_parts_mut<'a>(p: *mut u8, len: usize) -> &'a mut str {
407-
from_utf8_unchecked_mut(slice::from_raw_parts_mut(p, len))
408-
}
409-
410379
/// Converts a slice of bytes to a string slice without checking
411380
/// that the string contains valid UTF-8.
412381
///
@@ -2602,8 +2571,11 @@ impl str {
26022571
let len = self.len();
26032572
let ptr = self.as_ptr() as *mut u8;
26042573
unsafe {
2605-
(from_raw_parts_mut(ptr, mid),
2606-
from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
2574+
(from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, mid)),
2575+
from_utf8_unchecked_mut(slice::from_raw_parts_mut(
2576+
ptr.offset(mid as isize),
2577+
len - mid
2578+
)))
26072579
}
26082580
} else {
26092581
slice_error_fail(self, 0, mid)

0 commit comments

Comments
 (0)