1use std::borrow::Borrow;
2
3use crate::{AList, Bookmark};
4
5pub trait Get<K, V> {
6 fn get(self, alist: &AList<K, V>) -> Option<&V>;
7}
8
9impl<'q, Q, K, V> Get<K, V> for &mut Bookmark<'q, Q, K, V>
10where
11 K: Borrow<Q>,
12 Q: Eq + ?Sized,
13{
14 fn get(self, alist: &AList<K, V>) -> Option<&V> {
15 if !alist.is_valid(self) {
16 self.position = alist.position(self.key())?;
17 }
18
19 let (_, v) = &alist.pairs[self.position];
20 Some(v)
21 }
22}
23
24impl<Q, K, V> Get<K, V> for &Q
25where
26 K: Borrow<Q>,
27 Q: Eq + ?Sized,
28{
29 fn get(self, alist: &AList<K, V>) -> Option<&V> {
30 alist.find(self).map(|(_, v)| v)
31 }
32}
33
34pub trait GetMut<K, V> {
35 fn get_mut(self, alist: &mut AList<K, V>) -> Option<&mut V>;
36}
37
38impl<'q, Q, K, V> GetMut<K, V> for &mut Bookmark<'q, Q, K, V>
39where
40 K: Borrow<Q>,
41 Q: Eq + ?Sized,
42{
43 fn get_mut(self, alist: &mut AList<K, V>) -> Option<&mut V> {
44 if !alist.is_valid(self) {
45 self.position = alist.position(self.key())?;
46 }
47
48 let (_, v) = &mut alist.pairs[self.position];
49 Some(v)
50 }
51}
52
53impl<Q, K, V> GetMut<K, V> for &Q
54where
55 K: Borrow<Q>,
56 Q: Eq + ?Sized,
57{
58 fn get_mut(self, alist: &mut AList<K, V>) -> Option<&mut V> {
59 alist.find_mut(self).map(|(_, v)| v)
60 }
61}
62
63pub trait GetKeyValue<K, V> {
64 fn get_key_value(self, alist: &AList<K, V>) -> Option<(&K, &V)>;
65}
66
67impl<'q, Q, K, V> GetKeyValue<K, V> for &mut Bookmark<'q, Q, K, V>
68where
69 K: Borrow<Q>,
70 Q: Eq + ?Sized,
71{
72 fn get_key_value(self, alist: &AList<K, V>) -> Option<(&K, &V)> {
73 if !alist.is_valid(self) {
74 self.position = alist.position(self.key())?;
75 }
76
77 let (k, v) = &alist.pairs[self.position];
78 Some((k, v))
79 }
80}
81
82impl<Q, K, V> GetKeyValue<K, V> for &Q
83where
84 K: Borrow<Q>,
85 Q: Eq + ?Sized,
86{
87 fn get_key_value(self, alist: &AList<K, V>) -> Option<(&K, &V)> {
88 alist.find(self)
89 }
90}
91
92pub trait GetKeyValueMut<K, V> {
93 fn get_key_value_mut(self, alist: &mut AList<K, V>) -> Option<(&K, &mut V)>;
94}
95
96impl<'q, Q, K, V> GetKeyValueMut<K, V> for &mut Bookmark<'q, Q, K, V>
97where
98 K: Borrow<Q>,
99 Q: Eq + ?Sized,
100{
101 fn get_key_value_mut(self, alist: &mut AList<K, V>) -> Option<(&K, &mut V)> {
102 if !alist.is_valid(self) {
103 self.position = alist.position(self.key())?;
104 }
105
106 let (k, v) = &mut alist.pairs[self.position];
107 Some((&*k, v))
108 }
109}
110
111impl<Q, K, V> GetKeyValueMut<K, V> for &Q
112where
113 K: Borrow<Q>,
114 Q: Eq + ?Sized,
115{
116 fn get_key_value_mut(self, alist: &mut AList<K, V>) -> Option<(&K, &mut V)> {
117 alist.find_mut(self)
118 }
119}
120
121pub trait ContainsKey<K, V> {
122 fn contains_key(self, alist: &AList<K, V>) -> bool;
123}
124
125impl<'q, Q, K, V> ContainsKey<K, V> for &mut Bookmark<'q, Q, K, V>
126where
127 K: Borrow<Q>,
128 Q: Eq + ?Sized,
129{
130 fn contains_key(self, alist: &AList<K, V>) -> bool {
131 if !alist.is_valid(self) {
132 match alist.position(self.key()) {
133 Some(position) => self.position = position,
134 None => return false,
135 }
136 }
137
138 true
139 }
140}
141
142impl<Q, K, V> ContainsKey<K, V> for &Q
143where
144 K: Borrow<Q>,
145 Q: Eq + ?Sized,
146{
147 fn contains_key(self, alist: &AList<K, V>) -> bool {
148 alist.pairs.iter().any(|(k, _)| k.borrow() == self)
149 }
150}
151
152pub trait Remove<K, V> {
153 fn remove(self, alist: &mut AList<K, V>) -> Option<V>;
154}
155
156impl<'q, Q, K, V> Remove<K, V> for Bookmark<'q, Q, K, V>
157where
158 K: Borrow<Q>,
159 Q: Eq + ?Sized,
160{
161 fn remove(mut self, alist: &mut AList<K, V>) -> Option<V> {
162 if !alist.is_valid(&self) {
163 self.position = alist.position(self.key())?;
164 }
165
166 let (_, v) = alist.pairs.swap_remove(self.position);
167 Some(v)
168 }
169}
170
171impl<Q, K, V> Remove<K, V> for &Q
172where
173 K: Borrow<Q>,
174 Q: Eq + ?Sized,
175{
176 fn remove(self, alist: &mut AList<K, V>) -> Option<V> {
177 alist.position(self).map(|position| {
178 let (_, v) = alist.pairs.swap_remove(position);
179 v
180 })
181 }
182}
183
184pub trait RemoveEntry<K, V> {
185 fn remove_entry(self, alist: &mut AList<K, V>) -> Option<(K, V)>;
186}
187
188impl<'q, Q, K, V> RemoveEntry<K, V> for Bookmark<'q, Q, K, V>
189where
190 K: Borrow<Q>,
191 Q: Eq + ?Sized,
192{
193 fn remove_entry(mut self, alist: &mut AList<K, V>) -> Option<(K, V)> {
194 if !alist.is_valid(&self) {
195 self.position = alist.position(self.key())?;
196 }
197
198 Some(alist.pairs.swap_remove(self.position))
199 }
200}
201
202impl<Q, K, V> RemoveEntry<K, V> for &Q
203where
204 K: Borrow<Q>,
205 Q: Eq + ?Sized,
206{
207 fn remove_entry(self, alist: &mut AList<K, V>) -> Option<(K, V)> {
208 alist
209 .position(self)
210 .map(|position| alist.pairs.swap_remove(position))
211 }
212}