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

Merge into a single crate. Use macros even on unstable. #125

Merged
merged 2 commits into from
Nov 23, 2015
Merged
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
19 changes: 6 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ doctest = false
log-events = ["rustc-serialize"]

# Use unstable features to optimize space and time (memory and CPU usage).
unstable = ["string_cache_plugin"]
unstable = []

# HeapSizeOf support
heap_size = ["heapsize", "heapsize_plugin"]

[dependencies]
lazy_static = "0.1.10"
serde = "0.6"
phf_shared = "0.7.4"
debug_unreachable = "0.0.6"

[dev-dependencies]
rand = "0"
Expand All @@ -38,15 +40,6 @@ rand = "0"
version = "0"
optional = true

[dependencies.string_cache_plugin]
path = "plugin"
version = "0.1.9"
optional = true

[dependencies.string_cache_shared]
path = "shared"
version = "0.1.9"

[dependencies.heapsize]
version = "0.1.1"
optional = true
Expand All @@ -55,6 +48,6 @@ optional = true
version = "0.1.0"
optional = true

[build-dependencies.string_cache_shared]
path = "shared"
version = "0.1.9"
[build-dependencies]
phf_generator = "0.7.4"
phf_shared = "0.7.4"
95 changes: 54 additions & 41 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,73 @@
extern crate string_cache_shared;
extern crate phf_shared;
extern crate phf_generator;

use string_cache_shared::{STATIC_ATOM_SET, ALL_NS, pack_static};
#[path = "src/shared.rs"] #[allow(dead_code)] mod shared;
#[path = "src/static_atom_list.rs"] mod static_atom_list;

use std::env;
use std::ascii::AsciiExt;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::mem;
use std::path::Path;
use std::slice;

fn main() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("ns_atom_macros_without_plugin.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
writeln!(file, r"#[macro_export]").unwrap();
writeln!(file, r"macro_rules! ns {{").unwrap();
writeln!(file, "(\"\") => {{ $crate::Namespace({}) }};", atom("")).unwrap();
for &(prefix, url) in ALL_NS {
if !prefix.is_empty() {
generate_combination("".to_owned(), prefix, url, &mut file);
}
}
writeln!(file, r"}}").unwrap();
let hash_state = generate();
write_static_atom_set(&hash_state);
write_atom_macro(&hash_state);
}

writeln!(file, r"#[macro_export]").unwrap();
writeln!(file, r"macro_rules! atom {{").unwrap();
for &s in STATIC_ATOM_SET.iter() {
if is_ident(s) {
writeln!(file, r"( {} ) => {{ {} }};", s, atom(s)).unwrap();
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);
}
writeln!(file, r"({:?}) => {{ {} }};", s, atom(s)).unwrap();
}
writeln!(file, r"}}").unwrap();
phf_generator::generate_hash(static_atom_list::ATOMS)
}

fn generate_combination(prefix1: String, suffix: &str, url: &str, file: &mut BufWriter<File>) {
if suffix.is_empty() {
writeln!(file, r"({:?}) => {{ $crate::Namespace({}) }};", prefix1, atom(url)).unwrap();
writeln!(file, r"( {} ) => {{ $crate::Namespace({}) }};", prefix1, atom(url)).unwrap();
} else {
let prefix2 = prefix1.clone();
generate_combination(prefix1 + &*suffix[..1].to_ascii_lowercase(), &suffix[1..], url, file);
generate_combination(prefix2 + &*suffix[..1].to_ascii_uppercase(), &suffix[1..], url, file);
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: StaticAtomSet = StaticAtomSet {{");
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 atom(s: &str) -> String {
let data = pack_static(STATIC_ATOM_SET.get_index_or_hash(s).unwrap() as u32);
format!("$crate::Atom {{ data: 0x{:x} }}", data)
fn write_atom_macro(hash_state: &phf_generator::HashState) {
let set = shared::StaticAtomSet {
key: hash_state.key,
disps: leak(hash_state.disps.clone()),
atoms: leak(hash_state.map.iter().map(|&idx| static_atom_list::ATOMS[idx]).collect()),
};

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 &s in set.iter() {
let data = shared::pack_static(set.get_index_or_hash(s).unwrap() as u32);
writeln!(file, r"({:?}) => {{ $crate::Atom {{ data: 0x{:x} }} }};", s, data).unwrap();
}
writeln!(file, r"}}").unwrap();
}

fn is_ident(s: &str) -> bool {
let mut chars = s.chars();
!s.is_empty() && match chars.next().unwrap() {
'a'...'z' | 'A'...'Z' | '_' => true,
_ => false
} && chars.all(|c| match c {
'a'...'z' | 'A'...'Z' | '_' | '0'...'9' => true,
_ => false
})
fn leak<T>(v: Vec<T>) -> &'static [T] {
let slice = unsafe { slice::from_raw_parts(v.as_ptr(), v.len()) };
mem::forget(v);
slice
}
4 changes: 1 addition & 3 deletions examples/summarize-events/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ authors = [ "The Servo Project Developers" ]
[dependencies]
csv = "0"
rustc-serialize = "0"
phf_shared = "0.7.4"

[dependencies.string_cache]
path = "../.."

[dependencies.string_cache_shared]
path = "../../shared"
22 changes: 13 additions & 9 deletions examples/summarize-events/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

extern crate csv;
extern crate string_cache;
extern crate string_cache_shared;
extern crate rustc_serialize;
extern crate phf_shared;

#[path = "../../../src/shared.rs"]
#[allow(dead_code)]
mod shared;

use string_cache::Atom;

Expand All @@ -35,18 +39,18 @@ enum Kind {
impl Kind {
fn from_tag(tag: u8) -> Kind {
match tag {
string_cache_shared::DYNAMIC_TAG => Kind::Dynamic,
string_cache_shared::INLINE_TAG => Kind::Inline,
string_cache_shared::STATIC_TAG => Kind::Static,
shared::DYNAMIC_TAG => Kind::Dynamic,
shared::INLINE_TAG => Kind::Inline,
shared::STATIC_TAG => Kind::Static,
_ => panic!()
}
}

fn to_tag(self) -> u8 {
match self {
Kind::Dynamic => string_cache_shared::DYNAMIC_TAG,
Kind::Inline => string_cache_shared::INLINE_TAG,
Kind::Static => string_cache_shared::STATIC_TAG,
Kind::Dynamic => shared::DYNAMIC_TAG,
Kind::Inline => shared::INLINE_TAG,
Kind::Static => shared::STATIC_TAG,
}
}
}
Expand Down Expand Up @@ -77,10 +81,10 @@ fn main() {
match &ev.event[..] {
"intern" => {
let tag = (ev.id & 0xf) as u8;
assert!(tag <= string_cache_shared::STATIC_TAG);
assert!(tag <= shared::STATIC_TAG);

let string = match tag {
string_cache_shared::DYNAMIC_TAG => dynamic[&ev.id].clone(),
shared::DYNAMIC_TAG => dynamic[&ev.id].clone(),

// FIXME: We really shouldn't be allowed to do this. It's a memory-safety
// hazard; the field is only public for the atom!() macro.
Expand Down
21 changes: 0 additions & 21 deletions plugin/Cargo.toml

This file was deleted.

101 changes: 0 additions & 101 deletions plugin/src/atom/mod.rs

This file was deleted.

38 changes: 0 additions & 38 deletions plugin/src/lib.rs

This file was deleted.

Loading