Skip to content

Commit

Permalink
Fixed a possible fallible allocation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Koenders committed Sep 19, 2023
1 parent 1c67710 commit cea3f8d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/features/impl_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub(crate) struct VecWriter {

impl VecWriter {
/// Create a new vec writer with the given capacity
pub fn with_capacity(cap: usize) -> Self {
Self {
inner: Vec::with_capacity(cap),
}
pub fn with_capacity(cap: usize) -> Result<Self, alloc::collections::TryReserveError> {
let mut inner = Vec::new();
inner.try_reserve(cap)?;
Ok(Self { inner })
}
// May not be used in all feature combinations
#[allow(dead_code)]
Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn encode_to_vec<E: enc::Encode, C: Config>(val: E, config: C) -> Result<Vec
val.encode(&mut size_writer)?;
size_writer.into_writer().bytes_written
};
let writer = VecWriter::with_capacity(size);
let writer = VecWriter::with_capacity(size).map_err(EncodeError::from)?;
let mut encoder = enc::EncoderImpl::<_, C>::new(writer, config);
val.encode(&mut encoder)?;
Ok(encoder.into_writer().inner)
Expand Down Expand Up @@ -453,6 +453,8 @@ where
}
}

// TODO
// Vec does not implement Into for Box<[T]> because it allocates again
impl<T> Decode for Box<[T]>
where
T: Decode + 'static,
Expand All @@ -465,7 +467,6 @@ where

// TODO
// Vec does not implement Into for Box<[T]> because it allocates again
// we could do this manually with `Box::try_new_uninit`
#[cfg(not(feature = "unstable-strict-oom-checks"))]
impl<'de, T> BorrowDecode<'de> for Box<[T]>
where
Expand Down

0 comments on commit cea3f8d

Please sign in to comment.