//! Sources for key-values. //! //! This module defines the [`Source`] type and supporting APIs for //! working with collections of key-values.
usecrate::kv::{Error, Key, ToKey, ToValue, Value}; use std::fmt;
/// A source of key-values. /// /// The source may be a single pair, a set of pairs, or a filter over a set of pairs. /// Use the [`VisitSource`](trait.VisitSource.html) trait to inspect the structured data /// in a source. /// /// A source is like an iterator over its key-values, except with a push-based API /// instead of a pull-based one. /// /// # Examples /// /// Enumerating the key-values in a source: /// /// ``` /// # fn main() -> Result<(), log::kv::Error> { /// use log::kv::{self, Source, Key, Value, VisitSource}; /// /// // A `VisitSource` that prints all key-values /// // VisitSources are fed the key-value pairs of each key-values /// struct Printer; /// /// impl<'kvs> VisitSource<'kvs> for Printer { /// fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), kv::Error> { /// println!("{key}: {value}"); /// /// Ok(()) /// } /// } /// /// // A source with 3 key-values /// // Common collection types implement the `Source` trait /// let source = &[ /// ("a", 1), /// ("b", 2), /// ("c", 3), /// ]; /// /// // Pass an instance of the `VisitSource` to a `Source` to visit it /// source.visit(&mut Printer)?; /// # Ok(()) /// # } /// ``` pubtrait Source { /// Visit key-values. /// /// A source doesn't have to guarantee any ordering or uniqueness of key-values. /// If the given visitor returns an error then the source may early-return with it, /// even if there are more key-values. /// /// # Implementation notes /// /// A source should yield the same key-values to a subsequent visitor unless /// that visitor itself fails. fn visit<'kvs>(&'kvs self, visitor: &mutdyn VisitSource<'kvs>) -> Result<(), Error>;
/// Get the value for a given key. /// /// If the key appears multiple times in the source then which key is returned /// is implementation specific. /// /// # Implementation notes /// /// A source that can provide a more efficient implementation of this method /// should override it. fn get(&self, key: Key) -> Option<Value<'_>> {
get_default(self, key)
}
/// Count the number of key-values that can be visited. /// /// # Implementation notes /// /// A source that knows the number of key-values upfront may provide a more /// efficient implementation. /// /// A subsequent call to `visit` should yield the same number of key-values. fn count(&self) -> usize {
count_default(self)
}
}
/// A visitor for the key-value pairs in a [`Source`](trait.Source.html). pubtrait VisitSource<'kvs> { /// Visit a key-value pair. fn visit_pair(&mutself, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error>;
}
#[allow(clippy::needless_lifetimes)] // Not needless. impl<'a, 'kvs, T> VisitSource<'kvs> for &'a mut T where
T: VisitSource<'kvs> + ?Sized,
{ fn visit_pair(&mutself, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
(**self).visit_pair(key, value)
}
}
#[cfg(feature = "std")] mod std_support { usesuper::*; use std::borrow::Borrow; use std::collections::{BTreeMap, HashMap}; use std::hash::{BuildHasher, Hash}; use std::rc::Rc; use std::sync::Arc;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.