Crates.io | strpool |
lib.rs | strpool |
version | 1.1.1 |
source | src |
created_at | 2023-07-30 18:20:27.127619 |
updated_at | 2024-01-31 12:42:39.477974 |
description | String Pools / Strings Interning |
homepage | |
repository | https://github.com/NathanRoyer/strpool |
max_upload_size | |
id | 930171 |
size | 128,058 |
String Pools / Strings Interning
Deref<str>
implementation - one pointer resolution, one comparison, and one pointer incrementPoolStr
] type - a pointeralloc
is requiredPool
]'s Debug
implementation allows you to see all of its stringsP
) insertion/search, where P
is Pool
's const generic parameter# use {strpool::{Pool, PoolStr}, core::ops::Deref};
// you can reduce insertion/search complexity by raising this number,
// at the expense of more frequent allocations for small strings.
// strings are spread evenly into these subpools based on their hash.
// => must be a non-zero power of two.
const SUB_POOLS: usize = 1;
// no need for mutability, the pool uses atomic operations
let pool: Pool<SUB_POOLS> = Pool::new();
// use Pool::intern(&self, &str) to insert a string slice into the pool
// if the string was already present, that PoolStr will be reused.
let pool_string = pool.intern("Hello world!");
// you can obtain a &str with the Deref implementation
assert_eq!(pool_string.deref(), "Hello world!");
// Hash, Eq, Debug, Display are implemented as well.
// you can use Pool::find(&self, &str) to check if the pool contains a string
assert_eq!(pool.find("oh hi mark"), None);
// the empty string doesn't rely on a pool, it's always there
assert_eq!(pool.find(""), Some(PoolStr::empty()));
// See all interned strings via the Debug implementation
println!("{:#?}", pool);