Skip to content

Commit 917dd80

Browse files
committed
Use phf_codegen instead of phf_macros
1 parent 5169a5b commit 917dd80

9 files changed

Lines changed: 35 additions & 37 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "string_cache"
4-
version = "0.1.6"
4+
version = "0.1.7"
55
authors = [ "The Servo Project Developers" ]
66
description = "A string interning library for Rust, developed as part of the Servo project."
77
license = "MIT / Apache-2.0"
@@ -22,8 +22,6 @@ log-events = ["rustc-serialize"]
2222

2323
[dependencies]
2424
rand = "0"
25-
phf = "0.7"
26-
phf_macros = "0.7"
2725
lazy_static = "0.1.10"
2826
serde = "0.4.2"
2927

plugin/src/atom/mod.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ use std::iter::Chain;
1818
use std::collections::HashMap;
1919
use std::ascii::AsciiExt;
2020

21-
mod data;
22-
23-
// Build a PhfOrderedSet of static atoms.
24-
// Takes no arguments.
25-
pub fn expand_static_atom_set(cx: &mut ExtCtxt, sp: Span, tt: &[TokenTree]) -> Box<MacResult+'static> {
26-
ext_bail_if!(tt.len() != 0, cx, sp, "Usage: static_atom_map!()");
27-
let tts: Vec<TokenTree> = data::ATOMS.iter().flat_map(|k| {
28-
(quote_tokens!(&mut *cx, $k,)).into_iter()
29-
}).collect();
30-
MacEager::expr(quote_expr!(&mut *cx, phf_ordered_set!($tts)))
31-
}
3221

