diff --git a/src/error.rs b/src/error.rs index e24ba8b..41a0d80 100644 --- a/src/error.rs +++ b/src/error.rs @@ -139,13 +139,20 @@ pub enum DecodeError { duration: core::time::Duration, }, - /// The decoder tried to decode a `CStr` or `CString`, but the incoming data contained a 0 byte + /// The decoder tried to decode a `CStr`, but the incoming data contained a 0 byte #[cfg(feature = "std")] CStrNulError { /// The inner exception inner: std::ffi::FromBytesWithNulError, }, + /// The decoder tried to decode a `CString`, but the incoming data contained a 0 byte + #[cfg(feature = "std")] + CStringNulError { + /// The inner exception + inner: std::ffi::FromVecWithNulError, + }, + /// An uncommon error occured, see the inner text for more information #[cfg(feature = "alloc")] OtherString(alloc::string::String), diff --git a/src/features/impl_std.rs b/src/features/impl_std.rs index f06c26e..d49f5c8 100644 --- a/src/features/impl_std.rs +++ b/src/features/impl_std.rs @@ -128,15 +128,8 @@ impl Encode for CString { impl Decode for CString { fn decode(decoder: &mut D) -> Result { - // BlockedTODO: https://github.com/rust-lang/rust/issues/73179 - // use `from_vec_with_nul` instead, combined with: - // let bytes = std::vec::Vec::::decode(decoder)?; - - // now we have to allocate twice unfortunately - let vec: std::vec::Vec = std::vec::Vec::decode(decoder)?; - let cstr = - CStr::from_bytes_with_nul(&vec).map_err(|e| DecodeError::CStrNulError { inner: e })?; - Ok(cstr.into()) + let vec = std::vec::Vec::decode(decoder)?; + CString::from_vec_with_nul(vec).map_err(|inner| DecodeError::CStringNulError { inner }) } }