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 bfbe9db

Browse files
committedDec 31, 2023
Initial implementation of str::from_raw_parts[_mut]
1 parent 64d5515 commit bfbe9db

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
 

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

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Ways to create a `str` from bytes slice.
22
3-
use crate::mem;
3+
use crate::intrinsics::{
4+
assert_unsafe_precondition, is_aligned_and_not_null, is_valid_allocation_size,
5+
};
6+
use crate::{mem, ptr, slice};
47

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

‎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.