Skip to content

Commit 927d775

Browse files
committed
Add NonNull::<str>::str_from_raw_parts
1 parent ebcd7b1 commit 927d775

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
#![feature(const_size_of_val)]
133133
#![feature(const_slice_from_raw_parts)]
134134
#![feature(const_slice_ptr_len)]
135+
#![feature(const_str_from_raw_parts)]
135136
#![feature(const_str_from_utf8_unchecked_mut)]
136137
#![feature(const_swap)]
137138
#![feature(const_trait_impl)]

library/core/src/ptr/non_null.rs

+36
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,42 @@ impl<T> NonNull<[T]> {
634634
}
635635
}
636636

637+
impl NonNull<str> {
638+
/// Creates a non-null raw string slice from a thin pointer and a length.
639+
///
640+
/// The `len` argument is the number of **bytes**, not the number of characters.
641+
///
642+
/// This function is safe, but dereferencing the return value is unsafe.
643+
/// See the documentation of [`slice::from_raw_parts`] for slice safety
644+
/// requirements and [`str::from_utf8`] for string safety requirements.
645+
///
646+
/// [`str::from_utf8`]: crate::str::from_utf8
647+
///
648+
/// # Examples
649+
///
650+
/// ```rust
651+
/// #![feature(nonnull_str_from_raw_parts)]
652+
///
653+
/// use std::ptr::NonNull;
654+
///
655+
/// // create a string slice pointer when starting out with a pointer to the first byte
656+
/// let mut x = [b'a', b'b', b'c'];
657+
/// let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap();
658+
/// let str = NonNull::str_from_raw_parts(nonnull_pointer, 3);
659+
/// assert_eq!(unsafe { str.as_ref() }, "abc");
660+
/// ```
661+
///
662+
/// (Note that this example artificially demonstrates a use of this method,
663+
/// but `let str = NonNull::from(str::from_utf8_unchecked(&x[..]));` would be a better way to write code like this.)
664+
#[unstable(feature = "nonnull_str_from_raw_parts", issue = "none")]
665+
#[rustc_const_unstable(feature = "const_nonnull_str_from_raw_parts", issue = "none")]
666+
#[inline]
667+
pub const fn str_from_raw_parts(data: NonNull<u8>, len: usize) -> Self {
668+
// SAFETY: `data` is a `NonNull` pointer which is necessarily non-null
669+
unsafe { Self::new_unchecked(super::str_from_raw_parts_mut(data.as_ptr(), len)) }
670+
}
671+
}
672+
637673
#[stable(feature = "nonnull", since = "1.25.0")]
638674
impl<T: ?Sized> Clone for NonNull<T> {
639675
#[inline]

0 commit comments

Comments
 (0)