-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and use new string-cache-codegen crate
- Loading branch information
1 parent
e0b355e
commit a9efc44
Showing
10 changed files
with
189 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,14 @@ | ||
extern crate phf_generator; | ||
extern crate string_cache_codegen; | ||
|
||
#[path = "src/shared.rs"] #[allow(dead_code)] mod shared; | ||
#[path = "src/static_atom_list.rs"] mod static_atom_list; | ||
#[path = "src/static_atom_list.rs"] | ||
mod static_atom_list; | ||
|
||
use std::env; | ||
use std::fs::File; | ||
use std::io::{BufWriter, Write}; | ||
use std::path::Path; | ||
|
||
fn main() { | ||
let hash_state = generate(); | ||
write_static_atom_set(&hash_state); | ||
write_atom_macro(&hash_state); | ||
} | ||
|
||
fn generate() -> phf_generator::HashState { | ||
let mut set = std::collections::HashSet::new(); | ||
for atom in static_atom_list::ATOMS { | ||
if !set.insert(atom) { | ||
panic!("duplicate static atom `{:?}`", atom); | ||
} | ||
} | ||
phf_generator::generate_hash(static_atom_list::ATOMS) | ||
} | ||
|
||
fn write_static_atom_set(hash_state: &phf_generator::HashState) { | ||
let path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("static_atom_set.rs"); | ||
let mut file = BufWriter::new(File::create(&path).unwrap()); | ||
macro_rules! w { | ||
($($arg: expr),+) => { (writeln!(&mut file, $($arg),+).unwrap()) } | ||
} | ||
w!("pub static STATIC_ATOM_SET: PhfStrSet = PhfStrSet {{"); | ||
w!(" key: {},", hash_state.key); | ||
w!(" disps: &["); | ||
for &(d1, d2) in &hash_state.disps { | ||
w!(" ({}, {}),", d1, d2); | ||
} | ||
w!(" ],"); | ||
w!(" atoms: &["); | ||
for &idx in &hash_state.map { | ||
w!(" {:?},", static_atom_list::ATOMS[idx]); | ||
} | ||
w!(" ],"); | ||
w!("}};"); | ||
} | ||
|
||
fn write_atom_macro(hash_state: &phf_generator::HashState) { | ||
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("atom_macro.rs"); | ||
let mut file = BufWriter::new(File::create(&path).unwrap()); | ||
writeln!(file, r"#[macro_export]").unwrap(); | ||
writeln!(file, r"macro_rules! atom {{").unwrap(); | ||
for (i, &idx) in hash_state.map.iter().enumerate() { | ||
writeln!( | ||
file, | ||
r"({:?}) => {{ $crate::Atom {{ unsafe_data: 0x{:x}, phantom: ::std::marker::PhantomData }} }};", | ||
static_atom_list::ATOMS[idx], | ||
shared::pack_static(i as u32), | ||
).unwrap(); | ||
} | ||
writeln!(file, r"}}").unwrap(); | ||
string_cache_codegen::AtomType::new("atom::tests::TestAtom", "test_atom!") | ||
.atoms(static_atom_list::ATOMS) | ||
.write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("test_atom.rs")) | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
|
||
name = "string_cache_codegen" | ||
version = "0.3.0" | ||
authors = [ "The Servo Project Developers" ] | ||
description = "A codegen library for string-cache, developed as part of the Servo project." | ||
license = "MIT / Apache-2.0" | ||
repository = "https://github.com/servo/string-cache" | ||
documentation = "https://docs.rs/string_cache_codegen/" | ||
|
||
[lib] | ||
name = "string_cache_codegen" | ||
path = "lib.rs" | ||
|
||
[dependencies] | ||
phf_generator = "0.7.15" |
Oops, something went wrong.