From 148067d338db7aee66625e0f00082371ac5259a2 Mon Sep 17 00:00:00 2001 From: Zhixing Zhang Date: Sat, 28 Jun 2025 14:34:27 -0700 Subject: [PATCH 1/2] Remove FromBytesWithNulErrorKind --- library/alloc/src/ffi/c_str.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 93bdad7538007..d2f0b42149373 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -1,7 +1,7 @@ //! [`CString`] and its related types. use core::borrow::Borrow; -use core::ffi::{CStr, c_char}; +use core::ffi::{CStr, c_char, FromBytesWithNulError}; use core::num::NonZero; use core::slice::memchr; use core::str::{self, FromStr, Utf8Error}; @@ -130,12 +130,6 @@ pub struct CString { #[stable(feature = "alloc_c_string", since = "1.64.0")] pub struct NulError(usize, Vec); -#[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, @@ -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, } @@ -207,6 +201,13 @@ impl FromVecWithNulError { pub fn into_bytes(self) -> Vec { 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`]. @@ -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, }), } @@ -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) } } From a1459088821c4adb1212385f8cea5bc3eea26ad1 Mon Sep 17 00:00:00 2001 From: Zhixing Zhang Date: Sat, 28 Jun 2025 15:44:43 -0700 Subject: [PATCH 2/2] fmt --- library/alloc/src/ffi/c_str.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index d2f0b42149373..cbc79952e7ba7 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -1,7 +1,7 @@ //! [`CString`] and its related types. use core::borrow::Borrow; -use core::ffi::{CStr, c_char, FromBytesWithNulError}; +use core::ffi::{CStr, FromBytesWithNulError, c_char}; use core::num::NonZero; use core::slice::memchr; use core::str::{self, FromStr, Utf8Error};