Struct lasso::ThreadedRodeo
source · pub struct ThreadedRodeo<K = Spur, S = RandomState> { /* private fields */ }
Expand description
A concurrent string interner that caches strings quickly with a minimal memory footprint,
returning a unique key to re-access it with O(1)
internment and resolution.
This struct is only available with the multi-threaded
feature!
By default ThreadedRodeo uses the Spur
type for keys and RandomState
as the hasher
Implementations§
source§impl<K> ThreadedRodeo<K, RandomState>where
K: Key + Hash,
impl<K> ThreadedRodeo<K, RandomState>where
K: Key + Hash,
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new ThreadedRodeo
Example
use lasso::{ThreadedRodeo, Spur};
use std::{thread, sync::Arc};
let lasso: Arc<ThreadedRodeo<Spur>> = Arc::new(ThreadedRodeo::new());
let hello = lasso.get_or_intern("Hello, ");
let l = Arc::clone(&lasso);
let world = thread::spawn(move || {
l.get_or_intern("World!")
})
.join()
.unwrap();
assert_eq!("Hello, ", lasso.resolve(&hello));
assert_eq!("World!", lasso.resolve(&world));
sourcepub fn with_capacity(capacity: Capacity) -> Self
pub fn with_capacity(capacity: Capacity) -> Self
Create a new ThreadedRodeo with the specified capacity. The interner will be able to hold capacity
strings without reallocating. If capacity is 0, the interner will not allocate.
See Capacity
for more details
Example
use lasso::{ThreadedRodeo, Capacity, Spur};
let rodeo: ThreadedRodeo<Spur> = ThreadedRodeo::with_capacity(Capacity::for_strings(10));
sourcepub fn with_memory_limits(memory_limits: MemoryLimits) -> Self
pub fn with_memory_limits(memory_limits: MemoryLimits) -> Self
Create a new ThreadedRodeo with the specified memory limits. The interner will be able to hold max_memory_usage
bytes of interned strings until it will start returning None
from try_get_or_intern
or panicking from
get_or_intern
.
Note: If the capacity of the interner is greater than the memory limit, then that will be the effective maximum for allocated memory
See MemoryLimits
for more information
Example
use lasso::{ThreadedRodeo, MemoryLimits, Spur};
let rodeo: ThreadedRodeo<Spur> = ThreadedRodeo::with_memory_limits(MemoryLimits::for_memory_usage(4096));
sourcepub fn with_capacity_and_memory_limits(
capacity: Capacity,
memory_limits: MemoryLimits
) -> Self
pub fn with_capacity_and_memory_limits(
capacity: Capacity,
memory_limits: MemoryLimits
) -> Self
Create a new ThreadedRodeo with the specified capacity and memory limits. The interner will be able to hold max_memory_usage
bytes of interned strings until it will start returning None
from try_get_or_intern
or panicking from
get_or_intern
.
Note: If the capacity of the interner is greater than the memory limit, then that will be the effective maximum for allocated memory
See Capacity
MemoryLimits
for more information
Example
use lasso::{ThreadedRodeo, MemoryLimits, Spur};
let rodeo: ThreadedRodeo<Spur> = ThreadedRodeo::with_memory_limits(MemoryLimits::for_memory_usage(4096));
source§impl<K, S> ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Creates an empty ThreadedRodeo which will use the given hasher for its internal hashmap
Example
use lasso::{Spur, ThreadedRodeo};
use std::collections::hash_map::RandomState;
let rodeo: ThreadedRodeo<Spur, RandomState> = ThreadedRodeo::with_hasher(RandomState::new());
sourcepub fn with_capacity_and_hasher(capacity: Capacity, hash_builder: S) -> Self
pub fn with_capacity_and_hasher(capacity: Capacity, hash_builder: S) -> Self
Creates a new ThreadedRodeo with the specified capacity that will use the given hasher for its internal hashmap
See Capacity
for more details
Example
use lasso::{Spur, Capacity, ThreadedRodeo};
use std::collections::hash_map::RandomState;
let rodeo: ThreadedRodeo<Spur, RandomState> = ThreadedRodeo::with_capacity_and_hasher(Capacity::for_strings(10), RandomState::new());
sourcepub fn with_capacity_memory_limits_and_hasher(
capacity: Capacity,
memory_limits: MemoryLimits,
hash_builder: S
) -> Self
pub fn with_capacity_memory_limits_and_hasher(
capacity: Capacity,
memory_limits: MemoryLimits,
hash_builder: S
) -> Self
Creates a new ThreadedRodeo with the specified capacity and memory limits that will use the given hasher for its internal hashmap
See Capacity
and MemoryLimits
for more information
Example
use lasso::{Spur, Capacity, MemoryLimits, ThreadedRodeo};
use std::collections::hash_map::RandomState;
let rodeo: ThreadedRodeo<Spur, RandomState> = ThreadedRodeo::with_capacity_memory_limits_and_hasher(
Capacity::for_strings(10),
MemoryLimits::for_memory_usage(4096),
RandomState::new(),
);
sourcepub fn get_or_intern<T>(&self, val: T) -> Kwhere
T: AsRef<str>,
pub fn get_or_intern<T>(&self, val: T) -> Kwhere
T: AsRef<str>,
Get the key for a string, interning it if it does not yet exist
Panics
Panics if the key’s try_from_usize
function fails. With the default keys, this means that
you’ve interned more strings than it can handle. (For Spur
this means that u32::MAX - 1
unique strings were interned)
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
// Interned the string
let key = rodeo.get_or_intern("Strings of things with wings and dings");
assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
// No string was interned, as it was already contained
let key = rodeo.get_or_intern("Strings of things with wings and dings");
assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
sourcepub fn try_get_or_intern<T>(&self, val: T) -> LassoResult<K>where
T: AsRef<str>,
pub fn try_get_or_intern<T>(&self, val: T) -> LassoResult<K>where
T: AsRef<str>,
Get the key for a string, interning it if it does not yet exist
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
// Interned the string
let key = rodeo.get_or_intern("Strings of things with wings and dings");
assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
// No string was interned, as it was already contained
let key = rodeo.get_or_intern("Strings of things with wings and dings");
assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
sourcepub fn get_or_intern_static(&self, string: &'static str) -> K
pub fn get_or_intern_static(&self, string: &'static str) -> K
Get the key for a static string, interning it if it does not yet exist
This will not reallocate or copy the given string but will instead just store it
Panics
Panics if the key’s try_from_usize
function fails. With the default keys, this means that
you’ve interned more strings than it can handle. (For Spur
this means that u32::MAX - 1
unique strings were interned)
Example
use lasso::ThreadedRodeo;
let mut rodeo = ThreadedRodeo::default();
// Interned the string
let key = rodeo.get_or_intern_static("Strings of things with wings and dings");
assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
// No string was interned, as it was already contained
let key = rodeo.get_or_intern_static("Strings of things with wings and dings");
assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
sourcepub fn try_get_or_intern_static(&self, string: &'static str) -> LassoResult<K>
pub fn try_get_or_intern_static(&self, string: &'static str) -> LassoResult<K>
Get the key for a static string, interning it if it does not yet exist
This will not reallocate and copy the given string but will instead just store it
Example
use lasso::ThreadedRodeo;
let mut rodeo = ThreadedRodeo::default();
// Interned the string
let key = rodeo.try_get_or_intern_static("Strings of things with wings and dings").unwrap();
assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
// No string was interned, as it was already contained
let key = rodeo.try_get_or_intern_static("Strings of things with wings and dings").unwrap();
assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
sourcepub fn get<T>(&self, val: T) -> Option<K>where
T: AsRef<str>,
pub fn get<T>(&self, val: T) -> Option<K>where
T: AsRef<str>,
Get the key value of a string, returning None
if it doesn’t exist
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
let key = rodeo.get_or_intern("Strings of things with wings and dings");
assert_eq!(Some(key), rodeo.get("Strings of things with wings and dings"));
assert_eq!(None, rodeo.get("This string isn't interned"));
sourcepub fn contains<T>(&self, val: T) -> boolwhere
T: AsRef<str>,
pub fn contains<T>(&self, val: T) -> boolwhere
T: AsRef<str>,
Returns true
if the given string has been interned
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
let key = rodeo.get_or_intern("Strings of things with wings and dings");
assert!(rodeo.contains("Strings of things with wings and dings"));
assert!(!rodeo.contains("This string isn't interned"));
sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true
if the given key exists in the current interner
Example
use lasso::ThreadedRodeo;
let mut rodeo = ThreadedRodeo::default();
let key = rodeo.get_or_intern("Strings of things with wings and dings");
assert!(rodeo.contains_key(&key));
assert!(!rodeo.contains_key(&key_that_doesnt_exist));
sourcepub fn resolve<'a>(&'a self, key: &K) -> &'a str
pub fn resolve<'a>(&'a self, key: &K) -> &'a str
Resolves a string by its key. Only keys made by the current ThreadedRodeo may be used
Panics
Panics if the key is out of bounds
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
let key = rodeo.get_or_intern("Strings of things with wings and dings");
assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
sourcepub fn try_resolve<'a>(&'a self, key: &K) -> Option<&'a str>
pub fn try_resolve<'a>(&'a self, key: &K) -> Option<&'a str>
Resolves a string by its key, returning None
if it is out of bounds. Only keys made by the current
ThreadedRodeo may be used
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
let key = rodeo.get_or_intern("Strings of things with wings and dings");
assert_eq!(Some("Strings of things with wings and dings"), rodeo.try_resolve(&key));
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Gets the number of interned strings
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
rodeo.get_or_intern("Documentation often has little hidden bits in it");
assert_eq!(rodeo.len(), 1);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if there are no currently interned strings
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
assert!(rodeo.is_empty());
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of strings that can be interned without a reallocation
This is an unreliable measurement since the underlying hashmap is unreliable in its capacity measurement
Example
use lasso::{Spur, Capacity, ThreadedRodeo};
let rodeo: ThreadedRodeo<Spur> = ThreadedRodeo::with_capacity(Capacity::for_strings(10));
assert_eq!(rodeo.capacity(), 10);
sourcepub fn iter(&self) -> Iter<'_, K, S>
pub fn iter(&self) -> Iter<'_, K, S>
Returns an iterator over the interned strings and their key values
sourcepub fn set_memory_limits(&self, memory_limits: MemoryLimits)
pub fn set_memory_limits(&self, memory_limits: MemoryLimits)
Set the ThreadedRodeo
’s maximum memory usage while in-flight
Note that setting the maximum memory usage to below the currently allocated memory will do nothing
sourcepub fn current_memory_usage(&self) -> usize
pub fn current_memory_usage(&self) -> usize
Get the ThreadedRodeo
’s currently allocated memory
sourcepub fn max_memory_usage(&self) -> usize
pub fn max_memory_usage(&self) -> usize
Get the ThreadedRodeo
’s current maximum of allocated memory
sourcepub fn into_reader(self) -> RodeoReader<K, S>
pub fn into_reader(self) -> RodeoReader<K, S>
Consumes the current ThreadedRodeo, returning a RodeoReader
to allow contention-free access of the interner
from multiple threads
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
let key = rodeo.get_or_intern("Appear weak when you are strong, and strong when you are weak.");
let rodeo_reader = rodeo.into_reader();
assert_eq!(
"Appear weak when you are strong, and strong when you are weak.",
rodeo_reader.resolve(&key),
);
sourcepub fn into_resolver(self) -> RodeoResolver<K>
pub fn into_resolver(self) -> RodeoResolver<K>
Consumes the current ThreadedRodeo, returning a RodeoResolver
to allow contention-free access of the interner
from multiple threads with the lowest possible memory consumption
Example
use lasso::ThreadedRodeo;
let rodeo = ThreadedRodeo::default();
let key = rodeo.get_or_intern("Appear weak when you are strong, and strong when you are weak.");
let rodeo_resolver = rodeo.into_resolver();
assert_eq!(
"Appear weak when you are strong, and strong when you are weak.",
rodeo_resolver.resolve(&key),
);
Trait Implementations§
source§impl<K, S> Debug for ThreadedRodeo<K, S>where
K: Key + Hash + Debug,
S: BuildHasher + Clone,
impl<K, S> Debug for ThreadedRodeo<K, S>where
K: Key + Hash + Debug,
S: BuildHasher + Clone,
source§impl Default for ThreadedRodeo<Spur, RandomState>
impl Default for ThreadedRodeo<Spur, RandomState>
Creates a ThreadedRodeo using Spur
as its key and RandomState
as its hasher
source§impl<K, S, T> Extend<T> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
T: AsRef<str>,
impl<K, S, T> Extend<T> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
T: AsRef<str>,
source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<Str, K, S> FromIterator<Str> for ThreadedRodeo<K, S>where
Str: AsRef<str>,
K: Key + Hash,
S: BuildHasher + Clone + Default,
impl<Str, K, S> FromIterator<Str> for ThreadedRodeo<K, S>where
Str: AsRef<str>,
K: Key + Hash,
S: BuildHasher + Clone + Default,
source§fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = Str>,
fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = Str>,
source§impl<K, S> Index<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> Index<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
source§impl<K, S> Interner<K> for &ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> Interner<K> for &ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
source§fn get_or_intern(&mut self, val: &str) -> K
fn get_or_intern(&mut self, val: &str) -> K
source§fn try_get_or_intern(&mut self, val: &str) -> LassoResult<K>
fn try_get_or_intern(&mut self, val: &str) -> LassoResult<K>
source§fn get_or_intern_static(&mut self, val: &'static str) -> K
fn get_or_intern_static(&mut self, val: &'static str) -> K
source§fn try_get_or_intern_static(&mut self, val: &'static str) -> LassoResult<K>
fn try_get_or_intern_static(&mut self, val: &'static str) -> LassoResult<K>
source§impl<K, S> Interner<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> Interner<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
source§fn get_or_intern(&mut self, val: &str) -> K
fn get_or_intern(&mut self, val: &str) -> K
source§fn try_get_or_intern(&mut self, val: &str) -> LassoResult<K>
fn try_get_or_intern(&mut self, val: &str) -> LassoResult<K>
source§fn get_or_intern_static(&mut self, val: &'static str) -> K
fn get_or_intern_static(&mut self, val: &'static str) -> K
source§fn try_get_or_intern_static(&mut self, val: &'static str) -> LassoResult<K>
fn try_get_or_intern_static(&mut self, val: &'static str) -> LassoResult<K>
source§impl<K, S> IntoReader<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> IntoReader<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
§type Reader = RodeoReader<K, S>
type Reader = RodeoReader<K, S>
Reader
the interner will be converted intosource§impl<K, S> IntoResolver<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> IntoResolver<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
§type Resolver = RodeoResolver<K>
type Resolver = RodeoResolver<K>
Resolver
the reader will be converted intosource§impl<K, S> PartialEq<Rodeo<K, S>> for ThreadedRodeo<K, S>where
K: Eq + Hash + Key,
S: Clone + BuildHasher,
impl<K, S> PartialEq<Rodeo<K, S>> for ThreadedRodeo<K, S>where
K: Eq + Hash + Key,
S: Clone + BuildHasher,
source§impl<K, S> PartialEq<RodeoReader<K, S>> for ThreadedRodeo<K, S>where
K: Eq + Hash + Key,
S: Clone + BuildHasher,
impl<K, S> PartialEq<RodeoReader<K, S>> for ThreadedRodeo<K, S>where
K: Eq + Hash + Key,
S: Clone + BuildHasher,
source§fn eq(&self, other: &RodeoReader<K, S>) -> bool
fn eq(&self, other: &RodeoReader<K, S>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<K, S> PartialEq<RodeoResolver<K>> for ThreadedRodeo<K, S>where
K: Eq + Hash + Key,
S: Clone + BuildHasher,
impl<K, S> PartialEq<RodeoResolver<K>> for ThreadedRodeo<K, S>where
K: Eq + Hash + Key,
S: Clone + BuildHasher,
source§fn eq(&self, other: &RodeoResolver<K>) -> bool
fn eq(&self, other: &RodeoResolver<K>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<K, S> PartialEq<ThreadedRodeo<K, S>> for ThreadedRodeo<K, S>where
K: Eq + Hash,
S: Clone + BuildHasher,
impl<K, S> PartialEq<ThreadedRodeo<K, S>> for ThreadedRodeo<K, S>where
K: Eq + Hash,
S: Clone + BuildHasher,
source§impl<K, S> Reader<K> for &ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> Reader<K> for &ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
source§impl<K, S> Reader<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> Reader<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
source§impl<K, S> Resolver<K> for &ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> Resolver<K> for &ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
source§fn try_resolve<'a>(&'a self, key: &K) -> Option<&'a str>
fn try_resolve<'a>(&'a self, key: &K) -> Option<&'a str>
None
if it cannot be foundsource§unsafe fn resolve_unchecked<'a>(&'a self, key: &K) -> &'a str
unsafe fn resolve_unchecked<'a>(&'a self, key: &K) -> &'a str
source§fn contains_key(&self, key: &K) -> bool
fn contains_key(&self, key: &K) -> bool
true
if the current interner contains the given keysource§impl<K, S> Resolver<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
impl<K, S> Resolver<K> for ThreadedRodeo<K, S>where
K: Key + Hash,
S: BuildHasher + Clone,
source§unsafe fn resolve_unchecked<'a>(&'a self, key: &K) -> &'a str
unsafe fn resolve_unchecked<'a>(&'a self, key: &K) -> &'a str
ThreadedRodeo
does not actually have a resolve_unchecked()
method,
so this just forwards to the normal ThreadedRodeo::resolve()
method
source§fn try_resolve<'a>(&'a self, key: &K) -> Option<&'a str>
fn try_resolve<'a>(&'a self, key: &K) -> Option<&'a str>
None
if it cannot be foundsource§fn contains_key(&self, key: &K) -> bool
fn contains_key(&self, key: &K) -> bool
true
if the current interner contains the given key