Skip to main content

StoreMutRef

Trait StoreMutRef 

Source
pub trait StoreMutRef<K, V>: Store<K, V> {
    // Required methods
    fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
       where K: Borrow<Q>,
             Q: Key;
    fn get_or_insert_default(&mut self, key: &K) -> &mut V
       where V: Default;
}
Expand description

Mutable store that can return mutable references.

This trait extends StoreMut, adding the possibility to obtain mutable references as a requirement, so values can be mutated in-place.

§Examples

use std::collections::HashMap;
use zrx_store::{StoreMut, StoreMutRef};

// Create store and initial state
let mut store = HashMap::new();
store.insert("key", 42);

// Obtain mutable reference to value
let value = store.get_mut(&"key");
assert_eq!(value, Some(&mut 42));

Required Methods§

Source

fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Key,

Returns a mutable reference to the value identified by the key.

Source

fn get_or_insert_default(&mut self, key: &K) -> &mut V
where V: Default,

Returns a mutable reference to the value or creates the default.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<K, V> StoreMutRef<K, V> for BTreeMap<K, V>
where K: Key,

Source§

fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Key,

Returns a mutable reference to the value identified by the key.

§Examples
use std::collections::BTreeMap;
use zrx_store::{StoreMut, StoreMutRef};

// Create store and initial state
let mut store = BTreeMap::new();
store.insert("key", 42);

// Obtain mutable reference to value
let value = store.get_mut(&"key");
assert_eq!(value, Some(&mut 42));
Source§

fn get_or_insert_default(&mut self, key: &K) -> &mut V
where V: Default,

Returns a mutable reference to the value or creates the default.

§Examples
use std::collections::BTreeMap;
use zrx_store::StoreMutRef;

// Create store
let mut store = BTreeMap::new();

// Obtain mutable reference to value
let value = store.get_or_insert_default(&"key");
assert_eq!(value, &mut 0);
Source§

impl<K, V> StoreMutRef<K, V> for Slab<(K, V)>
where K: Key,

Source§

fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Key,

Returns a mutable reference to the value identified by the key.

§Examples
use slab::Slab;
use zrx_store::{StoreMut, StoreMutRef};

// Create store and initial state
let mut store = Slab::new();
StoreMut::insert(&mut store, "key", 42);

// Obtain mutable reference to value
let mut value = StoreMutRef::get_mut(&mut store, &"key");
assert_eq!(value, Some(&mut 42));
Source§

fn get_or_insert_default(&mut self, key: &K) -> &mut V
where V: Default,

Returns a mutable reference to the value or creates the default.

§Examples
use slab::Slab;
use zrx_store::StoreMutRef;

// Create store
let mut store = Slab::new();

// Obtain mutable reference to value
let value = StoreMutRef::get_or_insert_default(&mut store, &"key");
assert_eq!(value, &mut 0);
Source§

impl<K, V, S> StoreMutRef<K, V> for HashMap<K, V, S>
where K: Key, S: BuildHasher,

Source§

fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Key,

Returns a mutable reference to the value identified by the key.

§Examples
use std::collections::HashMap;
use zrx_store::{StoreMut, StoreMutRef};

// Create store and initial state
let mut store = HashMap::new();
store.insert("key", 42);

// Obtain mutable reference to value
let value = store.get_mut(&"key");
assert_eq!(value, Some(&mut 42));
Source§

fn get_or_insert_default(&mut self, key: &K) -> &mut V
where V: Default,

Returns a mutable reference to the value or creates the default.

§Examples
use std::collections::HashMap;
use zrx_store::StoreMutRef;

// Create store
let mut store = HashMap::new();

// Obtain mutable reference to value
let value = store.get_or_insert_default(&"key");
assert_eq!(value, &mut 0);

Implementors§

Source§

impl<K, V, S> StoreMutRef<K, V> for Queue<K, V, S>
where K: Key, S: StoreMut<K, Item>,

Source§

impl<K, V, S> StoreMutRef<K, V> for Stash<K, V, S>
where K: Key, S: StoreMut<K, usize>,