From b6e477273dd1a1d1b83ab88ff14d522c6853524f Mon Sep 17 00:00:00 2001 From: Ahmed Charles Date: Mon, 19 Feb 2024 01:49:27 -0800 Subject: [PATCH] feat: implement `size_hint` for `MapAccess` Fixes: #87 Signed-off-by: Ahmed Charles --- ciborium/src/value/de.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ciborium/src/value/de.rs b/ciborium/src/value/de.rs index f59ce92..256c09c 100644 --- a/ciborium/src/value/de.rs +++ b/ciborium/src/value/de.rs @@ -124,7 +124,7 @@ impl<'de> serde::de::Visitor<'de> for Visitor { #[inline] fn visit_map>(self, mut acc: A) -> Result { - let mut map = Vec::<(Value, Value)>::new(); + let mut map = Vec::<(Value, Value)>::with_capacity(acc.size_hint().unwrap_or(0)); while let Some(kv) = acc.next_entry()? { map.push(kv); @@ -541,6 +541,29 @@ impl<'a, 'de, T: Iterator> de::MapAccess<'de> ) -> Result { seed.deserialize(Deserializer(&self.0.next().unwrap().1)) } + + #[inline] + fn next_entry_seed, V: de::DeserializeSeed<'de>>( + &mut self, + kseed: K, + vseed: V, + ) -> Result, Self::Error> { + match self.0.next() { + Some((k, v)) => Ok(Some(( + kseed.deserialize(Deserializer(k))?, + vseed.deserialize(Deserializer(v))?, + ))), + None => Ok(None), + } + } + + #[inline] + fn size_hint(&self) -> Option { + match self.0.size_hint() { + (lower, Some(upper)) if lower == upper => Some(upper), + _ => None, + } + } } impl<'a, 'de> de::EnumAccess<'de> for Deserializer<&'a (Value, Value)> {