Skip to content

Commit

Permalink
Extract logic of taking flattened fields into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed May 8, 2023
1 parent e11d01f commit 1d11f03
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2751,16 +2751,7 @@ where
V: Visitor<'de>,
{
for item in self.0.iter_mut() {
// items in the vector are nulled out when used. So we can only use
// an item if it's still filled in and if the field is one we care
// about.
let use_item = match *item {
None => false,
Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)),
};

if use_item {
let (key, value) = item.take().unwrap();
if let Some((key, value)) = use_item(item, variants) {
return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
}
}
Expand Down Expand Up @@ -2912,16 +2903,7 @@ where
T: DeserializeSeed<'de>,
{
while let Some(item) = self.iter.next() {
// items in the vector are nulled out when used. So we can only use
// an item if it's still filled in and if the field is one we care
// about. In case we do not know which fields we want, we take them all.
let use_item = match *item {
None => false,
Some((ref c, _)) => c.as_str().map_or(false, |key| self.fields.contains(&key)),
};

if use_item {
let (key, content) = item.take().unwrap();
if let Some((key, content)) = use_item(item, self.fields) {
self.pending_content = Some(content);
return seed.deserialize(ContentDeserializer::new(key)).map(Some);
}
Expand All @@ -2939,3 +2921,26 @@ where
}
}
}

/// Checks if first element of the specified pair matches one of the key from
/// `keys` parameter and if this is true, takes it from the option and returns.
/// Otherwise, or if `item` already empty, returns `None`.
#[cfg(any(feature = "std", feature = "alloc"))]
fn use_item<'de>(
item: &mut Option<(Content<'de>, Content<'de>)>,
keys: &[&str],
) -> Option<(Content<'de>, Content<'de>)> {
// items in the vector are nulled out when used. So we can only use
// an item if it's still filled in and if the field is one we care
// about.
let use_item = match *item {
None => false,
Some((ref c, _)) => c.as_str().map_or(false, |key| keys.contains(&key)),
};

if use_item {
item.take()
} else {
None
}
}

0 comments on commit 1d11f03

Please sign in to comment.