@@ -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" ) ]
638674impl < T : ?Sized > Clone for NonNull < T > {
639675 #[ inline]
0 commit comments