From 9d58ec181f5b42229a2fc26bfb9f23acabc2f647 Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Thu, 30 Mar 2023 13:24:59 +0200 Subject: [PATCH] Small improvements, added documentation about the new feature --- src/error.rs | 2 +- src/features/impl_alloc.rs | 46 +------------------------------------- src/lib.rs | 15 +++++++------ 3 files changed, 10 insertions(+), 53 deletions(-) diff --git a/src/error.rs b/src/error.rs index 26b2d38e..1b82f6e7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,7 +12,7 @@ pub enum EncodeError { /// The writer ran out of storage. UnexpectedEnd, - /// The RefCell is already borrowed + /// The `RefCell` is already borrowed RefCellAlreadyBorrowed { /// The inner borrow error inner: core::cell::BorrowError, diff --git a/src/features/impl_alloc.rs b/src/features/impl_alloc.rs index 9f193986..fbaf35f2 100644 --- a/src/features/impl_alloc.rs +++ b/src/features/impl_alloc.rs @@ -41,20 +41,8 @@ impl enc::write::Writer for VecWriter { #[inline(always)] fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> { self.inner.try_reserve(bytes.len())?; + self.inner.extend_from_slice(bytes); - let start = self.inner.len(); - - // Get a slice of `&mut [MaybeUninit]` of the remaining capacity - let remaining = &mut self.inner.spare_capacity_mut()[..bytes.len()]; - for (i, b) in bytes.iter().copied().enumerate() { - // TODO: is there a better way to copy from `&mut [MaybeUninit]` to `&[u8]`? - remaining[i].write(b); - } - - unsafe { - // Safety: We reserved enough bytes, and the bytes have values written to them - self.inner.set_len(start + bytes.len()); - } Ok(()) } } @@ -462,41 +450,9 @@ where } } -#[cfg(feature = "unstable-strict-oom-checks")] impl Decode for Box<[T]> where T: Decode + 'static, -{ - fn decode(decoder: &mut D) -> Result { - let len = crate::de::decode_slice_len(decoder)?; - decoder.claim_container_read::(len)?; - - unsafe { - let mut result = Box::try_new_uninit_slice(len)?; - - let mut guard = DropGuard { - slice: &mut result, - idx: 0, - }; - - while guard.idx < len { - decoder.unclaim_bytes_read(core::mem::size_of::()); - let t = T::decode(decoder)?; - - guard.slice.get_unchecked_mut(guard.idx).write(t); - guard.idx += 1; - } - - core::mem::forget(guard); - Ok(result.assume_init()) - } - } -} - -#[cfg(not(feature = "unstable-strict-oom-checks"))] -impl Decode for Box<[T]> -where - T: Decode, { fn decode(decoder: &mut D) -> Result { let vec = Vec::::decode(decoder)?; diff --git a/src/lib.rs b/src/lib.rs index 41e29a23..8b1fa6c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,13 +19,14 @@ //! //! # Features //! -//! |Name |Default?|Supported types for Encode/Decode|Enabled methods |Other| -//! |------|--------|-----------------------------------------|-----------------------------------------------------------------|-----| -//! |std | Yes |`HashMap` and `HashSet`|`decode_from_std_read` and `encode_into_std_write`| -//! |alloc | Yes |All common containers in alloc, like `Vec`, `String`, `Box`|`encode_to_vec`| -//! |atomic| Yes |All `Atomic*` integer types, e.g. `AtomicUsize`, and `AtomicBool`|| -//! |derive| Yes |||Enables the `BorrowDecode`, `Decode` and `Encode` derive macros| -//! |serde | No |`Compat` and `BorrowCompat`, which will work for all types that implement serde's traits|serde-specific encode/decode functions in the [serde] module|Note: There are several [known issues](serde/index.html#known-issues) when using serde and bincode| +//! |Name |Default?|Supported types for Encode/Decode|Enabled methods |Other| +//! |--------------------------|--------|-----------------------------------------|-----------------------------------------------------------------|-----| +//! |std | Yes |`HashMap` and `HashSet`|`decode_from_std_read` and `encode_into_std_write`| +//! |alloc | Yes |All common containers in alloc, like `Vec`, `String`, `Box`|`encode_to_vec`| +//! |atomic | Yes |All `Atomic*` integer types, e.g. `AtomicUsize`, and `AtomicBool`|| +//! |derive | Yes |||Enables the `BorrowDecode`, `Decode` and `Encode` derive macros| +//! |serde | No |`Compat` and `BorrowCompat`, which will work for all types that implement serde's traits|serde-specific encode/decode functions in the [serde] module|Note: There are several [known issues](serde/index.html#known-issues) when using serde and bincode| +//! |unstable-strict-oom-checks| No |||Enabled strict OOM checks which ensures that bincode will never cause OOM issues.
This requires nightly feature so you need to use a nightly compiler.
This may break or be changed at any point in the future| //! //! # Which functions to use //!