Skip to content

Commit f6be9c3

Browse files
author
bors-servo
authored
Auto merge of #204 - derekdreery:example_simple, r=jdm
Add a simple example of library use. Specifically for internment without any static values. I thought it would be useful to show a simple use of this library. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/string-cache/204) <!-- Reviewable:end -->
2 parents e838e1c + 9488385 commit f6be9c3

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ string_cache_shared = {path = "./shared", version = "0.3"}
3737

3838
[dev-dependencies]
3939
rand = "0.4"
40+
string_cache_codegen = { version = "0.4", path = "./string-cache-codegen" }
4041

4142
[build-dependencies]
4243
string_cache_codegen = { version = "0.4", path = "./string-cache-codegen" }

examples/simple.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
extern crate string_cache;
2+
3+
use string_cache::DefaultAtom;
4+
5+
fn main() {
6+
7+
let mut interned_stuff = Vec::new();
8+
let text = "here is a sentence of text that will be tokenised and interned and some repeated \
9+
tokens is of text and";
10+
for word in text.split_whitespace() {
11+
let seen_before = interned_stuff.iter()
12+
// We can use impl PartialEq<T> where T is anything string-like to compare to
13+
// interned strings to either other interned strings, or actual strings Comparing two
14+
// interned strings is very fast (normally a single cpu operation).
15+
.filter(|interned_word| interned_word == &word)
16+
.count();
17+
if seen_before > 0 {
18+
println!(r#"Seen the word "{}" {} times"#, word, seen_before);
19+
} else {
20+
println!(r#"Not seen the word "{}" before"#, word);
21+
}
22+
// We use the impl From<(Cow<'a, str>, or &'a str, or String) for Atom<Static> to intern a
23+
// new string
24+
interned_stuff.push(DefaultAtom::from(word));
25+
}
26+
}

src/lib.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,100 @@
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 compile-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+
//!
36+
//! ```
37+
//! extern crate string_cache_codegen;
38+
//!
39+
//! use std::env;
40+
//! use std::path::Path;
41+
//!
42+
//! fn main() {
43+
//! string_cache_codegen::AtomType::new("foo::FooAtom", "foo_atom!")
44+
//! .atoms(&["foo", "bar"])
45+
//! .write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("foo_atom.rs"))
46+
//! .unwrap()
47+
//! }
48+
//! ```
49+
//!
50+
//! In `lib.rs`:
51+
//!
52+
//! ```ignore
53+
//! extern crate string_cache;
54+
//!
55+
//! mod foo {
56+
//! include!(concat!(env!("OUT_DIR"), "/foo_atom.rs"));
57+
//! }
58+
//!
59+
//! fn use_the_atom(t: &str) {
60+
//! match *t {
61+
//! foo_atom!("foo") => println!("Found foo!"),
62+
//! foo_atom!("bar") => println!("Found bar!"),
63+
//! // foo_atom!("baz") => println!("Found baz!"), - would be a compile time error
64+
//! _ => {
65+
//! println!("String not interned");
66+
//! // We can intern strings at runtime as well
67+
//! foo::FooAtom::from(t)
68+
//! }
69+
//! }
70+
//! }
71+
//! ```
72+
//!
73+
//! ## No compile-time atoms
74+
//!
75+
//! ```
76+
//! # extern crate string_cache;
77+
//! use string_cache::DefaultAtom;
78+
//!
79+
//! # fn main() {
80+
//! let mut interned_stuff = Vec::new();
81+
//! let text = "here is a sentence of text that will be tokenised and
82+
//! interned and some repeated tokens is of text and";
83+
//! for word in text.split_whitespace() {
84+
//! let seen_before = interned_stuff.iter()
85+
//! // We can use impl PartialEq<T> where T is anything string-like
86+
//! // to compare to interned strings to either other interned strings,
87+
//! // or actual strings Comparing two interned strings is very fast
88+
//! // (normally a single cpu operation).
89+
//! .filter(|interned_word| interned_word == &word)
90+
//! .count();
91+
//! if seen_before > 0 {
92+
//! println!(r#"Seen the word "{}" {} times"#, word, seen_before);
93+
//! } else {
94+
//! println!(r#"Not seen the word "{}" before"#, word);
95+
//! }
96+
//! // We use the impl From<(Cow<'a, str>, or &'a str, or String)> for
97+
//! // Atom<Static> to intern a new string.
98+
//! interned_stuff.push(DefaultAtom::from(word));
99+
//! }
100+
//! # }
101+
//! ```
102+
//!
103+
10104
#![crate_name = "string_cache"]
11105
#![crate_type = "rlib"]
12106

0 commit comments

Comments
 (0)