Skip to content

Commit

Permalink
Initial implementation of str::from_raw_parts[_mut]
Browse files Browse the repository at this point in the history
  • Loading branch information
Sky9x committed Dec 31, 2023
1 parent 64d5515 commit bfbe9db
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
43 changes: 42 additions & 1 deletion library/core/src/str/converts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Ways to create a `str` from bytes slice.

use crate::mem;
use crate::intrinsics::{
assert_unsafe_precondition, is_aligned_and_not_null, is_valid_allocation_size,
};
use crate::{mem, ptr, slice};

use super::validations::run_utf8_validation;
use super::Utf8Error;
Expand Down Expand Up @@ -205,3 +208,41 @@ pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
// comes from a reference which is guaranteed to be valid for writes.
unsafe { &mut *(v as *mut [u8] as *mut str) }
}

/// Creates an `&str` from a pointer and a length.
///
/// The pointed-to bytes must be valid UTF-8.
/// If this might not be the case, use `str::from_utf8(slice::from_raw_parts(ptr, len))`,
/// which will return an `Err` if the data isn't valid UTF-8.
///
/// This function is the `str` equivalent of [`slice::from_raw_parts`].
/// See that function's documentation for safety concerns and examples.
///
/// The mutable version of this function is [`from_raw_parts_mut`].
#[inline]
#[must_use]
#[unstable(feature = "str_from_raw_parts", issue = "119206")]
#[rustc_const_unstable(feature = "str_from_raw_parts", issue = "119206")]
pub const unsafe fn from_raw_parts<'a>(ptr: *const u8, len: usize) -> &'a str {
// SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
unsafe { &*ptr::from_raw_parts(ptr.cast(), len) }
}

/// Creates an `&mut str` from a pointer and a length.
///
/// The pointed-to bytes must be valid UTF-8.
/// If this might not be the case, use `str::from_utf8_mut(slice::from_raw_parts_mut(ptr, len))`,
/// which will return an `Err` if the data isn't valid UTF-8.
///
/// This function is the `str` equivalent of [`slice::from_raw_parts_mut`].
/// See that function's documentation for safety concerns and examples.
///
/// The immutable version of this function is [`from_raw_parts`].
#[inline]
#[must_use]
#[unstable(feature = "str_from_raw_parts", issue = "119206")]
#[rustc_const_unstable(feature = "const_str_from_raw_parts_mut", issue = "119206")]
pub const unsafe fn from_raw_parts_mut<'a>(ptr: *mut u8, len: usize) -> &'a str {
// SAFETY: the caller must uphold the safety contract for `from_raw_parts_mut`.
unsafe { &mut *ptr::from_raw_parts_mut(ptr.cast(), len) }
}
3 changes: 3 additions & 0 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub use converts::{from_utf8, from_utf8_unchecked};
#[stable(feature = "str_mut_extras", since = "1.20.0")]
pub use converts::{from_utf8_mut, from_utf8_unchecked_mut};

#[unstable(feature = "str_from_raw_parts", issue = "119206")]
pub use converts::{from_raw_parts, from_raw_parts_mut};

#[stable(feature = "rust1", since = "1.0.0")]
pub use error::{ParseBoolError, Utf8Error};

Expand Down

0 comments on commit bfbe9db

Please sign in to comment.