Skip to content

Commit 76d6428

Browse files
author
bors-servo
committed
Auto merge of #125 - servo:no-plugin, r=asajeffrey
Merge into a single crate. Use macros even on unstable. Breaking changes: * `ns!("")` should be written `ns!()` * Other `ns!(…)` macros should be lowercase, unquoted. Fixes #124 Closes #123 r? @asajeffrey <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/string-cache/125) <!-- Reviewable:end -->
2 parents 5f0302c + 4d505d6 commit 76d6428

File tree

16 files changed

+312
-541
lines changed

16 files changed

+312
-541
lines changed

Cargo.toml

+6-13
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ doctest = false
2222
log-events = ["rustc-serialize"]
2323

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

2727
# HeapSizeOf support
2828
heap_size = ["heapsize", "heapsize_plugin"]
2929

3030
[dependencies]
3131
lazy_static = "0.1.10"
3232
serde = "0.6"
33+
phf_shared = "0.7.4"
34+
debug_unreachable = "0.0.6"
3335

3436
[dev-dependencies]
3537
rand = "0"
@@ -38,15 +40,6 @@ rand = "0"
3840
version = "0"
3941
optional = true
4042

41-
[dependencies.string_cache_plugin]
42-
path = "plugin"
43-
version = "0.1.9"
44-
optional = true
45-
46-
[dependencies.string_cache_shared]
47-
path = "shared"
48-
version = "0.1.9"
49-
5043
[dependencies.heapsize]
5144
version = "0.1.1"
5245
optional = true
@@ -55,6 +48,6 @@ optional = true
5548
version = "0.1.0"
5649
optional = true
5750

58-
[build-dependencies.string_cache_shared]
59-
path = "shared"
60-
version = "0.1.9"
51+
[build-dependencies]
52+
phf_generator = "0.7.4"
53+
phf_shared = "0.7.4"

build.rs

+54-41
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,73 @@
1-
extern crate string_cache_shared;
1+
extern crate phf_shared;
2+
extern crate phf_generator;
23

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

57
use std::env;
6-
use std::ascii::AsciiExt;
78
use std::fs::File;
89
use std::io::{BufWriter, Write};
10+
use std::mem;
911
use std::path::Path;
12+
use std::slice;
1013

1114
fn main() {
12-
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("ns_atom_macros_without_plugin.rs");
13-
let mut file = BufWriter::new(File::create(&path).unwrap());
14-
writeln!(file, r"#[macro_export]").unwrap();
15-
writeln!(file, r"macro_rules! ns {{").unwrap();
16-
writeln!(file, "(\"\") => {{ $crate::Namespace({}) }};", atom("")).unwrap();
17-
for &(prefix, url) in ALL_NS {
18-
if !prefix.is_empty() {
19-
generate_combination("".to_owned(), prefix, url, &mut file);
20-
}
21-
}
22-
writeln!(file, r"}}").unwrap();
15+
let hash_state = generate();
16+
write_static_atom_set(&hash_state);
17+
write_atom_macro(&hash_state);
18+
}
2319

