1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use serde::{de::DeserializeOwned, Serialize};
use std::{borrow::Borrow, error::Error};
pub trait Map<'a, K, V>
where
K: Serialize + DeserializeOwned + ?Sized,
V: Serialize + DeserializeOwned,
{
type Error: Error;
type Iterator: Iterator<Item = (K, V)>;
type Keys: Iterator<Item = K>;
type Values: Iterator<Item = V>;
fn contains_key(&self, key: &K) -> Result<bool, Self::Error>;
fn get(&self, key: &K) -> Result<Option<V>, Self::Error>;
fn get_or_insert<F: FnOnce() -> V>(&self, key: &K, default: F) -> Result<V, Self::Error> {
self.get(key).and_then(|optv| match optv {
Some(v) => Ok(v),
None => {
self.insert(key, &default())?;
self.get(key).transpose().expect("default just inserted")
}
})
}
fn insert(&self, key: &K, value: &V) -> Result<(), Self::Error>;
fn remove(&self, key: &K) -> Result<(), Self::Error>;
fn clear(&self) -> Result<(), Self::Error>;
fn is_empty(&self) -> bool;
fn iter(&'a self) -> Self::Iterator;
fn keys(&'a self) -> Self::Keys;
fn values(&'a self) -> Self::Values;
fn multi_get<J>(
&self,
keys: impl IntoIterator<Item = J>,
) -> Result<Vec<Option<V>>, Self::Error>
where
J: Borrow<K>;
fn multi_insert<J, U>(
&self,
key_val_pairs: impl IntoIterator<Item = (J, U)>,
) -> Result<(), Self::Error>
where
J: Borrow<K>,
U: Borrow<V>;
fn multi_remove<J>(&self, keys: impl IntoIterator<Item = J>) -> Result<(), Self::Error>
where
J: Borrow<K>;
}