Skip to content

Commit 035ad18

Browse files
author
Richard Dodd
committed
Added some module-level docs
1 parent d5b6071 commit 035ad18

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

src/lib.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,96 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10+
//!
11+
//! A library for interning things that are `AsRef<str>`.
12+
//!
13+
//! Some strings may be interned at compile time using the `string-cache-codegen` crate, or the
14+
//! `EmptyStaticAtomSet` may be used that has no comiple-time interned strings. An `Atom` is an
15+
//! interned string for a given set (either `EmptyStaticAtomSet` or a generated `StaticAtomSet`).
16+
//!
17+
//! Generated `Atom`s will have assocated macros to intern static strings at compile-time.
18+
//!
19+
//! # Examples
20+
//!
21+
//! Here are two examples, one with compile-time `Atom`s, and one without.
22+
//!
23+
//! ## With compile-time atoms
24+
//!
25+
//! In `Cargo.toml`:
26+
//! ```toml
27+
//! [dependencies]
28+
//! string_cache = "0.7"
29+
//!
30+
//! [dev-dependencies]
31+
//! string_cache_codegen = "0.4"
32+
//! ```
33+
//!
34+
//! In `build.rs`:
35+
//! ```rust
36+
//! extern crate string_cache_codegen;
37+
//!
38+
//! use std::env;
39+
//! use std::path::Path;
40+
//!
41+
//! fn main() {
42+
//! string_cache_codegen::AtomType::new("foo::FooAtom", "foo_atom!")
43+
//! .atoms(&["foo", "bar"])
44+
//! .write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("foo_atom.rs"))
45+
//! .unwrap()
46+
//! }
47+
//! ```
48+
//!
49+
//! In `lib.rs`:
50+
//! ```rust
51+
//! extern crate string_cache;
52+
//!
53+
//! mod foo {
54+
//! include!(concat!(env!("OUT_DIR"), "/foo_atom.rs"));
55+
//! }
56+
//!
57+
//! fn use_the_atom(t: &str) {
58+
//! match *t {
59+
//! foo_atom!("foo") => println!("Found foo!"),
60+
//! foo_atom!("bar") => println!("Found bar!"),
61+
//! // foo_atom!("baz") => println!("Found baz!"), - would be a compile time error
62+
//! _ => {
63+
//! println!("String not interned");
64+
//! // We can intern strings at runtime as well
65+
//! foo::FooAtom::from(t)
66+
//! }
67+
//! }
68+
//! }
69+
//! ```
70+
//!
71+
//! ## No compile-time atoms
72+
//!
73+
//! ```rust
74+
//! extern crate string_cache;
75+
//!
76+
//!
77+
//! let mut interned_stuff = Vec::new();
78+
//! let text = "here is a sentence of text that will be tokenised and
79+
//! interned and some repeated tokens is of text and";
80+
//! for word in text.split_whitespace() {
81+
//! let seen_before = interned_stuff.iter()
82+
//! // We can use impl PartialEq<T> where T is anything string-like
83+
//! // to compare to interned strings to either other interned strings,
84+
//! // or actual strings Comparing two interned strings is very fast
85+
//! // (normally a single cpu operation).
86+
//! .filter(|interned_word| interned_word == &word)
87+
//! .count();
88+
//! if seen_before > 0 {
89+
//! println!(r#"Seen the word "{}" {} times"#, word, seen_before);
90+
//! } else {
91+
//! println!(r#"Not seen the word "{}" before"#, word);
92+
//! }
93+
//! // We use the impl From<(Cow<'a, str>, or &'a str, or String)> for
94+
//! // Atom<Static> to intern a new string.
95+
//! interned_stuff.push(DefaultAtom::from(word));
96+
//! }
97+
//! ```
98+
//!
99+
10100
#![crate_name = "string_cache"]
11101
#![crate_type = "rlib"]
12102

0 commit comments

Comments
 (0)