Struct elsa::sync::FrozenMap

source ·
pub struct FrozenMap<K, V> { /* private fields */ }
Expand description

Append-only threadsafe version of std::collections::HashMap where insertion does not require mutable access

Implementations§

source§

impl<K, V> FrozenMap<K, V>

source

pub fn new() -> Self

Examples found in repository?
examples/sync.rs (line 8)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let a = Arc::new(FrozenMap::new());
    for i in 1..10 {
        let b = a.clone();
        thread::spawn(move || {
            b.insert(i, i.to_string());
            thread::sleep(Duration::from_millis(300));
            loop {
                if let Some(opposite) = b.get(&(10 - i)) {
                    assert!(opposite.parse::<i32>().unwrap() == 10 - i);
                    break;
                } else {
                    thread::sleep(Duration::from_millis(200));
                }
            }
        });
    }

    thread::sleep(Duration::from_millis(1000));
}
source§

impl<K: Eq + Hash, V: StableDeref> FrozenMap<K, V>

source

pub fn insert(&self, k: K, v: V) -> &V::Target

If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and returns a reference to the given value.

Existing values are never overwritten.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
assert_eq!(map.insert(1, Box::new("a")), &"a");
assert_eq!(map.insert(1, Box::new("b")), &"a");
Examples found in repository?
examples/sync.rs (line 12)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let a = Arc::new(FrozenMap::new());
    for i in 1..10 {
        let b = a.clone();
        thread::spawn(move || {
            b.insert(i, i.to_string());
            thread::sleep(Duration::from_millis(300));
            loop {
                if let Some(opposite) = b.get(&(10 - i)) {
                    assert!(opposite.parse::<i32>().unwrap() == 10 - i);
                    break;
                } else {
                    thread::sleep(Duration::from_millis(200));
                }
            }
        });
    }

    thread::sleep(Duration::from_millis(1000));
}
source

pub fn insert_with(&self, k: K, f: impl FnOnce() -> V) -> &V::Target

If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and the value returned by the creation function, and returns a reference to the generated value.

Existing values are never overwritten.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Note that the write lock is held for the duration of this function’s execution, even while the value creation function is executing (if needed). This will block any concurrent get or insert calls.

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
assert_eq!(map.insert_with(1, || Box::new("a")), &"a");
assert_eq!(map.insert_with(1, || unreachable!()), &"a");
source

pub fn insert_with_key(&self, k: K, f: impl FnOnce(&K) -> V) -> &V::Target

If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and the value returned by the creation function, and returns a reference to the generated value.

Existing values are never overwritten.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Note that the write lock is held for the duration of this function’s execution, even while the value creation function is executing (if needed). This will block any concurrent get or insert calls.

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
assert_eq!(map.insert_with_key(1, |_| Box::new("a")), &"a");
assert_eq!(map.insert_with_key(1, |_| unreachable!()), &"a");
source

pub fn get<Q>(&self, k: &Q) -> Option<&V::Target>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
map.insert(1, Box::new("a"));
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
Examples found in repository?
examples/sync.rs (line 15)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let a = Arc::new(FrozenMap::new());
    for i in 1..10 {
        let b = a.clone();
        thread::spawn(move || {
            b.insert(i, i.to_string());
            thread::sleep(Duration::from_millis(300));
            loop {
                if let Some(opposite) = b.get(&(10 - i)) {
                    assert!(opposite.parse::<i32>().unwrap() == 10 - i);
                    break;
                } else {
                    thread::sleep(Duration::from_millis(200));
                }
            }
        });
    }

    thread::sleep(Duration::from_millis(1000));
}
source

pub fn map_get<Q, T, F>(&self, k: &Q, f: F) -> Option<T>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized, F: FnOnce(&V) -> T,

Applies a function to the owner of the value corresponding to the key (if any).

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
map.insert(1, Box::new("a"));
assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a")));
assert_eq!(map.map_get(&2, Clone::clone), None);
source§

impl<K, V> FrozenMap<K, V>

source

pub fn into_tuple_vec(self) -> Vec<(K, V)>

Collects the contents of this map into a vector of tuples.

The order of the entries is as if iterating a HashMap (stochastic).

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
map.insert(1, Box::new("a"));
map.insert(2, Box::new("b"));
let mut tuple_vec = map.into_tuple_vec();
tuple_vec.sort();

assert_eq!(tuple_vec, vec![(1, Box::new("a")), (2, Box::new("b"))]);
source

pub fn len(&self) -> usize

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
assert_eq!(map.len(), 0);
map.insert(1, Box::new("a"));
assert_eq!(map.len(), 1);
source

pub fn is_empty(&self) -> bool

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
assert_eq!(map.is_empty(), true);
map.insert(1, Box::new("a"));
assert_eq!(map.is_empty(), false);
source§

impl<K: Clone, V> FrozenMap<K, V>

source

pub fn keys_cloned(&self) -> Vec<K>

source§

impl<K: Eq + Hash, V: Copy> FrozenMap<K, V>

source

pub fn get_copy<Q>(&self, k: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a copy of the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
map.get_copy_or_insert(1, 6);
assert_eq!(map.get_copy(&1), Some(6));
assert_eq!(map.get_copy(&2), None);
source

pub fn get_copy_or_insert(&self, k: K, v: V) -> V

If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and returns a reference to the given value.

Existing values are never overwritten.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
assert_eq!(map.get_copy_or_insert(1, 6), 6);
assert_eq!(map.get_copy_or_insert(1, 12), 6);
source

pub fn get_copy_or_insert_with(&self, k: K, f: impl FnOnce() -> V) -> V

If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and the value returned by the creation function, and returns a reference to the generated value.

Existing values are never overwritten.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Note that the write lock is held for the duration of this function’s execution, even while the value creation function is executing (if needed). This will block any concurrent get or insert calls.

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
assert_eq!(map.get_copy_or_insert_with(1, || 6), 6);
assert_eq!(map.get_copy_or_insert_with(1, || unreachable!()), 6);
source

pub fn get_copy_or_insert_with_key(&self, k: K, f: impl FnOnce(&K) -> V) -> V

If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and the value returned by the creation function, and returns a reference to the generated value.

Existing values are never overwritten.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Note that the write lock is held for the duration of this function’s execution, even while the value creation function is executing (if needed). This will block any concurrent get or insert calls.

Examples
use elsa::sync::FrozenMap;

let map = FrozenMap::new();
assert_eq!(map.get_copy_or_insert_with_key(1, |_| 6), 6);
assert_eq!(map.get_copy_or_insert_with_key(1, |_| unreachable!()), 6);

Trait Implementations§

source§

impl<K, V> AsMut<HashMap<K, V>> for FrozenMap<K, V>

source§

fn as_mut(&mut self) -> &mut HashMap<K, V>

Get mutable access to the underlying HashMap.

This is safe, as it requires a &mut self, ensuring nothing is using the ‘frozen’ contents.

source§

impl<K: Clone, V: Clone> Clone for FrozenMap<K, V>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: Debug, V: Debug> Debug for FrozenMap<K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K, V> Default for FrozenMap<K, V>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<K: Eq + Hash, V: PartialEq> PartialEq for FrozenMap<K, V>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<K, V> RefUnwindSafe for FrozenMap<K, V>

§

impl<K, V> Send for FrozenMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for FrozenMap<K, V>
where K: Send + Sync, V: Send + Sync,

§

impl<K, V> Unpin for FrozenMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for FrozenMap<K, V>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.