24-
writeln!(file, r"#[macro_export]").unwrap();
25-
writeln!(file, r"macro_rules! atom {{").unwrap();
26-
for &s in STATIC_ATOM_SET.iter() {
27-
if is_ident(s) {
28-
writeln!(file, r"( {} ) => {{ {} }};", s, atom(s)).unwrap();
20+
fn generate() -> phf_generator::HashState {
21+
let mut set = std::collections::HashSet::new();
22+
for atom in static_atom_list::ATOMS {
23+
if !set.insert(atom) {
24+
panic!("duplicate static atom `{:?}`", atom);
2925
}
30-
writeln!(file, r"({:?}) => {{ {} }};", s, atom(s)).unwrap();
3126
}
32-
writeln!(file, r"}}").unwrap();
27+
phf_generator::generate_hash(static_atom_list::ATOMS)
3328
}
3429

35-
fn generate_combination(prefix1: String, suffix: &str, url: &str, file: &mut BufWriter<File>) {
36-
if suffix.is_empty() {
37-
writeln!(file, r"({:?}) => {{ $crate::Namespace({}) }};", prefix1, atom(url)).unwrap();
38-
writeln!(file, r"( {} ) => {{ $crate::Namespace({}) }};", prefix1, atom(url)).unwrap();
39-
} else {
40-
let prefix2 = prefix1.clone();
41-
generate_combination(prefix1 + &*suffix[..1].to_ascii_lowercase(), &suffix[1..], url, file);
42-
generate_combination(prefix2 + &*suffix[..1].to_ascii_uppercase(), &suffix[1..], url, file);
30+
fn write_static_atom_set(hash_state: &phf_generator::HashState) {
31+
let path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("static_atom_set.rs");
32+
let mut file = BufWriter::new(File::create(&path).unwrap());
33+
macro_rules! w {
34+
($($arg: expr),+) => { (writeln!(&mut file, $($arg),+).unwrap()) }
35+
}
36+
w!("pub static STATIC_ATOM_SET: StaticAtomSet = StaticAtomSet {{");
37+
w!(" key: {},", hash_state.key);
38+
w!(" disps: &[");
39+
for &(d1, d2) in &hash_state.disps {
40+
w!(" ({}, {}),", d1, d2);
41+
}
42+
w!(" ],");
43+
w!(" atoms: &[");
44+
for &idx in &hash_state.map {
45+
w!(" {:?},", static_atom_list::ATOMS[idx]);
4346
}
47+
w!(" ],");
48+
w!("}};");
4449
}
4550

46-
fn atom(s: &str) -> String {
47-
let data = pack_static(STATIC_ATOM_SET.get_index_or_hash(s).unwrap() as u32);
48-
format!("$crate::Atom {{ data: 0x{:x} }}", data)
51+
fn write_atom_macro(hash_state: &phf_generator::HashState) {
52+
let set = shared::StaticAtomSet {
53+
key: hash_state.key,
54+
disps: leak(hash_state.disps.clone()),
55+
atoms: leak(hash_state.map.iter().map(|&idx| static_atom_list::ATOMS[idx]).collect()),
56+
};
57+
58+
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("atom_macro.rs");
59+
let mut file = BufWriter::new(File::create(&path).unwrap());
60+
writeln!(file, r"#[macro_export]").unwrap();
61+
writeln!(file, r"macro_rules! atom {{").unwrap();
62+
for &s in set.iter() {
63+
let data = shared::pack_static(set.get_index_or_hash(s).unwrap() as u32);
64+
writeln!(file, r"({:?}) => {{ $crate::Atom {{ data: 0x{:x} }} }};", s, data).unwrap();
65+
}
66+
writeln!(file, r"}}").unwrap();
4967
}
5068

51-
fn is_ident(s: &str) -> bool {
52-
let mut chars = s.chars();
53-
!s.is_empty() && match chars.next().unwrap() {
54-
'a'...'z' | 'A'...'Z' | '_' => true,
55-
_ => false
56-
} && chars.all(|c| match c {
57-
'a'...'z' | 'A'...'Z' | '_' | '0'...'9' => true,
58-
_ => false
59-
})
69+
fn leak<T>(v: Vec<T>) -> &'static [T] {
70+
let slice = unsafe { slice::from_raw_parts(v.as_ptr(), v.len()) };
71+
mem::forget(v);
72+
slice
6073
}

examples/summarize-events/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ authors = [ "The Servo Project Developers" ]
77
[dependencies]
88
csv = "0"
99
rustc-serialize = "0"
10+
phf_shared = "0.7.4"
1011

1112
[dependencies.string_cache]
1213
path = "../.."
13-
14-
[dependencies.string_cache_shared]
15-
path = "../../shared"

examples/summarize-events/src/main.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99

1010
extern crate csv;
1111
extern crate string_cache;
12-
extern crate string_cache_shared;
1312
extern crate rustc_serialize;
13+
extern crate phf_shared;
14+
15+
#[path = "../../../src/shared.rs"]
16+
#[allow(dead_code)]
17+
mod shared;
1418

1519
use string_cache::Atom;
1620

@@ -35,18 +39,18 @@ enum Kind {
3539
impl Kind {
3640
fn from_tag(tag: u8) -> Kind {
3741
match tag {
38-
string_cache_shared::DYNAMIC_TAG => Kind::Dynamic,
39-
string_cache_shared::INLINE_TAG => Kind::Inline,
40-
string_cache_shared::STATIC_TAG => Kind::Static,
42+
shared::DYNAMIC_TAG => Kind::Dynamic,
43+
shared::INLINE_TAG => Kind::Inline,
44+
shared::STATIC_TAG => Kind::Static,
4145
_ => panic!()
4246
}
4347
}
4448

4549
fn to_tag(self) -> u8 {
4650
match self {
47-
Kind::Dynamic => string_cache_shared::DYNAMIC_TAG,
48-
Kind::Inline => string_cache_shared::INLINE_TAG,
49-
Kind::Static => string_cache_shared::STATIC_TAG,
51+
Kind::Dynamic => shared::DYNAMIC_TAG,
52+
Kind::Inline => shared::INLINE_TAG,
53+
Kind::Static => shared::STATIC_TAG,
5054
}
5155
}
5256
}
@@ -77,10 +81,10 @@ fn main() {
7781
match &ev.event[..] {
7882
"intern" => {
7983
let tag = (ev.id & 0xf) as u8;
80-
assert!(tag <= string_cache_shared::STATIC_TAG);
84+
assert!(tag <= shared::STATIC_TAG);
8185

8286
let string = match tag {
83-
string_cache_shared::DYNAMIC_TAG => dynamic[&ev.id].clone(),
87+
shared::DYNAMIC_TAG => dynamic[&ev.id].clone(),
8488

8589
// FIXME: We really shouldn't be allowed to do this. It's a memory-safety
8690
// hazard; the field is only public for the atom!() macro.

plugin/Cargo.toml

-21
This file was deleted.

plugin/src/atom/mod.rs

-101
This file was deleted.

plugin/src/lib.rs

-38
This file was deleted.

0 commit comments

Comments
 (0)