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

Run on Rust stable #95

Merged
merged 13 commits into from
Jul 27, 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
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
sudo: false
language: rust
rust: nightly
rust:
- nightly
- beta
- stable
script:
- cargo build
- cargo test
- cargo clean
- cargo test --features log-events
- cd examples/summarize-events/
- cargo build
- "if [ $TRAVIS_RUST_VERSION = nightly ]; then cargo test --features unstable; fi"
- "cd examples/summarize-events/ && cargo build"
notifications:
webhooks: http://build.servo.org:54856/travis
17 changes: 13 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]

name = "string_cache"
version = "0.1.6"
version = "0.1.7"
authors = [ "The Servo Project Developers" ]
description = "A string interning library for Rust, developed as part of the Servo project."
license = "MIT / Apache-2.0"
repository = "https://github.com/servo/string-cache"
documentation = "http://doc.servo.org/string_cache/"
build = "build.rs"

[lib]
name = "string_cache"
Expand All @@ -20,21 +21,29 @@ doctest = false
# See examples/event-log.
log-events = ["rustc-serialize"]

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

[dependencies]
rand = "0"
phf = "0.7"
phf_macros = "0.7"
lazy_static = "0.1.10"
serde = "0.4.2"

[dev-dependencies]
rand = "0"

[dependencies.rustc-serialize]
version = "0"
optional = true

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

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

[build-dependencies.string_cache_shared]
path = "shared"
version = "0.1.0"
59 changes: 59 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
extern crate string_cache_shared;

use string_cache_shared::{STATIC_ATOM_SET, ALL_NS, pack_static};

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

fn main() {
let path = Path::new(env!("OUT_DIR")).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();

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();
}
writeln!(file, r"({:?}) => {{ {} }};", s, atom(s)).unwrap();
}
writeln!(file, r"}}").unwrap();
}

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 atom(s: &str) -> String {
let data = pack_static(STATIC_ATOM_SET.get_index(s).unwrap() as u32);
format!("$crate::Atom {{ data: 0x{:x} }}", data)
}

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
})
}
36 changes: 3 additions & 33 deletions plugin/src/atom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ use std::iter::Chain;
use std::collections::HashMap;
use std::ascii::AsciiExt;

mod data;

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

fn atom_tok_to_str(t: &TokenTree) -> Option<InternedString> {
Some(get_ident(match *t {
Expand All @@ -38,17 +27,6 @@ fn atom_tok_to_str(t: &TokenTree) -> Option<InternedString> {
}))
}

// Build a map from atoms to IDs for use in implementing the atom!() macro.
lazy_static! {
static ref STATIC_ATOM_MAP: HashMap<&'static str, usize> = {
let mut m = HashMap::new();
for (i, x) in data::ATOMS.iter().enumerate() {
m.insert(*x, i);
}
m
};
}

// FIXME: libsyntax should provide this (rust-lang/rust#17637)
struct AtomResult {
expr: P<ast::Expr>,
Expand All @@ -66,12 +44,12 @@ impl MacResult for AtomResult {
}

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

let data = ::string_cache_shared::pack_static(*i as u32);
let data = ::string_cache_shared::pack_static(i as u32);

Some(AtomResult {
expr: quote_expr!(&mut *cx, ::string_cache::atom::Atom { data: $data }),
Expand All @@ -93,15 +71,7 @@ pub fn expand_atom(cx: &mut ExtCtxt, sp: Span, tt: &[TokenTree]) -> Box<MacResul
// Translate `ns!(HTML)` into `Namespace { atom: atom!("http://www.w3.org/1999/xhtml") }`.
// The argument is ASCII-case-insensitive.
pub fn expand_ns(cx: &mut ExtCtxt, sp: Span, tt: &[TokenTree]) -> Box<MacResult+'static> {
static ALL_NS: &'static [(&'static str, &'static str)] = &[
("", ""),
("html", "http://www.w3.org/1999/xhtml"),
("xml", "http://www.w3.org/XML/1998/namespace"),
("xmlns", "http://www.w3.org/2000/xmlns/"),
("xlink", "http://www.w3.org/1999/xlink"),
("svg", "http://www.w3.org/2000/svg"),
("mathml", "http://www.w3.org/1998/Math/MathML"),
];
use string_cache_shared::ALL_NS;

fn usage() -> String {
let ns_names: Vec<&'static str> = ALL_NS[1..].iter()
Expand Down
3 changes: 1 addition & 2 deletions plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#![feature(plugin_registrar, quote, box_syntax)]
#![feature(rustc_private, slice_patterns)]
#![deny(warnings)]
#![cfg_attr(test, deny(warnings))]
#![allow(unused_imports)] // for quotes

extern crate syntax;
Expand All @@ -33,7 +33,6 @@ mod atom;
// NB: This needs to be public or we get a linker error.
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_macro("static_atom_set", atom::expand_static_atom_set);
reg.register_macro("atom", atom::expand_atom);
reg.register_macro("ns", atom::expand_ns);
}
10 changes: 9 additions & 1 deletion shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
[package]

name = "string_cache_shared"
version = "0.1.3"
version = "0.1.2"
authors = [ "The Servo Project Developers" ]
description = "A string interning library for Rust, developed as part of the Servo project − shared code between the compiler plugin and main crate."
license = "MIT / Apache-2.0"
repository = "https://github.com/servo/string-cache"
build = "build.rs"

[lib]

name = "string_cache_shared"
path = "lib.rs"

[dependencies]
debug_unreachable = "0.0.5"
phf = "0.7.3"

[build-dependencies]
phf_codegen = "0.7.3"
20 changes: 20 additions & 0 deletions shared/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extern crate phf_codegen;

mod static_atom_list;

use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

fn main() {
let mut set = phf_codegen::OrderedSet::new();
for &atom in static_atom_list::ATOMS {
set.entry(atom);
}

let path = Path::new(env!("OUT_DIR")).join("static_atom_set.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
write!(&mut file, "pub static STATIC_ATOM_SET: phf::OrderedSet<&'static str> = ").unwrap();
set.build(&mut file).unwrap();
write!(&mut file, ";\n").unwrap();
}
Loading