Skip to content

Commit

Permalink
Return allocation error in deserialize instead of panicking
Browse files Browse the repository at this point in the history
There's no way to catch allocation errors since out of memory errors
cause an abort. Fail gracefully by returning the error instead of
panicking.
  • Loading branch information
epilys committed Nov 8, 2020
1 parent 28fb0f4 commit d1394a0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lib.rs
Expand Up @@ -233,6 +233,12 @@ pub enum CollectionAllocErr {
},
}

impl fmt::Display for CollectionAllocErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Allocation error: {:?}", self)
}
}

impl From<LayoutErr> for CollectionAllocErr {
fn from(_: LayoutErr) -> Self {
CollectionAllocErr::CapacityOverflow
Expand Down Expand Up @@ -1543,8 +1549,10 @@ where
where
B: SeqAccess<'de>,
{
use serde::de::Error;
let len = seq.size_hint().unwrap_or(0);
let mut values = SmallVec::with_capacity(len);
let mut values = SmallVec::new();
values.try_reserve(len).map_err(B::Error::custom)?;

while let Some(value) = seq.next_element()? {
values.push(value);
Expand Down

0 comments on commit d1394a0

Please sign in to comment.