string_intern/lib.rs
1//! String interning for rust
2//!
3//! # Example
4//!
5//! ```
6//! use string_intern::{Validator, Symbol};
7//!
8//! struct UserIdSymbol;
9//!
10//! // This symbol may contain anything
11//! impl Validator for UserIdSymbol {
12//! // Use an error from standard library to make example shorter
13//! type Err = ::std::string::ParseError;
14//! fn validate_symbol(val: &str) -> Result<(), Self::Err> {
15//! Ok(())
16//! }
17//! }
18//!
19//! /// Actual symbol type you will use in code
20//! type UserId = Symbol<UserIdSymbol>;
21//!
22//! // Create from const (panics on invalid input)
23//! let x = UserId::from("user1");
24//! // Create from user input
25//! let y: UserId = format!("user{}", 1).parse().unwrap();
26//! // Both point to the same bytes
27//! assert!(x[..].as_bytes() as *const _ == y[..].as_bytes() as *const _);
28//! ```
29#[macro_use] extern crate lazy_static;
30#[cfg(feature = "rustc-serialize")] extern crate rustc_serialize;
31#[cfg(feature = "serde")] extern crate serde;
32#[cfg(test)] extern crate serde_json;
33
34mod base_type;
35mod validator;
36
37pub use base_type::Symbol;
38pub use validator::Validator;
39
40#[cfg(test)]
41mod test {
42 use super::{Validator, Symbol};
43
44 struct AnyString;
45
46 impl Validator for AnyString {
47 // Use an error from standard library to make example shorter
48 type Err = ::std::string::ParseError;
49 fn validate_symbol(_: &str) -> Result<(), Self::Err> {
50 Ok(())
51 }
52 }
53
54 #[test]
55 fn test_sync() {
56 fn sync<T: Sync>(_: T) { }
57 sync(Symbol::<AnyString>::from("x"))
58 }
59
60 #[test]
61 fn test_send() {
62 fn send<T: Send>(_: T) { }
63 send(Symbol::<AnyString>::from("x"))
64 }
65}