Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 93d27fa

Browse files
authoredJan 26, 2024
Rollup merge of rust-lang#119466 - Sky9x:str_from_raw_parts, r=dtolnay
Initial implementation of `str::from_raw_parts[_mut]` ACP (accepted): rust-lang/libs-team#167 Tracking issue: rust-lang#119206 Thanks to `@Kixiron` for previous work on this (rust-lang#107207) `@rustbot` label +T-libs-api -T-libs r? `@thomcc` Closes rust-lang#107207.
2 parents 2a41d8b + f94a942 commit 93d27fa

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed
 

‎library/alloc/src/str.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub use core::str::SplitAsciiWhitespace;
3030
pub use core::str::SplitInclusive;
3131
#[stable(feature = "rust1", since = "1.0.0")]
3232
pub use core::str::SplitWhitespace;
33+
#[unstable(feature = "str_from_raw_parts", issue = "119206")]
34+
pub use core::str::{from_raw_parts, from_raw_parts_mut};
3335
#[stable(feature = "rust1", since = "1.0.0")]
3436
pub use core::str::{from_utf8, from_utf8_mut, Bytes, CharIndices, Chars};
3537
#[stable(feature = "rust1", since = "1.0.0")]

‎library/core/src/str/converts.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Ways to create a `str` from bytes slice.
22
3-
use crate::mem;
3+
use crate::{mem, ptr};
44

55
use super::validations::run_utf8_validation;
66
use super::Utf8Error;
@@ -205,3 +205,41 @@ pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
205205
// comes from a reference which is guaranteed to be valid for writes.
206206
unsafe { &mut *(v as *mut [u8] as *mut str) }
207207
}
208+
209+
/// Creates an `&str` from a pointer and a length.
210+
///
211+
/// The pointed-to bytes must be valid UTF-8.
212+
/// If this might not be the case, use `str::from_utf8(slice::from_raw_parts(ptr, len))`,
213+
/// which will return an `Err` if the data isn't valid UTF-8.
214+
///
215+
/// This function is the `str` equivalent of [`slice::from_raw_parts`](crate::slice::from_raw_parts).
216+
/// See that function's documentation for safety concerns and examples.
217+
///
218+
/// The mutable version of this function is [`from_raw_parts_mut`].
219+
#[inline]
220+
#[must_use]
221+
#[unstable(feature = "str_from_raw_parts", issue = "119206")]
222+
#[rustc_const_unstable(feature = "str_from_raw_parts", issue = "119206")]
223+
pub const unsafe fn from_raw_parts<'a>(ptr: *const u8, len: usize) -> &'a str {
224+
// SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
225+
unsafe { &*ptr::from_raw_parts(ptr.cast(), len) }
226+
}
227+
228+
/// Creates an `&mut str` from a pointer and a length.
229+
///
230+
/// The pointed-to bytes must be valid UTF-8.
231+
/// If this might not be the case, use `str::from_utf8_mut(slice::from_raw_parts_mut(ptr, len))`,
232+
/// which will return an `Err` if the data isn't valid UTF-8.
233+
///
234+
/// This function is the `str` equivalent of [`slice::from_raw_parts_mut`](crate::slice::from_raw_parts_mut).
235+
/// See that function's documentation for safety concerns and examples.
236+
///
237+
/// The immutable version of this function is [`from_raw_parts`].
238+
#[inline]
239+
#[must_use]
240+
#[unstable(feature = "str_from_raw_parts", issue = "119206")]
241+
#[rustc_const_unstable(feature = "const_str_from_raw_parts_mut", issue = "119206")]
242+
pub const unsafe fn from_raw_parts_mut<'a>(ptr: *mut u8, len: usize) -> &'a str {
243+
// SAFETY: the caller must uphold the safety contract for `from_raw_parts_mut`.
244+
unsafe { &mut *ptr::from_raw_parts_mut(ptr.cast(), len) }
245+
}

‎library/core/src/str/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ pub use converts::{from_utf8, from_utf8_unchecked};
3333
#[stable(feature = "str_mut_extras", since = "1.20.0")]
3434
pub use converts::{from_utf8_mut, from_utf8_unchecked_mut};
3535

36+
#[unstable(feature = "str_from_raw_parts", issue = "119206")]
37+
pub use converts::{from_raw_parts, from_raw_parts_mut};
38+
3639
#[stable(feature = "rust1", since = "1.0.0")]
3740
pub use error::{ParseBoolError, Utf8Error};
3841

0 commit comments

Comments
 (0)
Please sign in to comment.