#string-cache #codegen #atom #servo #part #compile-time

build string_cache_codegen

A codegen library for string-cache, developed as part of the Servo project

14 releases

0.7.0 Jul 1, 2026
0.6.1 Sep 8, 2025
0.6.0 Aug 26, 2025
0.5.4 Feb 20, 2025
0.3.1 Nov 2, 2016

#81 in Procedural macros

Download history 1422413/week @ 2026-04-23 1418376/week @ 2026-04-30 1477649/week @ 2026-05-07 1707587/week @ 2026-05-14 1744995/week @ 2026-05-21 1907746/week @ 2026-05-28 2456905/week @ 2026-06-04 2130878/week @ 2026-06-11 2021171/week @ 2026-06-18 1815840/week @ 2026-06-25 1806355/week @ 2026-07-02 2209697/week @ 2026-07-09 2366349/week @ 2026-07-16 2391221/week @ 2026-07-23 2494165/week @ 2026-07-30 2183772/week @ 2026-08-06

9,791,403 downloads per month
Used in 4,334 crates (13 directly)

MIT/Apache

15KB
238 lines

A crate to create static string caches at compiletime.

Examples

With static atoms:

In Cargo.toml:

[package]
build = "build.rs"

[dependencies]
string_cache = "0.10"

[build-dependencies]
string_cache_codegen = "0.7"

In build.rs:

extern crate string_cache_codegen;

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

fn main() {
    string_cache_codegen::AtomType::new("foo::FooAtom", "foo_atom!")
        .atoms(&["foo", "bar"])
        .write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("foo_atom.rs"))
        .unwrap()
}

In lib.rs:

extern crate string_cache;

mod foo {
    include!(concat!(env!("OUT_DIR"), "/foo_atom.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:

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

Dependencies

~180KB