summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-05-29 12:50:06 +0000
committerbors <bors@rust-lang.org>2018-05-29 12:50:06 +0000
commit889d8dcaa7546acf5b2f406b338caa708a8d93f8 (patch)
treee6814ac3d79e0a5eb38f326117597e95ff5156d2 /src
parentAuto merge of #51019 - Zoxc:hash-bytes, r=michaelwoerister (diff)
parentget rid of str::from_raw_parts_mut (diff)
downloadgrust-889d8dcaa7546acf5b2f406b338caa708a8d93f8.tar.gz
grust-889d8dcaa7546acf5b2f406b338caa708a8d93f8.tar.bz2
grust-889d8dcaa7546acf5b2f406b338caa708a8d93f8.tar.xz
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.
Diffstat (limited to 'src')
-rw-r--r--src/libcore/slice/mod.rs11
-rw-r--r--src/libcore/str/mod.rs38
2 files changed, 11 insertions, 38 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs
index ab986e4c86..d52cc8cbe3 100644
--- a/src/libcore/slice/mod.rs
+++ b/src/libcore/slice/mod.rs
@@ -3839,10 +3839,9 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
3839/// valid for `len` elements, nor whether the lifetime inferred is a suitable 3839/// valid for `len` elements, nor whether the lifetime inferred is a suitable
3840/// lifetime for the returned slice. 3840/// lifetime for the returned slice.
3841/// 3841///
3842/// `p` must be non-null, even for zero-length slices, because non-zero bits 3842/// `p` must be non-null and aligned, even for zero-length slices, as is
3843/// are required to distinguish between a zero-length slice within `Some()` 3843/// required for all references. However, for zero-length slices, `p` can be
3844/// from `None`. `p` can be a bogus non-dereferencable pointer, such as `0x1`, 3844/// a bogus non-dereferencable pointer such as [`NonNull::dangling()`].
3845/// for zero-length slices, though.
3846/// 3845///
3847/// # Caveat 3846/// # Caveat
3848/// 3847///
@@ -3864,6 +3863,8 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
3864/// let slice = slice::from_raw_parts(ptr, amt); 3863/// let slice = slice::from_raw_parts(ptr, amt);
3865/// } 3864/// }
3866/// ``` 3865/// ```
3866///
3867/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
3867#[inline] 3868#[inline]
3868#[stable(feature = "rust1", since = "1.0.0")] 3869#[stable(feature = "rust1", since = "1.0.0")]
3869pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] { 3870pub 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] {
3875/// 3876///
3876/// This function is unsafe for the same reasons as `from_raw_parts`, as well 3877/// This function is unsafe for the same reasons as `from_raw_parts`, as well
3877/// as not being able to provide a non-aliasing guarantee of the returned 3878/// 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
3879/// `from_raw_parts`. 3880/// `from_raw_parts`.
3880#[inline] 3881#[inline]
3881#[stable(feature = "rust1", since = "1.0.0")] 3882#[stable(feature = "rust1", since = "1.0.0")]
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index 13d808ede5..3169893fcd 100644
--- a/src/libcore/str/mod.rs
+++ b/src/libcore/str/mod.rs
@@ -376,37 +376,6 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
376 Ok(unsafe { from_utf8_unchecked_mut(v) }) 376 Ok(unsafe { from_utf8_unchecked_mut(v) })
377} 377}
378 378
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///
406unsafe 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
410/// Converts a slice of bytes to a string slice without checking 379/// Converts a slice of bytes to a string slice without checking
411/// that the string contains valid UTF-8. 380/// that the string contains valid UTF-8.
412/// 381///
@@ -2602,8 +2571,11 @@ impl str {
2602 let len = self.len(); 2571 let len = self.len();
2603 let ptr = self.as_ptr() as *mut u8; 2572 let ptr = self.as_ptr() as *mut u8;
2604 unsafe { 2573 unsafe {
2605 (from_raw_parts_mut(ptr, mid), 2574 (from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, mid)),
2606 from_raw_parts_mut(ptr.offset(mid as isize), len - mid)) 2575 from_utf8_unchecked_mut(slice::from_raw_parts_mut(
2576 ptr.offset(mid as isize),
2577 len - mid
2578 )))
2607 } 2579 }
2608 } else { 2580 } else {
2609 slice_error_fail(self, 0, mid) 2581 slice_error_fail(self, 0, mid)