string-cache

Build Status

Documentation

A string interning library for Rust, developed as part of the Servo project.

Simple usage

In Cargo.toml:

toml [dependencies] string_cache = "0.7"

In lib.rs:

rust extern crate string_cache; use string_cache::DefaultAtom as Atom;

With static atoms

In Cargo.toml:

```toml [package] build = "build.rs"

[dependencies] string_cache = "0.7"

[build-dependencies] stringcachecodegen = "0.4" ```

In build.rs:

```rust extern crate stringcachecodegen;

use std::env; use std::path::Path;

fn main() { stringcachecodegen::AtomType::new("foo::FooAtom", "fooatom!") .atoms(&["foo", "bar"]) .writetofile(&Path::new(&env::var("OUTDIR").unwrap()).join("foo_atom.rs")) .unwrap() } ```

In lib.rs:

```rust extern crate string_cache;

mod foo { include!(concat!(env!("OUTDIR"), "/fooatom.rs")); } ```

The generated code will define a FooAtom type and a foo_atom! macro. The macro can be used in expression or patterns, with strings listed in build.rs. For example:

rust fn compute_something(input: &foo::FooAtom) -> u32 { match *input { foo_atom!("foo") => 1, foo_atom!("bar") => 2, _ => 3, } }