Revise comments on the FlatMapDeserializer entry taker

This commit is contained in:
David Tolnay
2023-05-10 00:39:10 -07:00
parent b5a9eff32e
commit ee9166ec97
+16 -17
View File
@@ -2750,8 +2750,8 @@ where
where where
V: Visitor<'de>, V: Visitor<'de>,
{ {
for item in self.0.iter_mut() { for entry in self.0.iter_mut() {
if let Some((key, value)) = use_item(item, variants) { if let Some((key, value)) = flat_map_take_entry(entry, variants) {
return visitor.visit_enum(EnumDeserializer::new(key, Some(value))); return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
} }
} }
@@ -2902,8 +2902,8 @@ where
where where
T: DeserializeSeed<'de>, T: DeserializeSeed<'de>,
{ {
for item in self.iter.by_ref() { for entry in self.iter.by_ref() {
if let Some((key, content)) = use_item(item, self.fields) { if let Some((key, content)) = flat_map_take_entry(entry, self.fields) {
self.pending_content = Some(content); self.pending_content = Some(content);
return seed.deserialize(ContentDeserializer::new(key)).map(Some); return seed.deserialize(ContentDeserializer::new(key)).map(Some);
} }
@@ -2922,24 +2922,23 @@ where
} }
} }
/// Checks if first element of the specified pair matches one of the key from /// Claims one key-value pair from a FlatMapDeserializer's field buffer if the
/// `keys` parameter and if this is true, takes it from the option and returns. /// field name matches any of the recognized ones.
/// Otherwise, or if `item` already empty, returns `None`.
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
fn use_item<'de>( fn flat_map_take_entry<'de>(
item: &mut Option<(Content<'de>, Content<'de>)>, entry: &mut Option<(Content<'de>, Content<'de>)>,
keys: &[&str], recognized: &[&str],
) -> Option<(Content<'de>, Content<'de>)> { ) -> Option<(Content<'de>, Content<'de>)> {
// items in the vector are nulled out when used. So we can only use // Entries in the FlatMapDeserializer buffer are nulled out as they get
// an item if it's still filled in and if the field is one we care // claimed for deserialization. We only use an entry if it is still present
// about. // and if the field is one recognized by the current data structure.
let use_item = match *item { let is_recognized = match entry {
None => false, None => false,
Some((ref c, _)) => c.as_str().map_or(false, |key| keys.contains(&key)), Some((k, _v)) => k.as_str().map_or(false, |name| recognized.contains(&name)),
}; };
if use_item { if is_recognized {
item.take() entry.take()
} else { } else {
None None
} }