Skip to content

Remove FromBytesWithNulErrorKind and make FromVecWithNulErrorKind more actionable #143163

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 12 additions & 18 deletions library/alloc/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! [`CString`] and its related types.

use core::borrow::Borrow;
use core::ffi::{CStr, c_char};
use core::ffi::{CStr, FromBytesWithNulError, c_char};
use core::num::NonZero;
use core::slice::memchr;
use core::str::{self, FromStr, Utf8Error};
Expand Down Expand Up @@ -130,12 +130,6 @@ pub struct CString {
#[stable(feature = "alloc_c_string", since = "1.64.0")]
pub struct NulError(usize, Vec<u8>);

#[derive(Clone, PartialEq, Eq, Debug)]
enum FromBytesWithNulErrorKind {
InteriorNul(usize),
NotNulTerminated,
}

/// An error indicating that a nul byte was not in the expected position.
///
/// The vector used to create a [`CString`] must have one and only one nul byte,
Expand All @@ -154,7 +148,7 @@ enum FromBytesWithNulErrorKind {
#[derive(Clone, PartialEq, Eq, Debug)]
#[stable(feature = "alloc_c_string", since = "1.64.0")]
pub struct FromVecWithNulError {
error_kind: FromBytesWithNulErrorKind,
error_kind: FromBytesWithNulError,
bytes: Vec<u8>,
}

Expand Down Expand Up @@ -207,6 +201,13 @@ impl FromVecWithNulError {
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}

/// Access the underlying conversion error that was the cause of this error.
#[must_use]
#[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
pub fn kind(&self) -> FromBytesWithNulError {
self.error_kind
}
}

/// An error indicating invalid UTF-8 when converting a [`CString`] into a [`String`].
Expand Down Expand Up @@ -679,11 +680,11 @@ impl CString {
Ok(unsafe { Self::_from_vec_with_nul_unchecked(v) })
}
Some(nul_pos) => Err(FromVecWithNulError {
error_kind: FromBytesWithNulErrorKind::InteriorNul(nul_pos),
error_kind: FromBytesWithNulError::InteriorNul { position: nul_pos },
bytes: v,
}),
None => Err(FromVecWithNulError {
error_kind: FromBytesWithNulErrorKind::NotNulTerminated,
error_kind: FromBytesWithNulError::NotNulTerminated,
bytes: v,
}),
}
Expand Down Expand Up @@ -1033,14 +1034,7 @@ impl fmt::Display for NulError {
#[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
impl fmt::Display for FromVecWithNulError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.error_kind {
FromBytesWithNulErrorKind::InteriorNul(pos) => {
write!(f, "data provided contains an interior nul byte at pos {pos}")
}
FromBytesWithNulErrorKind::NotNulTerminated => {
write!(f, "data provided is not nul terminated")
}
}
self.error_kind.fmt(f)
}
}

Expand Down
Loading