Skip to main content

Store

Trait Store 

Source
pub trait Store<K, V>: Send + Sync {
    // Required methods
    fn get(&self, key: &K) -> Option<Arc<V>>;
    fn insert(&self, key: K, value: Arc<V>) -> Option<Arc<V>>;
    fn remove(&self, key: &K) -> Option<Arc<V>>;
    fn len(&self) -> usize;
    fn retain(&self, keep: &mut dyn FnMut(&K, &V) -> bool);
    fn values(&self) -> Vec<Arc<V>>;
    fn keys(&self) -> Vec<K>;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn contains_key(&self, key: &K) -> bool { ... }
}
Expand description

The map holding the entries of one Resource.

The cache keeps one store per enabled resource and does everything else itself: it decides what to insert and remove for each gateway event, keeps the guild and channel indexes, and enforces the Limits. A store only has to be a concurrent map of Arced values (D-151, D-155).

Every method takes &self: the cache is shared between shards, so a store synchronises internally. Each call must be atomic on its own; nothing more is required (the cache never relies on two calls happening together).

discord.js counterpart: the Collection a makeCache function returns for one manager.

§Examples

A store over a Mutexed HashMap, for illustration; DashMapStore is the default:

use std::{collections::HashMap, hash::Hash, sync::{Arc, Mutex}};

use djs_cache::Store;

#[derive(Debug, Default)]
struct MutexStore<K, V>(Mutex<HashMap<K, Arc<V>>>);

impl<K, V> Store<K, V> for MutexStore<K, V>
where
    K: Eq + Hash + Copy + Send + Sync,
    V: Send + Sync,
{
    fn get(&self, key: &K) -> Option<Arc<V>> {
        self.0.lock().unwrap().get(key).cloned()
    }
    fn insert(&self, key: K, value: Arc<V>) -> Option<Arc<V>> {
        self.0.lock().unwrap().insert(key, value)
    }
    fn remove(&self, key: &K) -> Option<Arc<V>> {
        self.0.lock().unwrap().remove(key)
    }
    fn len(&self) -> usize {
        self.0.lock().unwrap().len()
    }
    fn retain(&self, keep: &mut dyn FnMut(&K, &V) -> bool) {
        self.0.lock().unwrap().retain(|key, value| keep(key, value));
    }
    fn values(&self) -> Vec<Arc<V>> {
        self.0.lock().unwrap().values().cloned().collect()
    }
    fn keys(&self) -> Vec<K> {
        self.0.lock().unwrap().keys().copied().collect()
    }
}

let store = MutexStore::default();
assert!(store.insert(1_u64, Arc::new("one")).is_none());
assert_eq!(store.insert(1, Arc::new("uno")).as_deref(), Some(&"one"));
assert_eq!(store.get(&1).as_deref(), Some(&"uno"));
assert_eq!(store.len(), 1);

Required Methods§

Source

fn get(&self, key: &K) -> Option<Arc<V>>

Returns the value of key, if any.

Source

fn insert(&self, key: K, value: Arc<V>) -> Option<Arc<V>>

Inserts or replaces the value of key, returning the previous one.

Source

fn remove(&self, key: &K) -> Option<Arc<V>>

Removes the value of key, returning it.

Source

fn len(&self) -> usize

Returns the number of entries.

Source

fn retain(&self, keep: &mut dyn FnMut(&K, &V) -> bool)

Removes every entry for which keep returns false.

Entries inserted while this runs may or may not be visited.

Source

fn values(&self) -> Vec<Arc<V>>

Returns a snapshot of every value, in no particular order.

Entries changed while this runs may appear in their old or new state, or not at all.

Source

fn keys(&self) -> Vec<K>

Returns a snapshot of every key, in no particular order, with the same consistency as values.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns whether the store has no entries.

Source

fn contains_key(&self, key: &K) -> bool

Returns whether key has a value.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<K, V> Store<K, V> for DashMapStore<K, V>
where K: Eq + Hash + Copy + Send + Sync, V: Send + Sync,