Skip to content

Commit

Permalink
Fixed compilation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Koenders committed Mar 17, 2024
1 parent e44d7c1 commit c4e11a1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
81 changes: 59 additions & 22 deletions src/features/impl_alloc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
de::{read::Reader, BorrowDecoder, Decode, Decoder},
de::{BorrowDecoder, Decode, Decoder},
enc::{
self,
write::{SizeWriter, Writer},
Expand All @@ -16,6 +16,9 @@ use alloc::{
vec::Vec,
};

#[cfg(not(feature = "unstable-strict-oom-checks"))]
use crate::de::read::Reader;

#[cfg(target_has_atomic = "ptr")]
use alloc::sync::Arc;

Expand All @@ -38,6 +41,7 @@ impl VecWriter {
}
}

#[cfg(not(feature = "unstable-strict-oom-checks"))]
impl enc::write::Writer for VecWriter {
#[inline(always)]
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
Expand All @@ -48,6 +52,21 @@ impl enc::write::Writer for VecWriter {
}
}

#[cfg(feature = "unstable-strict-oom-checks")]
impl enc::write::Writer for VecWriter {
#[inline(always)]
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
self.inner.try_reserve(bytes.len())?;
unsafe {
// Safety: We just reserved `bytes.len()` additional bytes
core::mem::MaybeUninit::copy_from_slice(self.inner.spare_capacity_mut(), bytes);
self.inner.set_len(self.inner.len() + bytes.len());
}

Ok(())
}
}

/// Encode the given value into a `Vec<u8>` with the given `Config`. See the [config] module for more information.
///
/// [config]: config/index.html
Expand Down Expand Up @@ -263,25 +282,34 @@ where
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let len = crate::de::decode_slice_len(decoder)?;

#[cfg(not(feature = "unstable-strict-oom-checks"))]
if unty::type_equal::<T, u8>() {
decoder.claim_container_read::<T>(len)?;
// optimize for reading u8 vecs
let mut vec = alloc::vec::from_elem(0u8, len);
decoder.reader().read(&mut vec)?;
// Safety: Vec<T> is Vec<u8>
Ok(unsafe { core::mem::transmute(vec) })
} else {
decoder.claim_container_read::<T>(len)?;
return Ok(unsafe { core::mem::transmute(vec) });
}

let mut vec = Vec::with_capacity(len);
for _ in 0..len {
// See the documentation on `unclaim_bytes_read` as to why we're doing this here
decoder.unclaim_bytes_read(core::mem::size_of::<T>());
decoder.claim_container_read::<T>(len)?;

vec.push(T::decode(decoder)?);
}
Ok(vec)
#[cfg(feature = "unstable-strict-oom-checks")]
let mut vec = Vec::try_with_capacity(len)?;
#[cfg(not(feature = "unstable-strict-oom-checks"))]
let mut vec = Vec::with_capacity(len);

for _ in 0..len {
// See the documentation on `unclaim_bytes_read` as to why we're doing this here
decoder.unclaim_bytes_read(core::mem::size_of::<T>());

// Should never fail because we allocated enough capacity
#[cfg(feature = "unstable-strict-oom-checks")]
debug_assert!(vec.push_within_capacity(T::decode(decoder)?).is_ok());
#[cfg(not(feature = "unstable-strict-oom-checks"))]
vec.push(T::decode(decoder)?);
}
Ok(vec)
}
}

Expand All @@ -292,25 +320,33 @@ where
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
let len = crate::de::decode_slice_len(decoder)?;

#[cfg(not(feature = "unstable-strict-oom-checks"))]
if unty::type_equal::<T, u8>() {
decoder.claim_container_read::<T>(len)?;
// optimize for reading u8 vecs
let mut vec = alloc::vec![0u8; len];
let mut vec = alloc::vec::from_elem(0u8, len);
decoder.reader().read(&mut vec)?;
// Safety: Vec<T> is Vec<u8>
Ok(unsafe { core::mem::transmute(vec) })
} else {
decoder.claim_container_read::<T>(len)?;
return Ok(unsafe { core::mem::transmute(vec) });
}
decoder.claim_container_read::<T>(len)?;

let mut vec = Vec::with_capacity(len);
for _ in 0..len {
// See the documentation on `unclaim_bytes_read` as to why we're doing this here
decoder.unclaim_bytes_read(core::mem::size_of::<T>());
#[cfg(feature = "unstable-strict-oom-checks")]
let mut vec = Vec::try_with_capacity(len)?;
#[cfg(not(feature = "unstable-strict-oom-checks"))]
let mut vec = Vec::with_capacity(len);

vec.push(T::borrow_decode(decoder)?);
}
Ok(vec)
for _ in 0..len {
// See the documentation on `unclaim_bytes_read` as to why we're doing this here
decoder.unclaim_bytes_read(core::mem::size_of::<T>());

// Should never fail because we allocated enough capacity
#[cfg(feature = "unstable-strict-oom-checks")]
debug_assert!(vec.push_within_capacity(T::borrow_decode(decoder)?).is_ok());
#[cfg(not(feature = "unstable-strict-oom-checks"))]
vec.push(T::borrow_decode(decoder)?);
}
Ok(vec)
}
}

Expand Down Expand Up @@ -404,6 +440,7 @@ where

// TODO
// Vec does not implement Into for Box<[T]> because it allocates again
#[cfg(not(feature = "unstable-strict-oom-checks"))]
impl<T> Decode for Box<[T]>
where
T: Decode + 'static,
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(
feature = "unstable-strict-oom-checks",
feature(allocator_api, new_uninit)
feature(
allocator_api,
new_uninit,
maybe_uninit_write_slice,
vec_push_within_capacity,
try_with_capacity
)
)]

//! Bincode is a crate for encoding and decoding using a tiny binary
Expand Down

0 comments on commit c4e11a1

Please sign in to comment.