Skip to content

Commit

Permalink
Added try_reserve to VecWriter, Vec, VecDeque and HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKoenders committed Dec 11, 2021
1 parent d45db8d commit f2731c0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Errors that can be encounting by Encoding and Decoding.
#[cfg(feature = "alloc")]
use alloc::collections::TryReserveError;

/// Errors that can be encountered by encoding a type
#[non_exhaustive]
#[derive(Debug)]
Expand Down Expand Up @@ -54,6 +57,13 @@ pub enum EncodeError {
/// Serde provided bincode with a sequence without a length, which is not supported in bincode
#[cfg(feature = "serde")]
SequenceMustHaveLength,

/// bincode failed to allocate enough memory
#[cfg(feature = "alloc")]
OutOfMemory {
/// The inner error
inner: TryReserveError,
},
}

impl core::fmt::Display for EncodeError {
Expand Down Expand Up @@ -139,6 +149,13 @@ pub enum DecodeError {
/// Serde tried decoding a borrowed value from an owned reader. Use `serde_decode_borrowed_from_*` instead
#[cfg(feature = "serde")]
CannotBorrowOwnedData,

/// bincode failed to allocate enough memory
#[cfg(feature = "alloc")]
OutOfMemory {
/// The inner error
inner: TryReserveError,
},
}

impl core::fmt::Display for DecodeError {
Expand Down
17 changes: 14 additions & 3 deletions src/features/impl_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ impl VecWriter {

impl enc::write::Writer for VecWriter {
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
self.inner
.try_reserve(bytes.len())
.map_err(|inner| EncodeError::OutOfMemory { inner })?;
self.inner.extend_from_slice(bytes);
Ok(())
}
Expand All @@ -52,7 +55,11 @@ where
{
fn decode<D: Decoder>(mut decoder: D) -> Result<Self, DecodeError> {
let len = crate::de::decode_slice_len(&mut decoder)?;
let mut map = BinaryHeap::with_capacity(len);
let mut map = BinaryHeap::new();
// TODO:
// map.try_reserve(len).map_err(|inner| DecodeError::OutOfMemory { inner })?;
map.reserve(len);

for _ in 0..len {
let key = T::decode(&mut decoder)?;
map.push(key);
Expand Down Expand Up @@ -140,7 +147,9 @@ where
{
fn decode<D: Decoder>(mut decoder: D) -> Result<Self, DecodeError> {
let len = crate::de::decode_slice_len(&mut decoder)?;
let mut map = VecDeque::with_capacity(len);
let mut map = VecDeque::new();
map.try_reserve(len)
.map_err(|inner| DecodeError::OutOfMemory { inner })?;
for _ in 0..len {
let key = T::decode(&mut decoder)?;
map.push_back(key);
Expand Down Expand Up @@ -168,7 +177,9 @@ where
{
fn decode<D: Decoder>(mut decoder: D) -> Result<Self, DecodeError> {
let len = crate::de::decode_slice_len(&mut decoder)?;
let mut vec = Vec::with_capacity(len);
let mut vec = Vec::new();
vec.try_reserve(len)
.map_err(|inner| DecodeError::OutOfMemory { inner })?;
for _ in 0..len {
vec.push(T::decode(&mut decoder)?);
}
Expand Down
5 changes: 4 additions & 1 deletion src/features/impl_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ where
{
fn decode<D: Decoder>(mut decoder: D) -> Result<Self, DecodeError> {
let len = crate::de::decode_slice_len(&mut decoder)?;
let mut map = HashMap::with_capacity(len);
let mut map = HashMap::new();
map.try_reserve(len)
.map_err(|inner| DecodeError::OutOfMemory { inner })?;

for _ in 0..len {
let k = K::decode(&mut decoder)?;
let v = V::decode(&mut decoder)?;
Expand Down

0 comments on commit f2731c0

Please sign in to comment.