Skip to content

Commit

Permalink
Optimize tick trimming (#2689)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Oct 20, 2023
1 parent ec66a9b commit f6fe766
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 24 deletions.
8 changes: 0 additions & 8 deletions crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/tokens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
8 changes: 0 additions & 8 deletions crates/libs/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/metadata/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
12 changes: 10 additions & 2 deletions crates/libs/metadata/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
4 changes: 1 addition & 3 deletions crates/libs/metadata/src/type_name.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/standalone/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit f6fe766

Please sign in to comment.