Skip to content

Commit

Permalink
Small improvements, added documentation about the new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKoenders committed Mar 30, 2023
1 parent d486ea3 commit 9d58ec1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum EncodeError {
/// The writer ran out of storage.
UnexpectedEnd,

/// The RefCell<T> is already borrowed
/// The `RefCell<T>` is already borrowed
RefCellAlreadyBorrowed {
/// The inner borrow error
inner: core::cell::BorrowError,
Expand Down
46 changes: 1 addition & 45 deletions src/features/impl_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>]` 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<u8>]` 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(())
}
}
Expand Down Expand Up @@ -462,41 +450,9 @@ where
}
}

#[cfg(feature = "unstable-strict-oom-checks")]
impl<T> Decode for Box<[T]>
where
T: Decode + 'static,
{
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let len = crate::de::decode_slice_len(decoder)?;
decoder.claim_container_read::<T>(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::<T>());
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<T> Decode for Box<[T]>
where
T: Decode,
{
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let vec = Vec::<T>::decode(decoder)?;
Expand Down
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>This requires nightly feature so you need to use a nightly compiler.<br>This may break or be changed at any point in the future|
//!
//! # Which functions to use
//!
Expand Down

0 comments on commit 9d58ec1

Please sign in to comment.