bip_bencode/access/
dict.rs1use std::borrow::Cow;
2use std::collections::BTreeMap;
3
4pub trait BDictAccess<K, V> {
6 fn to_list(&self) -> Vec<(&K, &V)>;
8
9 fn lookup(&self, key: &[u8]) -> Option<&V>;
11
12 fn lookup_mut(&mut self, key: &[u8]) -> Option<&mut V>;
14
15 fn insert(&mut self, key: K, value: V) -> Option<V>;
17
18 fn remove(&mut self, key: &[u8]) -> Option<V>;
20}
21
22impl<'a, V> BDictAccess<&'a [u8], V> for BTreeMap<&'a [u8], V> {
23 fn to_list(&self) -> Vec<(&&'a [u8], &V)> {
24 self.iter().map(|(k, v)| (k, v)).collect()
25 }
26
27 fn lookup(&self, key: &[u8]) -> Option<&V> {
28 self.get(key)
29 }
30
31 fn lookup_mut(&mut self, key: &[u8]) -> Option<&mut V> {
32 self.get_mut(key)
33 }
34
35 fn insert(&mut self, key: &'a [u8], value: V) -> Option<V> {
36 self.insert(key, value)
37 }
38
39 fn remove(&mut self, key: &[u8]) -> Option<V> {
40 self.remove(key)
41 }
42}
43
44impl<'a, V> BDictAccess<Cow<'a, [u8]>, V> for BTreeMap<Cow<'a, [u8]>, V> {
45 fn to_list(&self) -> Vec<(&Cow<'a, [u8]>, &V)> {
46 self.iter().map(|(k, v)| (k, v)).collect()
47 }
48
49 fn lookup(&self, key: &[u8]) -> Option<&V> {
50 self.get(key)
51 }
52
53 fn lookup_mut(&mut self, key: &[u8]) -> Option<&mut V> {
54 self.get_mut(key)
55 }
56
57 fn insert(&mut self, key: Cow<'a, [u8]>, value: V) -> Option<V> {
58 self.insert(key, value)
59 }
60
61 fn remove(&mut self, key: &[u8]) -> Option<V> {
62 self.remove(key)
63 }
64}