Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return allocation error in deserialize instead of panicking #238

Merged
merged 1 commit into from Nov 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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