Skip to content

Commit 47cc5cc

Browse files
committed
Update to use the new error type and correctly compile the doc tests
1 parent 685f066 commit 47cc5cc

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/libstd/ffi/c_str.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,14 @@ pub struct NulError(usize, Vec<u8>);
234234

235235
/// An error indicating that a nul byte was not in the expected position.
236236
///
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.
239239
///
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.
244242
///
245243
/// [`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
249245
///
250246
/// # Examples
251247
///
@@ -726,6 +722,7 @@ impl CString {
726722
/// # Example
727723
///
728724
/// ```
725+
/// #![feature(cstring_from_vec_with_nul)]
729726
/// use std::ffi::CString;
730727
/// assert_eq!(
731728
/// unsafe { CString::from_vec_with_nul_unchecked(b"abc\0".to_vec()) },
@@ -753,36 +750,44 @@ impl CString {
753750
/// called without the ending nul byte.
754751
///
755752
/// ```
753+
/// #![feature(cstring_from_vec_with_nul)]
756754
/// use std::ffi::CString;
757755
/// assert_eq!(
758756
/// CString::from_vec_with_nul(b"abc\0".to_vec())
759757
/// .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")
761759
/// );
762760
/// ```
763761
///
764762
/// A incorrectly formatted vector will produce an error.
765763
///
766764
/// ```
767-
/// use std::ffi::{CString, FromBytesWithNulError};
765+
/// #![feature(cstring_from_vec_with_nul)]
766+
/// use std::ffi::{CString, FromVecWithNulError};
768767
/// // 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();
770769
/// // 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();
772771
/// ```
773772
///
774773
/// [`new`]: #method.new
775774
#[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> {
777776
let nul_pos = memchr::memchr(0, &v);
778777
match nul_pos {
779778
Some(nul_pos) if nul_pos + 1 == v.len() => {
780779
// SAFETY: We know there is only one nul byte, at the end
781780
// of the vec.
782781
Ok(unsafe { Self::from_vec_with_nul_unchecked(v) })
783782
}
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+
}),
786791
}
787792
}
788793
}

0 commit comments

Comments
 (0)