Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ptr::{str_from_raw_parts, str_from_raw_parts_mut} functions #91216

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
#![feature(const_size_of_val)]
#![feature(const_slice_from_raw_parts)]
#![feature(const_slice_ptr_len)]
#![feature(const_str_from_raw_parts)]
#![feature(const_str_from_utf8_unchecked_mut)]
#![feature(const_swap)]
#![feature(const_trait_impl)]
Expand Down
63 changes: 63 additions & 0 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,69 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
from_raw_parts_mut(data.cast(), len)
}

/// Forms a raw string slice from a pointer and a length.
///
/// The `len` argument is the number of **bytes**, not the number of characters.
///
/// This function is safe, but actually using the return value is unsafe.
/// See the documentation of [`slice::from_raw_parts`] for slice safety requirements and [`str::from_utf8`] for string safety requirements.
///
/// [`slice::from_raw_parts`]: crate::slice::from_raw_parts
/// [`str::from_utf8`]: crate::str::from_utf8
///
/// # Examples
///
/// ```rust
/// #![feature(str_from_raw_parts)]
/// use std::ptr;
///
/// // create a string slice pointer when starting out with a pointer to the first element
/// let x = "abc";
/// let raw_pointer = x.as_ptr();
/// let str = ptr::str_from_raw_parts(raw_pointer, 3);
/// assert_eq!(unsafe { &*str }, x);
/// ```
#[inline]
#[unstable(feature = "str_from_raw_parts", issue = "none")]
#[rustc_const_unstable(feature = "const_str_from_raw_parts", issue = "none")]
pub const fn str_from_raw_parts(data: *const u8, len: usize) -> *const str {
from_raw_parts(data.cast(), len)
}

/// Performs the same functionality as [`str_from_raw_parts`], except that a
/// raw mutable string slice is returned, as opposed to a raw immutable string slice.
///
/// See the documentation of [`slice_from_raw_parts`] for more details.
///
/// This function is safe, but actually using the return value is unsafe.
/// See the documentation of [`slice::from_raw_parts_mut`] for slice safety requirements and [`str::from_utf8_mut`] for string safety requirements.
///
/// [`slice::from_raw_parts_mut`]: crate::slice::from_raw_parts_mut
/// [`str::from_utf8_mut`]: crate::str::from_utf8_mut
///
/// # Examples
///
/// ```rust
/// #![feature(str_from_raw_parts)]
/// use std::ptr;
///
/// let mut x = [b'a', b'b', b'c'];
/// let raw_pointer = x.as_mut_ptr();
/// let str = ptr::str_from_raw_parts_mut(raw_pointer, 3);
///
/// unsafe {
/// (*(str as *mut [u8]))[2] = b'z'; // assign a value at an index in the string slice
/// };
///
/// assert_eq!(unsafe { &*str }, "abz");
/// ```
#[inline]
#[unstable(feature = "str_from_raw_parts", issue = "none")]
#[rustc_const_unstable(feature = "const_str_from_raw_parts", issue = "none")]
pub const fn str_from_raw_parts_mut(data: *mut u8, len: usize) -> *mut str {
from_raw_parts_mut(data.cast(), len)
}

/// Swaps the values at two mutable locations of the same type, without
/// deinitializing either.
///
Expand Down
36 changes: 36 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,42 @@ impl<T> NonNull<[T]> {
}
}

impl NonNull<str> {
/// Creates a non-null raw string slice from a thin pointer and a length.
///
/// The `len` argument is the number of **bytes**, not the number of characters.
///
/// This function is safe, but dereferencing the return value is unsafe.
/// See the documentation of [`slice::from_raw_parts`] for slice safety
/// requirements and [`str::from_utf8`] for string safety requirements.
///
/// [`str::from_utf8`]: crate::str::from_utf8
///
/// # Examples
///
/// ```rust
/// #![feature(nonnull_str_from_raw_parts)]
///
/// use std::ptr::NonNull;
///
/// // create a string slice pointer when starting out with a pointer to the first byte
/// let mut x = [b'a', b'b', b'c'];
/// let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap();
/// let str = NonNull::str_from_raw_parts(nonnull_pointer, 3);
/// assert_eq!(unsafe { str.as_ref() }, "abc");
/// ```
///
/// (Note that this example artificially demonstrates a use of this method,
/// but `let str = NonNull::from(str::from_utf8_unchecked(&x[..]));` would be a better way to write code like this.)
#[unstable(feature = "nonnull_str_from_raw_parts", issue = "none")]
#[rustc_const_unstable(feature = "const_nonnull_str_from_raw_parts", issue = "none")]
#[inline]
pub const fn str_from_raw_parts(data: NonNull<u8>, len: usize) -> Self {
// SAFETY: `data` is a `NonNull` pointer which is necessarily non-null
unsafe { Self::new_unchecked(super::str_from_raw_parts_mut(data.as_ptr(), len)) }
}
}

#[stable(feature = "nonnull", since = "1.25.0")]
impl<T: ?Sized> Clone for NonNull<T> {
#[inline]
Expand Down