forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict.rs
More file actions
64 lines (49 loc) · 1.68 KB
/
dict.rs
File metadata and controls
64 lines (49 loc) · 1.68 KB
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
use std::borrow::Cow;
use std::collections::BTreeMap;
/// Trait for working with generic map data structures.
pub trait BDictAccess<K, V> {
/// Convert the dictionary to an unordered list of key/value pairs.
fn to_list(&self) -> Vec<(&K, &V)>;
/// Lookup a value in the dictionary.
fn lookup(&self, key: &[u8]) -> Option<&V>;
/// Lookup a mutable value in the dictionary.
fn lookup_mut(&mut self, key: &[u8]) -> Option<&mut V>;
/// Insert a key/value pair into the dictionary.
fn insert(&mut self, key: K, value: V) -> Option<V>;
/// Remove a value from the dictionary and return it.
fn remove(&mut self, key: &[u8]) -> Option<V>;
}
impl<'a, V> BDictAccess<&'a [u8], V> for BTreeMap<&'a [u8], V> {
fn to_list(&self) -> Vec<(&&'a [u8], &V)> {
self.iter().collect()
}
fn lookup(&self, key: &[u8]) -> Option<&V> {
self.get(key)
}
fn lookup_mut(&mut self, key: &[u8]) -> Option<&mut V> {
self.get_mut(key)
}
fn insert(&mut self, key: &'a [u8], value: V) -> Option<V> {
self.insert(key, value)
}
fn remove(&mut self, key: &[u8]) -> Option<V> {
self.remove(key)
}
}
impl<'a, V> BDictAccess<Cow<'a, [u8]>, V> for BTreeMap<Cow<'a, [u8]>, V> {
fn to_list(&self) -> Vec<(&Cow<'a, [u8]>, &V)> {
self.iter().collect()
}
fn lookup(&self, key: &[u8]) -> Option<&V> {
self.get(key)
}
fn lookup_mut(&mut self, key: &[u8]) -> Option<&mut V> {
self.get_mut(key)
}
fn insert(&mut self, key: Cow<'a, [u8]>, value: V) -> Option<V> {
self.insert(key, value)
}
fn remove(&mut self, key: &[u8]) -> Option<V> {
self.remove(key)
}
}