defuse_map_utils/
btree_map.rs

1use std::{
2    collections::btree_map::{self, BTreeMap},
3    mem,
4};
5
6use crate::IterableMap;
7
8use super::{Entry, Map, OccupiedEntry, VacantEntry};
9
10impl<K, V> Map for BTreeMap<K, V>
11where
12    K: Ord,
13{
14    type K = K;
15
16    type V = V;
17
18    type VacantEntry<'a>
19        = btree_map::VacantEntry<'a, K, V>
20    where
21        Self: 'a;
22
23    type OccupiedEntry<'a>
24        = btree_map::OccupiedEntry<'a, K, V>
25    where
26        Self: 'a;
27
28    #[inline]
29    fn contains_key(&self, k: &Self::K) -> bool {
30        self.contains_key(k)
31    }
32
33    #[inline]
34    fn get(&self, k: &Self::K) -> Option<&Self::V> {
35        self.get(k)
36    }
37
38    #[inline]
39    fn get_mut(&mut self, k: &Self::K) -> Option<&mut Self::V> {
40        self.get_mut(k)
41    }
42
43    #[inline]
44    fn insert(&mut self, k: Self::K, v: Self::V) -> Option<Self::V> {
45        self.insert(k, v)
46    }
47
48    #[inline]
49    fn entry(&mut self, k: Self::K) -> Entry<Self::VacantEntry<'_>, Self::OccupiedEntry<'_>> {
50        match self.entry(k) {
51            btree_map::Entry::Occupied(entry) => Entry::Occupied(entry),
52            btree_map::Entry::Vacant(entry) => Entry::Vacant(entry),
53        }
54    }
55
56    #[inline]
57    fn remove(&mut self, k: &Self::K) -> Option<Self::V> {
58        self.remove(k)
59    }
60}
61
62impl<'a, K, V> VacantEntry<'a> for btree_map::VacantEntry<'a, K, V>
63where
64    K: Ord,
65{
66    type K = K;
67
68    type V = V;
69
70    #[inline]
71    fn key(&self) -> &Self::K {
72        self.key()
73    }
74
75    #[inline]
76    fn into_key(self) -> Self::K {
77        self.into_key()
78    }
79
80    #[inline]
81    fn insert(self, v: Self::V) -> &'a mut Self::V {
82        self.insert(v)
83    }
84}
85
86impl<'a, K, V> OccupiedEntry<'a> for btree_map::OccupiedEntry<'a, K, V>
87where
88    K: Ord,
89{
90    type K = K;
91
92    type V = V;
93
94    #[inline]
95    fn key(&self) -> &Self::K {
96        self.key()
97    }
98
99    #[inline]
100    fn get(&self) -> &Self::V {
101        self.get()
102    }
103
104    #[inline]
105    fn get_mut(&mut self) -> &mut Self::V {
106        self.get_mut()
107    }
108
109    #[inline]
110    fn into_mut(self) -> &'a mut Self::V {
111        self.into_mut()
112    }
113
114    #[inline]
115    fn insert(&mut self, v: Self::V) -> Self::V {
116        self.insert(v)
117    }
118
119    #[inline]
120    fn remove(self) -> Self::V {
121        self.remove()
122    }
123}
124
125impl<K, V> IterableMap for BTreeMap<K, V>
126where
127    K: Eq + Ord,
128{
129    type Keys<'a>
130        = btree_map::Keys<'a, K, V>
131    where
132        Self: 'a;
133
134    type Values<'a>
135        = btree_map::Values<'a, K, V>
136    where
137        Self: 'a;
138
139    type ValuesMut<'a>
140        = btree_map::ValuesMut<'a, K, V>
141    where
142        Self: 'a;
143
144    type Iter<'a>
145        = btree_map::Iter<'a, K, V>
146    where
147        Self: 'a;
148
149    type IterMut<'a>
150        = btree_map::IterMut<'a, K, V>
151    where
152        Self: 'a;
153
154    type Drain<'a>
155        = btree_map::IntoIter<K, V>
156    where
157        Self: 'a;
158
159    #[inline]
160    fn len(&self) -> usize {
161        self.len()
162    }
163
164    #[inline]
165    fn is_empty(&self) -> bool {
166        self.is_empty()
167    }
168
169    #[inline]
170    fn keys(&self) -> Self::Keys<'_> {
171        self.keys()
172    }
173
174    #[inline]
175    fn values(&self) -> Self::Values<'_> {
176        self.values()
177    }
178
179    #[inline]
180    fn values_mut(&mut self) -> Self::ValuesMut<'_> {
181        self.values_mut()
182    }
183
184    #[inline]
185    fn iter(&self) -> Self::Iter<'_> {
186        self.iter()
187    }
188
189    #[inline]
190    fn iter_mut(&mut self) -> Self::IterMut<'_> {
191        self.iter_mut()
192    }
193
194    #[inline]
195    fn drain(&mut self) -> Self::Drain<'_> {
196        mem::take(self).into_iter()
197    }
198
199    #[inline]
200    fn clear(&mut self) {
201        self.clear();
202    }
203}