Keep deserialize aliases as a sorted set

This commit is contained in:
David Tolnay 2023-08-05 16:56:54 -07:00
parent da55ed7e8d
commit 4d93e9f44c
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 18 additions and 32 deletions

View File

@ -2001,7 +2001,7 @@ fn deserialize_untagged_newtype_variant(
}
fn deserialize_generated_identifier(
fields: &[(&str, Ident, &[String])],
fields: &[(&str, Ident, &BTreeSet<String>)],
cattrs: &attr::Container,
is_variant: bool,
other_idx: Option<usize>,
@ -2187,7 +2187,7 @@ fn deserialize_custom_identifier(
fn deserialize_identifier(
this_value: &TokenStream,
fields: &[(&str, Ident, &[String])],
fields: &[(&str, Ident, &BTreeSet<String>)],
is_variant: bool,
fallthrough: Option<TokenStream>,
fallthrough_borrowed: Option<TokenStream>,

View File

@ -134,7 +134,7 @@ pub struct Name {
serialize_renamed: bool,
deserialize: String,
deserialize_renamed: bool,
deserialize_aliases: Vec<String>,
deserialize_aliases: BTreeSet<String>,
}
fn unraw(ident: &Ident) -> String {
@ -148,16 +148,12 @@ impl Name {
de_name: Attr<String>,
de_aliases: Option<VecAttr<String>>,
) -> Name {
let deserialize_aliases = match de_aliases {
Some(de_aliases) => {
let mut alias_list = BTreeSet::new();
for alias_name in de_aliases.get() {
alias_list.insert(alias_name);
}
alias_list.into_iter().collect()
let mut alias_set = BTreeSet::new();
if let Some(de_aliases) = de_aliases {
for alias_name in de_aliases.get() {
alias_set.insert(alias_name);
}
None => Vec::new(),
};
}
let ser_name = ser_name.get();
let ser_renamed = ser_name.is_some();
@ -168,7 +164,7 @@ impl Name {
serialize_renamed: ser_renamed,
deserialize: de_name.unwrap_or(source_name),
deserialize_renamed: de_renamed,
deserialize_aliases,
deserialize_aliases: alias_set,
}
}
@ -182,23 +178,9 @@ impl Name {
&self.deserialize
}
fn deserialize_aliases(&self) -> &[String] {
fn deserialize_aliases(&self) -> &BTreeSet<String> {
&self.deserialize_aliases
}
fn insert_deserialize_name_into_aliases(&mut self) {
// `deserialize_aliases` was constructed from a BTreeSet, so it is
// sorted and does not contain duplicates.
//
// In `Name::from_attrs` it's too early to insert the field's real name
// because rename_all rules have not yet gotten applied at that point.
match self.deserialize_aliases.binary_search(&self.deserialize) {
Ok(_) => {} // already present
Err(pos) => self
.deserialize_aliases
.insert(pos, self.deserialize.clone()),
}
}
}
#[derive(Copy, Clone)]
@ -986,7 +968,7 @@ impl Variant {
&self.name
}
pub fn aliases(&self) -> &[String] {
pub fn aliases(&self) -> &BTreeSet<String> {
self.name.deserialize_aliases()
}
@ -997,7 +979,9 @@ impl Variant {
if !self.name.deserialize_renamed {
self.name.deserialize = rules.deserialize.apply_to_variant(&self.name.deserialize);
}
self.name.insert_deserialize_name_into_aliases();
self.name
.deserialize_aliases
.insert(self.name.deserialize.clone());
}
pub fn rename_all_rules(&self) -> RenameAllRules {
@ -1326,7 +1310,7 @@ impl Field {
&self.name
}
pub fn aliases(&self) -> &[String] {
pub fn aliases(&self) -> &BTreeSet<String> {
self.name.deserialize_aliases()
}
@ -1337,7 +1321,9 @@ impl Field {
if !self.name.deserialize_renamed {
self.name.deserialize = rules.deserialize.apply_to_field(&self.name.deserialize);
}
self.name.insert_deserialize_name_into_aliases();
self.name
.deserialize_aliases
.insert(self.name.deserialize.clone());
}
pub fn skip_serializing(&self) -> bool {