From c1e9828e7daf7f6d5fbee55ee132704d0a6f35f5 Mon Sep 17 00:00:00 2001 From: Trangar Date: Thu, 13 Jan 2022 20:52:51 +0100 Subject: [PATCH] Implemented the newly stabilized CString::from_vec_with_nul method (#473) --- src/error.rs | 9 ++++++++- src/features/impl_std.rs | 11 ++--------- 2 files changed, 10 insertions(+), 10 deletions(-) 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 }) } }