Skip to content

Commit

Permalink
feat: implement size_hint for MapAccess
Browse files Browse the repository at this point in the history
Fixes: #87

Signed-off-by: Ahmed Charles <[email protected]>
  • Loading branch information
ahmedcharles committed Feb 19, 2024
1 parent ba48adc commit b6e4772
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ciborium/src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<'de> serde::de::Visitor<'de> for Visitor {

#[inline]
fn visit_map<A: de::MapAccess<'de>>(self, mut acc: A) -> Result<Self::Value, A::Error> {
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);
Expand Down Expand Up @@ -541,6 +541,29 @@ impl<'a, 'de, T: Iterator<Item = &'a (Value, Value)>> de::MapAccess<'de>
) -> Result<V::Value, Self::Error> {
seed.deserialize(Deserializer(&self.0.next().unwrap().1))
}

#[inline]
fn next_entry_seed<K: de::DeserializeSeed<'de>, V: de::DeserializeSeed<'de>>(
&mut self,
kseed: K,
vseed: V,
) -> Result<Option<(K::Value, V::Value)>, 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<usize> {
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)> {
Expand Down

0 comments on commit b6e4772

Please sign in to comment.