Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compile time and introduce features #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions x11-keysymdef/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ lazy_static = "1.3.0"
[build-dependencies]
serde_json = "1.0.40"
serde = { version = "1.0.97", features = ["derive"] }

[features]
default = ["by_name", "by_codepoint", "by_keysym" ]
by_name = []
by_codepoint = []
by_keysym = []
95 changes: 52 additions & 43 deletions x11-keysymdef/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,65 +56,74 @@ fn from_json() {
writeln!(&mut file, "::lazy_static::lazy_static! {{").unwrap();

// First map: Index by name
writeln!(
&mut file,
"static ref BY_NAMES: ::std::collections::HashMap<&'static str, &'static Record> = {{"
)
.unwrap();
writeln!(&mut file, "let mut map = ::std::collections::HashMap::new();").unwrap();
for (index, r) in records.iter().enumerate() {
// multiple names per record possible, add one entry for each
for name in &r.names {
// Use unchecked access here because we just built the array of records
// with exactly these indices.
writeln!(
&mut file,
r#"map.entry("{}").or_insert(unsafe {{ RECORDS.get_unchecked({}) }});"#,
name, index
)
.unwrap();
#[cfg(feature = "by_name")]
{
writeln!(
&mut file,
"static ref BY_NAMES: ::std::collections::HashMap<&'static str, &'static Record> = {{"
)
.unwrap();
writeln!(&mut file, "::std::collections::HashMap::from([").unwrap();
for (index, r) in records.iter().enumerate() {
// multiple names per record possible, add one entry for each
for name in &r.names {
// Use unchecked access here because we just built the array of records
// with exactly these indices.
writeln!(
&mut file,
r#"("{}", unsafe {{ RECORDS.get_unchecked({}) }}),"#,
name, index
)
.unwrap();
}
}
writeln!(&mut file, "])").unwrap();
writeln!(&mut file, "}};").unwrap();
}
writeln!(&mut file, "map").unwrap();
writeln!(&mut file, "}};").unwrap();

// Next up: Index by codepoint
writeln!(
&mut file,
"static ref BY_CODEPOINT: std::collections::HashMap<char, &'static Record> = {{"
)
.unwrap();
writeln!(&mut file, "let mut map = std::collections::HashMap::new();").unwrap();

for (index, r) in records.iter().enumerate() {
#[cfg(feature = "by_codepoint")]
{
writeln!(
&mut file,
r#"map.entry('\u{{{:x}}}').or_insert(unsafe {{ RECORDS.get_unchecked({}) }});"#,
r.unicode, index
"static ref BY_CODEPOINT: std::collections::HashMap<char, &'static Record> = {{"
)
.unwrap();
writeln!(&mut file, "std::collections::HashMap::from([").unwrap();

for (index, r) in records.iter().enumerate() {
writeln!(
&mut file,
r#"('\u{{{:x}}}', unsafe {{ RECORDS.get_unchecked({}) }}),"#,
r.unicode, index
)
.unwrap();
}
writeln!(&mut file, "])").unwrap();
writeln!(&mut file, "}};").unwrap();
}
writeln!(&mut file, "map").unwrap();
writeln!(&mut file, "}};").unwrap();

// Last but not least: Index by keysym
writeln!(
&mut file,
"static ref BY_KEYSYM: std::collections::HashMap<u32, &'static Record> = {{"
)
.unwrap();
writeln!(&mut file, "let mut map = std::collections::HashMap::new();").unwrap();

for (index, r) in records.iter().enumerate() {
#[cfg(feature = "by_keysym")]
{
writeln!(
&mut file,
r#"map.entry({}).or_insert(unsafe {{ RECORDS.get_unchecked({}) }});"#,
r.keysym, index
"static ref BY_KEYSYM: std::collections::HashMap<u32, &'static Record> = {{"
)
.unwrap();
writeln!(&mut file, "std::collections::HashMap::from([").unwrap();

for (index, r) in records.iter().enumerate() {
writeln!(
&mut file,
r#"({}, unsafe {{ RECORDS.get_unchecked({}) }}),"#,
r.keysym, index
)
.unwrap();
}
writeln!(&mut file, "])").unwrap();
writeln!(&mut file, "}};").unwrap();
}
writeln!(&mut file, "map").unwrap();
writeln!(&mut file, "}};").unwrap();

// End of lazy_static
writeln!(&mut file, "}}").unwrap();
Expand Down
6 changes: 6 additions & 0 deletions x11-keysymdef/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@
include!(concat!(env!("OUT_DIR"), "/mapping.rs"));

/// Look up a record by the mnemonic macro name
#[cfg(feature = "by_name")]
pub fn lookup_by_name(name: &str) -> Option<&'static Record> {
BY_NAMES.get(&name).copied()
}

/// Look up a record by unicode char (unicode code point)
#[cfg(feature = "by_codepoint")]
pub fn lookup_by_codepoint(codepoint: char) -> Option<&'static Record> {
BY_CODEPOINT.get(&codepoint).copied()
}

/// Look up a mnemonic macro name by the keysym code
#[cfg(feature = "by_keysym")]
pub fn lookup_by_keysym(keysym: u32) -> Option<&'static Record> {
BY_KEYSYM.get(&keysym).copied()
}

#[test]
fn access_works() {
#[cfg(feature = "by_name")]
assert!(lookup_by_name("Uhorngrave").is_some());
#[cfg(feature = "by_codepoint")]
assert!(lookup_by_codepoint('\u{1EEA}').is_some());
#[cfg(feature = "by_keysym")]
assert!(lookup_by_keysym(0x1eea).is_some());
}