3322
fn atom_tok_to_str(t: &TokenTree) -> Option<InternedString> {
3423
Some(get_ident(match *t {
@@ -38,17 +27,6 @@ fn atom_tok_to_str(t: &TokenTree) -> Option<InternedString> {
3827
}))
3928
}
4029

41-
// Build a map from atoms to IDs for use in implementing the atom!() macro.
42-
lazy_static! {
43-
static ref STATIC_ATOM_MAP: HashMap<&'static str, usize> = {
44-
let mut m = HashMap::new();
45-
for (i, x) in data::ATOMS.iter().enumerate() {
46-
m.insert(*x, i);
47-
}
48-
m
49-
};
50-
}
51-
5230
// FIXME: libsyntax should provide this (rust-lang/rust#17637)
5331
struct AtomResult {
5432
expr: P<ast::Expr>,
@@ -66,12 +44,12 @@ impl MacResult for AtomResult {
6644
}
6745

6846
fn make_atom_result(cx: &mut ExtCtxt, name: &str) -> Option<AtomResult> {
69-
let i = match STATIC_ATOM_MAP.get(name) {
47+
let i = match ::string_cache_shared::STATIC_ATOM_SET.get_index(name) {
7048
Some(i) => i,
7149
None => return None,
7250
};
7351

74-
let data = ::string_cache_shared::pack_static(*i as u32);
52+
let data = ::string_cache_shared::pack_static(i as u32);
7553

7654
Some(AtomResult {
7755
expr: quote_expr!(&mut *cx, ::string_cache::atom::Atom { data: $data }),

plugin/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ mod atom;
3333
// NB: This needs to be public or we get a linker error.
3434
#[plugin_registrar]
3535
pub fn plugin_registrar(reg: &mut Registry) {
36-
reg.register_macro("static_atom_set", atom::expand_static_atom_set);
3736
reg.register_macro("atom", atom::expand_atom);
3837
reg.register_macro("ns", atom::expand_ns);
3938
}

shared/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ authors = [ "The Servo Project Developers" ]
66
description = "A string interning library for Rust, developed as part of the Servo project − shared code between the compiler plugin and main crate."
77
license = "MIT / Apache-2.0"
88
repository = "https://github.com/servo/string-cache"
9+
build = "build.rs"
910

1011
[lib]
1112

@@ -14,3 +15,7 @@ path = "lib.rs"
1415

1516
[dependencies]
1617
debug_unreachable = "0.0.5"
18+
phf = "0.7.3"
19+
20+
[build-dependencies]
21+
phf_codegen = "0.7.3"

shared/build.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
extern crate phf_codegen;
2+
3+
mod static_atom_list;
4+
5+
use std::fs::File;
6+
use std::io::{BufWriter, Write};
7+
use std::path::Path;
8+
9+
fn main() {
10+
let mut set = phf_codegen::OrderedSet::new();
11+
for &atom in static_atom_list::ATOMS {
12+
set.entry(atom);
13+
}
14+
15+
let path = Path::new(env!("OUT_DIR")).join("static_atom_set.rs");
16+
let mut file = BufWriter::new(File::create(&path).unwrap());
17+
write!(&mut file, "pub static STATIC_ATOM_SET: phf::OrderedSet<&'static str> = ").unwrap();
18+
set.build(&mut file).unwrap();
19+
write!(&mut file, ";\n").unwrap();
20+
}

shared/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
#![deny(warnings)]
1515

1616
#[macro_use] extern crate debug_unreachable;
17+
extern crate phf;
1718

1819
use std::ptr;
1920
use std::slice;
2021

2122
pub use self::UnpackedAtom::{Dynamic, Inline, Static};
2223

24+
include!(concat!(env!("OUT_DIR"), "/static_atom_set.rs"));
25+
2326
// FIXME(rust-lang/rust#18153): generate these from an enum
2427
pub const DYNAMIC_TAG: u8 = 0u8;
2528
pub const INLINE_TAG: u8 = 1u8; // len in upper nybble

src/atom/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#![allow(non_upper_case_globals)]
1111

12-
use phf::OrderedSet;
1312
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1413

1514
use std::fmt;
@@ -25,7 +24,7 @@ use std::sync::Mutex;
2524
use std::sync::atomic::AtomicIsize;
2625
use std::sync::atomic::Ordering::SeqCst;
2726

28-
use string_cache_shared::{self, UnpackedAtom, Static, Inline, Dynamic};
27+
use string_cache_shared::{self, UnpackedAtom, Static, Inline, Dynamic, STATIC_ATOM_SET};
2928

3029
#[cfg(feature = "log-events")]
3130
use event::Event;
@@ -37,8 +36,6 @@ macro_rules! log (($e:expr) => (()));
3736
// Needed for memory safety of the tagging scheme!
3837
const ENTRY_ALIGNMENT: usize = 16;
3938

40-
// Macro-generated table for static atoms.
41-
static static_atom_set: OrderedSet<&'static str> = static_atom_set!();
4239

4340
struct StringCache {
4441
buckets: [*mut StringCacheEntry; 4096],
@@ -173,7 +170,7 @@ impl Atom {
173170

174171
#[inline]
175172
pub fn from_slice(string_to_add: &str) -> Atom {
176-
let unpacked = match static_atom_set.get_index(string_to_add) {
173+
let unpacked = match STATIC_ATOM_SET.get_index(string_to_add) {
177174
Some(id) => Static(id as u32),
178175
None => {
179176
let len = string_to_add.len();
@@ -200,7 +197,7 @@ impl Atom {
200197
let buf = string_cache_shared::inline_orig_bytes(&self.data);
201198
str::from_utf8(buf).unwrap()
202199
},
203-
Static(idx) => *static_atom_set.index(idx as usize).expect("bad static atom"),
200+
Static(idx) => *STATIC_ATOM_SET.index(idx as usize).expect("bad static atom"),
204201
Dynamic(entry) => {
205202
let entry = entry as *mut StringCacheEntry;
206203
&(*entry).string

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
#![deny(warnings)]
1616
#![cfg_attr(test, feature(test))]
1717
#![cfg_attr(bench, feature(rand))]
18-
#![plugin(phf_macros, string_cache_plugin)]
18+
#![plugin(string_cache_plugin)]
1919

2020
#[cfg(test)]
2121
extern crate test;
2222

23-
extern crate phf;
24-
2523
#[macro_use]
2624
extern crate lazy_static;
2725

0 commit comments

Comments
 (0)