#![warn(missing_docs)]
#[cfg(feature = "serde")]
mod static_str;
#[cfg(feature = "serde")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
pub use static_str::*;
use std::{cell::RefCell, collections::HashSet};
thread_local! {
static STATIC_STRS: RefCell<HashSet<&'static str>> = Default::default();
}
pub fn static_str(s: &str) -> &'static str {
STATIC_STRS.with(|strs| {
let mut strs = strs.borrow_mut();
if !strs.contains(s) {
strs.insert(Box::leak(s.to_string().into_boxed_str()));
}
*strs.get(s).unwrap()
})
}
pub fn is_cached(s: &str) -> bool {
STATIC_STRS.with(|strs| strs.borrow().contains(s))
}
#[cfg(test)]
#[test]
fn it_works() {
let a = static_str("a");
let b = static_str("b");
let c = static_str("c");
let d = static_str("d");
assert_eq!(a, "a");
assert_eq!(b, "b");
assert_eq!(c, "c");
assert_eq!(d, "d");
}