• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::collections::hash_map;
2 use std::collections::HashMap;
3 use std::hash::Hash;
4 
5 use super::value::ProtobufValue;
6 
7 /// Implemented for `HashMap` with appropriate keys and values
8 pub trait ReflectMap: 'static {
reflect_iter(&self) -> ReflectMapIter9     fn reflect_iter(&self) -> ReflectMapIter;
10 
len(&self) -> usize11     fn len(&self) -> usize;
12 }
13 
14 impl<K: ProtobufValue + Eq + Hash + 'static, V: ProtobufValue + 'static> ReflectMap
15     for HashMap<K, V>
16 {
reflect_iter<'a>(&'a self) -> ReflectMapIter<'a>17     fn reflect_iter<'a>(&'a self) -> ReflectMapIter<'a> {
18         ReflectMapIter {
19             imp: Box::new(ReflectMapIterImpl::<'a, K, V> { iter: self.iter() }),
20         }
21     }
22 
len(&self) -> usize23     fn len(&self) -> usize {
24         HashMap::len(self)
25     }
26 }
27 
28 trait ReflectMapIterTrait<'a> {
next(&mut self) -> Option<(&'a dyn ProtobufValue, &'a dyn ProtobufValue)>29     fn next(&mut self) -> Option<(&'a dyn ProtobufValue, &'a dyn ProtobufValue)>;
30 }
31 
32 struct ReflectMapIterImpl<'a, K: Eq + Hash + 'static, V: 'static> {
33     iter: hash_map::Iter<'a, K, V>,
34 }
35 
36 impl<'a, K: ProtobufValue + Eq + Hash + 'static, V: ProtobufValue + 'static> ReflectMapIterTrait<'a>
37     for ReflectMapIterImpl<'a, K, V>
38 {
next(&mut self) -> Option<(&'a dyn ProtobufValue, &'a dyn ProtobufValue)>39     fn next(&mut self) -> Option<(&'a dyn ProtobufValue, &'a dyn ProtobufValue)> {
40         match self.iter.next() {
41             Some((k, v)) => Some((k as &dyn ProtobufValue, v as &dyn ProtobufValue)),
42             None => None,
43         }
44     }
45 }
46 
47 pub struct ReflectMapIter<'a> {
48     imp: Box<dyn ReflectMapIterTrait<'a> + 'a>,
49 }
50 
51 impl<'a> Iterator for ReflectMapIter<'a> {
52     type Item = (&'a dyn ProtobufValue, &'a dyn ProtobufValue);
53 
next(&mut self) -> Option<(&'a dyn ProtobufValue, &'a dyn ProtobufValue)>54     fn next(&mut self) -> Option<(&'a dyn ProtobufValue, &'a dyn ProtobufValue)> {
55         self.imp.next()
56     }
57 }
58 
59 impl<'a> IntoIterator for &'a dyn ReflectMap {
60     type IntoIter = ReflectMapIter<'a>;
61     type Item = (&'a dyn ProtobufValue, &'a dyn ProtobufValue);
62 
into_iter(self) -> Self::IntoIter63     fn into_iter(self) -> Self::IntoIter {
64         self.reflect_iter()
65     }
66 }
67