• Home
  • Raw
  • Download

Lines Matching +full:source +full:- +full:map

1 //! Sources for key-value pairs.
12 /// A source of key-value pairs.
14 /// The source may be a single pair, a set of pairs, or a filter over a set of pairs.
16 /// in a source.
17 pub trait Source { interface
18 /// Visit key-value pairs.
20 /// A source doesn't have to guarantee any ordering or uniqueness of key-value pairs.
21 /// If the given visitor returns an error then the source may early-return with it,
22 /// even if there are more key-value pairs.
26 /// A source should yield the same key-value pairs to a subsequent visitor unless
28 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error>; in visit()
32 /// If the key appears multiple times in the source then which key is returned
37 /// A source that can provide a more efficient implementation of this method
40 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
45 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>>; in get()
47 /// Count the number of key-value pairs that can be visited.
51 /// A source that knows the number of key-value pairs upfront may provide a more
54 /// A subsequent call to `visit` should yield the same number of key-value pairs
57 fn count(&self) -> usize { in count()
62 fn count(&self) -> usize; in count()
65 /// The default implemention of `Source::get`
66 pub(crate) fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> Option<Value<'v>> { in get_default()
73 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in get_default()
84 let _ = source.visit(&mut get); in get_default()
88 /// The default implementation of `Source::count`.
89 pub(crate) fn count_default(source: impl Source) -> usize { in count_default()
93 fn visit_pair(&mut self, _: Key<'kvs>, _: Value<'kvs>) -> Result<(), Error> { in count_default()
101 let _ = source.visit(&mut count); in count_default()
105 impl<'a, T> Source for &'a T impl
107 T: Source + ?Sized,
109 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
110 Source::visit(&**self, visitor) in visit()
113 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
114 Source::get(&**self, key) in get()
117 fn count(&self) -> usize { in count()
118 Source::count(&**self) in count()
122 impl<K, V> Source for (K, V) impl
127 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
131 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
139 fn count(&self) -> usize { in count()
144 impl<S> Source for [S] impl
146 S: Source,
148 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
149 for source in self { in visit()
150 source.visit(visitor)?; in visit()
156 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
157 for source in self { in get()
158 if let Some(found) = source.get(key.clone()) { in get()
166 fn count(&self) -> usize { in count()
171 impl<S> Source for Option<S>
173 S: Source,
175 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
176 if let Some(ref source) = *self { in visit()
177 source.visit(visitor)?; in visit()
183 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
187 fn count(&self) -> usize { in count()
188 self.as_ref().map(Source::count).unwrap_or(0) in count()
192 /// A visitor for the key-value pairs in a [`Source`](trait.Source.html).
194 /// Visit a key-value pair.
195 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error>; in visit_pair()
202 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in visit_pair()
208 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in visit_pair()
215 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in visit_pair()
222 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in visit_pair()
229 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in visit_pair()
243 impl<S> Source for Box<S>
245 S: Source + ?Sized,
247 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
248 Source::visit(&**self, visitor) in visit()
251 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
252 Source::get(&**self, key) in get()
255 fn count(&self) -> usize { in count()
256 Source::count(&**self) in count()
260 impl<S> Source for Vec<S>
262 S: Source,
264 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
265 Source::visit(&**self, visitor) in visit()
268 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
269 Source::get(&**self, key) in get()
272 fn count(&self) -> usize { in count()
273 Source::count(&**self) in count()
281 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in visit_pair()
286 impl<K, V, S> Source for HashMap<K, V, S>
292 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
299 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
300 HashMap::get(self, key.as_str()).map(|v| v.to_value()) in get()
303 fn count(&self) -> usize { in count()
308 impl<K, V> Source for BTreeMap<K, V>
313 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
320 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
321 BTreeMap::get(self, key.as_str()).map(|v| v.to_value()) in get()
324 fn count(&self) -> usize { in count()
337 assert_eq!(1, Source::count(&Box::new(("a", 1)))); in count()
338 assert_eq!(2, Source::count(&vec![("a", 1), ("b", 2)])); in count()
343 let source = vec![("a", 1), ("b", 2), ("a", 1)]; in get() localVariable
346 Source::get(&source, Key::from_str("a")).unwrap().to_token() in get()
349 let source = Box::new(Option::None::<(&str, i32)>); in get() localVariable
350 assert!(Source::get(&source, Key::from_str("a")).is_none()); in get()
355 let mut map = HashMap::new(); in hash_map() localVariable
356 map.insert("a", 1); in hash_map()
357 map.insert("b", 2); in hash_map()
359 assert_eq!(2, Source::count(&map)); in hash_map()
362 Source::get(&map, Key::from_str("a")).unwrap().to_token() in hash_map()
368 let mut map = BTreeMap::new(); in btree_map() localVariable
369 map.insert("a", 1); in btree_map()
370 map.insert("b", 2); in btree_map()
372 assert_eq!(2, Source::count(&map)); in btree_map()
375 Source::get(&map, Key::from_str("a")).unwrap().to_token() in btree_map()
381 /// The result of calling `Source::as_map`.
384 /// Visit this source as a map.
385 pub fn as_map<S>(source: S) -> AsMap<S> in as_map()
387 S: Source, in as_map()
389 AsMap(source) in as_map()
392 impl<S> Source for AsMap<S>
394 S: Source,
396 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
400 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
404 fn count(&self) -> usize { in count()
411 S: Source,
413 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { in fmt()
420 /// The result of calling `Source::as_list`
423 /// Visit this source as a list.
424 pub fn as_list<S>(source: S) -> AsList<S> in as_list()
426 S: Source, in as_list()
428 AsList(source) in as_list()
431 impl<S> Source for AsList<S>
433 S: Source,
435 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in visit()
439 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in get()
443 fn count(&self) -> usize { in count()
450 S: Source,
452 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { in fmt()
467 S: Source,
469 fn stream(&self, stream: &mut value::Stream) -> value::Result { in stream()
473 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in stream()
476 .map_err(|_| Error::msg("failed to stream map key"))?; in stream()
479 .map_err(|_| Error::msg("failed to stream map value"))?; in stream()
486 .map_err(|_| self::sval::Error::msg("failed to begin map"))?; in stream()
489 .map_err(|_| self::sval::Error::msg("failed to visit key-values"))?; in stream()
493 .map_err(|_| self::sval::Error::msg("failed to end map")) in stream()
499 S: Source,
501 fn stream(&self, stream: &mut value::Stream) -> value::Result { in stream()
505 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in stream()
518 .map_err(|_| self::sval::Error::msg("failed to visit key-values"))?; in stream()
532 use crate::kv::source;
539 kvs: source::AsMap<&'a dyn Source>, in derive_stream()
545 kvs: source::AsList<&'a dyn Source>, in derive_stream()
553 //! `serde` adapters for serializing a `Source` as a map.
559 /// Serialize a `Source` as a map.
560 pub fn serialize<T, S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> in serialize()
562 T: Source, in serialize() argument
565 as_map(source).serialize(serializer) in serialize()
571 //! `serde` adapters for serializing a `Source` as a list.
577 /// Serialize a `Source` as a list.
578 pub fn serialize<T, S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> in serialize()
580 T: Source, in serialize() argument
583 as_list(source).serialize(serializer) in serialize()
595 T: Source,
597 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> in serialize()
607 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in serialize()
610 .map_err(|_| Error::msg("failed to serialize map entry"))?; in serialize()
615 let mut map = serializer.serialize_map(Some(self.count()))?; in serialize() localVariable
617 self.visit(&mut SerializerVisitor(&mut map)) in serialize()
618 .map_err(|_| S::Error::custom("failed to visit key-values"))?; in serialize()
620 map.end() in serialize()
626 T: Source,
628 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> in serialize()
638 fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> { in serialize()
661 use crate::kv::source;
669 #[serde(with = "source::as_map")] in derive_serialize()
670 kvs: &'a dyn Source, in derive_serialize()
677 #[serde(with = "source::as_list")] in derive_serialize()
678 kvs: &'a dyn Source, in derive_serialize()
691 fn _check(_: &dyn Source) {} in source_is_object_safe() argument
706 impl Source for OnePair { in count()
707 fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> { in count()
711 fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> { in count()
715 fn count(&self) -> usize { in count()
720 assert_eq!(1, Source::count(&("a", 1))); in count()
721 assert_eq!(2, Source::count(&[("a", 1), ("b", 2)] as &[_])); in count()
722 assert_eq!(0, Source::count(&Option::None::<(&str, i32)>)); in count()
723 assert_eq!(1, Source::count(&OnePair { key: "a", value: 1 })); in count()
728 let source = &[("a", 1), ("b", 2), ("a", 1)] as &[_]; in get() localVariable
731 Source::get(source, Key::from_str("a")).unwrap().to_token() in get()
735 Source::get(source, Key::from_str("b")).unwrap().to_token() in get()
737 assert!(Source::get(&source, Key::from_str("c")).is_none()); in get()
739 let source = Option::None::<(&str, i32)>; in get() localVariable
740 assert!(Source::get(&source, Key::from_str("a")).is_none()); in get()
745 let _ = crate::kv::source::as_map(("a", 1)); in as_map()
746 let _ = crate::kv::source::as_map(&("a", 1) as &dyn Source); in as_map()
751 let _ = crate::kv::source::as_list(("a", 1)); in as_list()
752 let _ = crate::kv::source::as_list(&("a", 1) as &dyn Source); in as_list()