Skip to content

Commit 1d11f03

Browse files
committed
Extract logic of taking flattened fields into a function
1 parent e11d01f commit 1d11f03

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

serde/src/private/de.rs

+25-20
Original file line numberDiff line numberDiff line change
@@ -2751,16 +2751,7 @@ where
27512751
V: Visitor<'de>,
27522752
{
27532753
for item in self.0.iter_mut() {
2754-
// items in the vector are nulled out when used. So we can only use
2755-
// an item if it's still filled in and if the field is one we care
2756-
// about.
2757-
let use_item = match *item {
2758-
None => false,
2759-
Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)),
2760-
};
2761-
2762-
if use_item {
2763-
let (key, value) = item.take().unwrap();
2754+
if let Some((key, value)) = use_item(item, variants) {
27642755
return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
27652756
}
27662757
}
@@ -2912,16 +2903,7 @@ where
29122903
T: DeserializeSeed<'de>,
29132904
{
29142905
while let Some(item) = self.iter.next() {
2915-
// items in the vector are nulled out when used. So we can only use
2916-
// an item if it's still filled in and if the field is one we care
2917-
// about. In case we do not know which fields we want, we take them all.
2918-
let use_item = match *item {
2919-
None => false,
2920-
Some((ref c, _)) => c.as_str().map_or(false, |key| self.fields.contains(&key)),
2921-
};
2922-
2923-
if use_item {
2924-
let (key, content) = item.take().unwrap();
2906+
if let Some((key, content)) = use_item(item, self.fields) {
29252907
self.pending_content = Some(content);
29262908
return seed.deserialize(ContentDeserializer::new(key)).map(Some);
29272909
}
@@ -2939,3 +2921,26 @@ where
29392921
}
29402922
}
29412923
}
2924+
2925+
/// Checks if first element of the specified pair matches one of the key from
2926+
/// `keys` parameter and if this is true, takes it from the option and returns.
2927+
/// Otherwise, or if `item` already empty, returns `None`.
2928+
#[cfg(any(feature = "std", feature = "alloc"))]
2929+
fn use_item<'de>(
2930+
item: &mut Option<(Content<'de>, Content<'de>)>,
2931+
keys: &[&str],
2932+
) -> Option<(Content<'de>, Content<'de>)> {
2933+
// items in the vector are nulled out when used. So we can only use
2934+
// an item if it's still filled in and if the field is one we care
2935+
// about.
2936+
let use_item = match *item {
2937+
None => false,
2938+
Some((ref c, _)) => c.as_str().map_or(false, |key| keys.contains(&key)),
2939+
};
2940+
2941+
if use_item {
2942+
item.take()
2943+
} else {
2944+
None
2945+
}
2946+
}

0 commit comments

Comments
 (0)