diff --git a/src/lib.rs b/src/lib.rs index 86fb997..efff3bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -142,6 +142,7 @@ impl SizeLimit for Bounded { Err(Box::new(ErrorKind::SizeLimit)) } } + #[inline(always)] fn limit(&self) -> Option { Some(self.0) } } @@ -149,6 +150,7 @@ impl SizeLimit for Bounded { impl SizeLimit for Infinite { #[inline(always)] fn add(&mut self, _: u64) -> Result<()> { Ok (()) } + #[inline(always)] fn limit(&self) -> Option { None } } diff --git a/src/serde/mod.rs b/src/serde/mod.rs index df78d17..f5c801a 100644 --- a/src/serde/mod.rs +++ b/src/serde/mod.rs @@ -137,15 +137,15 @@ pub fn serialize_into(writer: &mut W, value: &T, siz pub fn serialize(value: &T, size_limit: S) -> Result> where T: serde::Serialize, S: SizeLimit, E: ByteOrder { - // Since we are putting values directly into a vector, we can do size - // computation out here and pre-allocate a buffer of *exactly* - // the right size. let mut writer = match size_limit.limit() { Some(size_limit) => { let actual_size = try!(serialized_size_bounded(value, size_limit).ok_or(ErrorKind::SizeLimit)); Vec::with_capacity(actual_size as usize) } - None => Vec::new() + None => { + let size = serialized_size(value) as usize; + Vec::with_capacity(size) + } }; try!(serialize_into::<_, _, _, E>(&mut writer, value, super::Infinite));