From f6fe766c3a8edf58afdfa69c273108052c9b9b32 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Fri, 20 Oct 2023 13:09:10 -0500 Subject: [PATCH] Optimize tick trimming (#2689) --- crates/libs/bindgen/src/lib.rs | 8 -------- crates/libs/bindgen/src/tokens/mod.rs | 2 +- crates/libs/metadata/src/lib.rs | 8 -------- crates/libs/metadata/src/reader.rs | 2 +- crates/libs/metadata/src/tables.rs | 12 ++++++++++-- crates/libs/metadata/src/type_name.rs | 4 +--- crates/tests/standalone/build.rs | 2 +- 7 files changed, 14 insertions(+), 24 deletions(-) diff --git a/crates/libs/bindgen/src/lib.rs b/crates/libs/bindgen/src/lib.rs index 1ab31d442e..5a8b5ef1f7 100644 --- a/crates/libs/bindgen/src/lib.rs +++ b/crates/libs/bindgen/src/lib.rs @@ -257,11 +257,3 @@ fn extension(path: &str) -> &str { fn directory(path: &str) -> &str { path.rsplit_once(&['/', '\\']).map_or("", |(directory, _)| directory) } - -fn trim_tick(name: &str) -> &str { - if name.as_bytes().iter().rev().nth(1) == Some(&b'`') { - &name[..name.len() - 2] - } else { - name - } -} diff --git a/crates/libs/bindgen/src/tokens/mod.rs b/crates/libs/bindgen/src/tokens/mod.rs index e5f019a8b9..12a54142fa 100644 --- a/crates/libs/bindgen/src/tokens/mod.rs +++ b/crates/libs/bindgen/src/tokens/mod.rs @@ -438,6 +438,6 @@ pub fn to_ident(name: &str) -> TokenStream { "abstract" | "as" | "become" | "box" | "break" | "const" | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut" | "override" | "priv" | "pub" | "ref" | "return" | "static" | "struct" | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while" | "yield" | "try" | "async" | "await" | "dyn" => format!("r#{name}").into(), "Self" | "self" => format!("{name}_").into(), "_" => "unused".into(), - _ => crate::trim_tick(name).into(), + _ => name.into(), } } diff --git a/crates/libs/metadata/src/lib.rs b/crates/libs/metadata/src/lib.rs index 2484783510..15220b1783 100644 --- a/crates/libs/metadata/src/lib.rs +++ b/crates/libs/metadata/src/lib.rs @@ -73,14 +73,6 @@ pub fn coded_index_size(tables: &[usize]) -> usize { } } -fn trim_tick(name: &str) -> &str { - if name.as_bytes().iter().rev().nth(1) == Some(&b'`') { - &name[..name.len() - 2] - } else { - name - } -} - #[derive(Debug)] pub enum Value { Bool(bool), diff --git a/crates/libs/metadata/src/reader.rs b/crates/libs/metadata/src/reader.rs index d60c98ba95..3445a7ad2b 100644 --- a/crates/libs/metadata/src/reader.rs +++ b/crates/libs/metadata/src/reader.rs @@ -52,7 +52,7 @@ impl Reader { namespace_items.entry(field.name()).or_default().push(Item::Const(field)); } } else { - namespace_items.entry(trim_tick(name)).or_default().push(Item::Type(def)); + namespace_items.entry(name).or_default().push(Item::Type(def)); // TODO: these should all be fields on the Apis class so we don't have to go looking for all of these as well. if def.extends() == Some(TypeName::Enum) && !def.flags().contains(TypeAttributes::WindowsRuntime) && !def.has_attribute("ScopedEnumAttribute") { diff --git a/crates/libs/metadata/src/tables.rs b/crates/libs/metadata/src/tables.rs index 4efce73d81..d7e3ad6086 100644 --- a/crates/libs/metadata/src/tables.rs +++ b/crates/libs/metadata/src/tables.rs @@ -298,7 +298,7 @@ impl TypeDef { } pub fn name(&self) -> &'static str { - self.str(1) + trim_tick(self.str(1)) } pub fn namespace(&self) -> &'static str { @@ -395,7 +395,7 @@ impl TypeDef { impl TypeRef { pub fn name(&self) -> &'static str { - self.str(1) + trim_tick(self.str(1)) } pub fn namespace(&self) -> &'static str { @@ -410,3 +410,11 @@ impl TypeRef { self.decode(0) } } + +fn trim_tick(name: &str) -> &str { + if name.as_bytes().iter().rev().nth(1) == Some(&b'`') { + &name[..name.len() - 2] + } else { + name + } +} diff --git a/crates/libs/metadata/src/type_name.rs b/crates/libs/metadata/src/type_name.rs index 2740b153d4..431e0f3f97 100644 --- a/crates/libs/metadata/src/type_name.rs +++ b/crates/libs/metadata/src/type_name.rs @@ -1,7 +1,5 @@ #![allow(non_upper_case_globals)] -use super::*; - #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] pub struct TypeName { pub namespace: &'static str, @@ -54,7 +52,7 @@ impl TypeName { } pub fn new(namespace: &'static str, name: &'static str) -> Self { - Self { namespace, name: trim_tick(name) } + Self { namespace, name } } pub fn parse(full_name: &'static str) -> Self { diff --git a/crates/tests/standalone/build.rs b/crates/tests/standalone/build.rs index dd15b53220..8af6e7a589 100644 --- a/crates/tests/standalone/build.rs +++ b/crates/tests/standalone/build.rs @@ -165,7 +165,7 @@ fn write_no_inner_attr(output: &str, filter: &[&str]) { } fn riddle(output: &str, filter: &[&str], config: &[&str]) { - std::fs::remove_file(output).expect("Failed to delete output"); + _ = std::fs::remove_file(output); let mut command = std::process::Command::new("cargo");