1use crate::kv::{Error, Key, ToKey, ToValue, Value};
7use std::fmt;
8
9pub trait Source {
52 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error>;
63
64 fn get(&self, key: Key) -> Option<Value<'_>> {
74 get_default(self, key)
75 }
76
77 fn count(&self) -> usize {
86 count_default(self)
87 }
88}
89
90fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> Option<Value<'v>> {
92 struct Get<'k, 'v> {
93 key: Key<'k>,
94 found: Option<Value<'v>>,
95 }
96
97 impl<'k, 'kvs> VisitSource<'kvs> for Get<'k, 'kvs> {
98 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
99 if self.key == key {
100 self.found = Some(value);
101 }
102
103 Ok(())
104 }
105 }
106
107 let mut get = Get { key, found: None };
108
109 let _ = source.visit(&mut get);
110 get.found
111}
112
113fn count_default(source: impl Source) -> usize {
115 struct Count(usize);
116
117 impl<'kvs> VisitSource<'kvs> for Count {
118 fn visit_pair(&mut self, _: Key<'kvs>, _: Value<'kvs>) -> Result<(), Error> {
119 self.0 += 1;
120
121 Ok(())
122 }
123 }
124
125 let mut count = Count(0);
126 let _ = source.visit(&mut count);
127 count.0
128}
129
130impl<'a, T> Source for &'a T
131where
132 T: Source + ?Sized,
133{
134 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
135 Source::visit(&**self, visitor)
136 }
137
138 fn get(&self, key: Key) -> Option<Value<'_>> {
139 Source::get(&**self, key)
140 }
141
142 fn count(&self) -> usize {
143 Source::count(&**self)
144 }
145}
146
147impl<K, V> Source for (K, V)
148where
149 K: ToKey,
150 V: ToValue,
151{
152 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
153 visitor.visit_pair(self.0.to_key(), self.1.to_value())
154 }
155
156 fn get(&self, key: Key) -> Option<Value<'_>> {
157 if self.0.to_key() == key {
158 Some(self.1.to_value())
159 } else {
160 None
161 }
162 }
163
164 fn count(&self) -> usize {
165 1
166 }
167}
168
169impl<S> Source for [S]
170where
171 S: Source,
172{
173 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
174 for source in self {
175 source.visit(visitor)?;
176 }
177
178 Ok(())
179 }
180
181 fn get(&self, key: Key) -> Option<Value<'_>> {
182 for source in self {
183 if let Some(found) = source.get(key.clone()) {
184 return Some(found);
185 }
186 }
187
188 None
189 }
190
191 fn count(&self) -> usize {
192 self.iter().map(Source::count).sum()
193 }
194}
195
196impl<const N: usize, S> Source for [S; N]
197where
198 S: Source,
199{
200 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
201 Source::visit(self as &[_], visitor)
202 }
203
204 fn get(&self, key: Key) -> Option<Value<'_>> {
205 Source::get(self as &[_], key)
206 }
207
208 fn count(&self) -> usize {
209 Source::count(self as &[_])
210 }
211}
212
213impl<S> Source for Option<S>
214where
215 S: Source,
216{
217 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
218 if let Some(source) = self {
219 source.visit(visitor)?;
220 }
221
222 Ok(())
223 }
224
225 fn get(&self, key: Key) -> Option<Value<'_>> {
226 self.as_ref().and_then(|s| s.get(key))
227 }
228
229 fn count(&self) -> usize {
230 self.as_ref().map_or(0, Source::count)
231 }
232}
233
234pub trait VisitSource<'kvs> {
236 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error>;
238}
239
240impl<'a, 'kvs, T> VisitSource<'kvs> for &'a mut T
241where
242 T: VisitSource<'kvs> + ?Sized,
243{
244 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
245 (**self).visit_pair(key, value)
246 }
247}
248
249impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugMap<'a, 'b> {
250 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
251 self.entry(&key, &value);
252 Ok(())
253 }
254}
255
256impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugList<'a, 'b> {
257 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
258 self.entry(&(key, value));
259 Ok(())
260 }
261}
262
263impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugSet<'a, 'b> {
264 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
265 self.entry(&(key, value));
266 Ok(())
267 }
268}
269
270impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugTuple<'a, 'b> {
271 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
272 self.field(&key);
273 self.field(&value);
274 Ok(())
275 }
276}
277
278#[cfg(feature = "std")]
279mod std_support {
280 use super::*;
281 use std::borrow::Borrow;
282 use std::collections::{BTreeMap, HashMap};
283 use std::hash::{BuildHasher, Hash};
284 use std::rc::Rc;
285 use std::sync::Arc;
286
287 impl<S> Source for Box<S>
288 where
289 S: Source + ?Sized,
290 {
291 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
292 Source::visit(&**self, visitor)
293 }
294
295 fn get(&self, key: Key) -> Option<Value<'_>> {
296 Source::get(&**self, key)
297 }
298
299 fn count(&self) -> usize {
300 Source::count(&**self)
301 }
302 }
303
304 impl<S> Source for Arc<S>
305 where
306 S: Source + ?Sized,
307 {
308 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
309 Source::visit(&**self, visitor)
310 }
311
312 fn get(&self, key: Key) -> Option<Value<'_>> {
313 Source::get(&**self, key)
314 }
315
316 fn count(&self) -> usize {
317 Source::count(&**self)
318 }
319 }
320
321 impl<S> Source for Rc<S>
322 where
323 S: Source + ?Sized,
324 {
325 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
326 Source::visit(&**self, visitor)
327 }
328
329 fn get(&self, key: Key) -> Option<Value<'_>> {
330 Source::get(&**self, key)
331 }
332
333 fn count(&self) -> usize {
334 Source::count(&**self)
335 }
336 }
337
338 impl<S> Source for Vec<S>
339 where
340 S: Source,
341 {
342 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
343 Source::visit(&**self, visitor)
344 }
345
346 fn get(&self, key: Key) -> Option<Value<'_>> {
347 Source::get(&**self, key)
348 }
349
350 fn count(&self) -> usize {
351 Source::count(&**self)
352 }
353 }
354
355 impl<'kvs, V> VisitSource<'kvs> for Box<V>
356 where
357 V: VisitSource<'kvs> + ?Sized,
358 {
359 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
360 (**self).visit_pair(key, value)
361 }
362 }
363
364 impl<K, V, S> Source for HashMap<K, V, S>
365 where
366 K: ToKey + Borrow<str> + Eq + Hash,
367 V: ToValue,
368 S: BuildHasher,
369 {
370 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
371 for (key, value) in self {
372 visitor.visit_pair(key.to_key(), value.to_value())?;
373 }
374 Ok(())
375 }
376
377 fn get(&self, key: Key) -> Option<Value<'_>> {
378 HashMap::get(self, key.as_str()).map(|v| v.to_value())
379 }
380
381 fn count(&self) -> usize {
382 self.len()
383 }
384 }
385
386 impl<K, V> Source for BTreeMap<K, V>
387 where
388 K: ToKey + Borrow<str> + Ord,
389 V: ToValue,
390 {
391 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
392 for (key, value) in self {
393 visitor.visit_pair(key.to_key(), value.to_value())?;
394 }
395 Ok(())
396 }
397
398 fn get(&self, key: Key) -> Option<Value<'_>> {
399 BTreeMap::get(self, key.as_str()).map(|v| v.to_value())
400 }
401
402 fn count(&self) -> usize {
403 self.len()
404 }
405 }
406
407 #[cfg(test)]
408 mod tests {
409 use crate::kv::value;
410
411 use super::*;
412
413 #[test]
414 fn count() {
415 assert_eq!(1, Source::count(&Box::new(("a", 1))));
416 assert_eq!(2, Source::count(&vec![("a", 1), ("b", 2)]));
417 }
418
419 #[test]
420 fn get() {
421 let source = vec![("a", 1), ("b", 2), ("a", 1)];
422 assert_eq!(
423 value::inner::Token::I64(1),
424 Source::get(&source, Key::from_str("a")).unwrap().to_token()
425 );
426
427 let source = Box::new(None::<(&str, i32)>);
428 assert!(Source::get(&source, Key::from_str("a")).is_none());
429 }
430
431 #[test]
432 fn hash_map() {
433 let mut map = HashMap::new();
434 map.insert("a", 1);
435 map.insert("b", 2);
436
437 assert_eq!(2, Source::count(&map));
438 assert_eq!(
439 value::inner::Token::I64(1),
440 Source::get(&map, Key::from_str("a")).unwrap().to_token()
441 );
442 }
443
444 #[test]
445 fn btree_map() {
446 let mut map = BTreeMap::new();
447 map.insert("a", 1);
448 map.insert("b", 2);
449
450 assert_eq!(2, Source::count(&map));
451 assert_eq!(
452 value::inner::Token::I64(1),
453 Source::get(&map, Key::from_str("a")).unwrap().to_token()
454 );
455 }
456 }
457}
458
459#[cfg(feature = "kv_unstable")]
461pub use VisitSource as Visitor;
462
463#[cfg(test)]
464mod tests {
465 use crate::kv::value;
466
467 use super::*;
468
469 #[test]
470 fn source_is_object_safe() {
471 fn _check(_: &dyn Source) {}
472 }
473
474 #[test]
475 fn visitor_is_object_safe() {
476 fn _check(_: &dyn VisitSource) {}
477 }
478
479 #[test]
480 fn count() {
481 struct OnePair {
482 key: &'static str,
483 value: i32,
484 }
485
486 impl Source for OnePair {
487 fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
488 visitor.visit_pair(self.key.to_key(), self.value.to_value())
489 }
490 }
491
492 assert_eq!(1, Source::count(&("a", 1)));
493 assert_eq!(2, Source::count(&[("a", 1), ("b", 2)] as &[_]));
494 assert_eq!(0, Source::count(&None::<(&str, i32)>));
495 assert_eq!(1, Source::count(&OnePair { key: "a", value: 1 }));
496 }
497
498 #[test]
499 fn get() {
500 let source = &[("a", 1), ("b", 2), ("a", 1)] as &[_];
501 assert_eq!(
502 value::inner::Token::I64(1),
503 Source::get(source, Key::from_str("a")).unwrap().to_token()
504 );
505 assert_eq!(
506 value::inner::Token::I64(2),
507 Source::get(source, Key::from_str("b")).unwrap().to_token()
508 );
509 assert!(Source::get(&source, Key::from_str("c")).is_none());
510
511 let source = None::<(&str, i32)>;
512 assert!(Source::get(&source, Key::from_str("a")).is_none());
513 }
514}