bitwarden_crypto/store/backend/implementation/
basic.rsuse zeroize::ZeroizeOnDrop;
use crate::{store::backend::StoreBackend, KeyId};
pub(crate) struct BasicBackend<Key: KeyId> {
keys: std::collections::HashMap<Key, Key::KeyValue>,
}
impl<Key: KeyId> BasicBackend<Key> {
pub fn new() -> Self {
Self {
keys: std::collections::HashMap::new(),
}
}
}
impl<Key: KeyId> StoreBackend<Key> for BasicBackend<Key> {
fn upsert(&mut self, key_id: Key, key: <Key as KeyId>::KeyValue) {
self.keys.insert(key_id, key);
}
fn get(&self, key_id: Key) -> Option<&<Key as KeyId>::KeyValue> {
self.keys.get(&key_id)
}
fn remove(&mut self, key_id: Key) {
self.keys.remove(&key_id);
}
fn clear(&mut self) {
self.keys.clear();
}
fn retain(&mut self, f: fn(Key) -> bool) {
self.keys.retain(|k, _| f(*k));
}
}
impl<Key: KeyId> ZeroizeOnDrop for BasicBackend<Key> {}
impl<Key: KeyId> Drop for BasicBackend<Key> {
fn drop(&mut self) {
self.clear();
}
}