@@ -234,18 +234,14 @@ pub struct NulError(usize, Vec<u8>);
234
234
235
235
/// An error indicating that a nul byte was not in the expected position.
236
236
///
237
- /// The slice used to create a [`CStr`] or the vector used to create a
238
- /// [`CString`] must have one and only one nul byte, positioned at the end.
237
+ /// The slice used to create a [`CStr`] must have one and only one nul byte,
238
+ /// positioned at the end.
239
239
///
240
- /// This error is created by the
241
- /// [`from_bytes_with_nul`][`CStr::from_bytes_with_nul`] method on
242
- /// [`CStr`] or the [`from_vec_with_nul`][`CString::from_vec_with_nul`] method
243
- /// on [`CString`]. See their documentation for more.
240
+ /// This error is created by the [`from_bytes_with_nul`] method on [`CStr`].
241
+ /// See its documentation for more.
244
242
///
245
243
/// [`CStr`]: struct.CStr.html
246
- /// [`CStr::from_bytes_with_nul`]: struct.CStr.html#method.from_bytes_with_nul
247
- /// [`CString`]: struct.CString.html
248
- /// [`CString::from_vec_with_nul`]: struct.CString.html#method.from_vec_with_nul
244
+ /// [`from_bytes_with_nul`]: struct.CStr.html#method.from_bytes_with_nul
249
245
///
250
246
/// # Examples
251
247
///
@@ -726,6 +722,7 @@ impl CString {
726
722
/// # Example
727
723
///
728
724
/// ```
725
+ /// #![feature(cstring_from_vec_with_nul)]
729
726
/// use std::ffi::CString;
730
727
/// assert_eq!(
731
728
/// unsafe { CString::from_vec_with_nul_unchecked(b"abc\0".to_vec()) },
@@ -753,36 +750,44 @@ impl CString {
753
750
/// called without the ending nul byte.
754
751
///
755
752
/// ```
753
+ /// #![feature(cstring_from_vec_with_nul)]
756
754
/// use std::ffi::CString;
757
755
/// assert_eq!(
758
756
/// CString::from_vec_with_nul(b"abc\0".to_vec())
759
757
/// .expect("CString::from_vec_with_nul failed"),
760
- /// CString::new(b"abc".to_vec())
758
+ /// CString::new(b"abc".to_vec()).expect("CString::new failed")
761
759
/// );
762
760
/// ```
763
761
///
764
762
/// A incorrectly formatted vector will produce an error.
765
763
///
766
764
/// ```
767
- /// use std::ffi::{CString, FromBytesWithNulError};
765
+ /// #![feature(cstring_from_vec_with_nul)]
766
+ /// use std::ffi::{CString, FromVecWithNulError};
768
767
/// // Interior nul byte
769
- /// let _: FromBytesWithNulError = CString::from_vec_with_nul(b"a\0bc".to_vec()).unwrap_err();
768
+ /// let _: FromVecWithNulError = CString::from_vec_with_nul(b"a\0bc".to_vec()).unwrap_err();
770
769
/// // No nul byte
771
- /// let _: FromBytesWithNulError = CString::from_vec_with_nul(b"abc".to_vec()).unwrap_err();
770
+ /// let _: FromVecWithNulError = CString::from_vec_with_nul(b"abc".to_vec()).unwrap_err();
772
771
/// ```
773
772
///
774
773
/// [`new`]: #method.new
775
774
#[ unstable( feature = "cstring_from_vec_with_nul" , issue = "73179" ) ]
776
- pub fn from_vec_with_nul ( v : Vec < u8 > ) -> Result < Self , FromBytesWithNulError > {
775
+ pub fn from_vec_with_nul ( v : Vec < u8 > ) -> Result < Self , FromVecWithNulError > {
777
776
let nul_pos = memchr:: memchr ( 0 , & v) ;
778
777
match nul_pos {
779
778
Some ( nul_pos) if nul_pos + 1 == v. len ( ) => {
780
779
// SAFETY: We know there is only one nul byte, at the end
781
780
// of the vec.
782
781
Ok ( unsafe { Self :: from_vec_with_nul_unchecked ( v) } )
783
782
}
784
- Some ( nul_pos) => Err ( FromBytesWithNulError :: interior_nul ( nul_pos) ) ,
785
- None => Err ( FromBytesWithNulError :: not_nul_terminated ( ) ) ,
783
+ Some ( nul_pos) => Err ( FromVecWithNulError {
784
+ error_kind : FromBytesWithNulErrorKind :: InteriorNul ( nul_pos) ,
785
+ bytes : v,
786
+ } ) ,
787
+ None => Err ( FromVecWithNulError {
788
+ error_kind : FromBytesWithNulErrorKind :: NotNulTerminated ,
789
+ bytes : v,
790
+ } ) ,
786
791
}
787
792
}
788
793
}
0 commit comments