Trait log::kv::Source

source ·
pub trait Source {
    // Required method
    fn visit<'kvs>(
        &'kvs self,
        visitor: &mut dyn VisitSource<'kvs>
    ) -> Result<(), Error>;

    // Provided methods
    fn get(&self, key: Key<'_>) -> Option<Value<'_>> { ... }
    fn count(&self) -> usize { ... }
}
Expand description

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 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:

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)?;

Required Methods§

source

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

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.

Provided Methods§

source

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

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.

source

fn count(&self) -> usize

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.

Implementations on Foreign Types§

source§

impl<'a, T> Source for &'a T
where T: Source + ?Sized,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<K, V> Source for (K, V)
where K: ToKey, V: ToValue,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<K, V> Source for BTreeMap<K, V>
where K: ToKey + Borrow<str> + Ord, V: ToValue,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<K, V, S> Source for HashMap<K, V, S>
where K: ToKey + Borrow<str> + Eq + Hash, V: ToValue, S: BuildHasher,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<S> Source for Option<S>
where S: Source,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<S> Source for [S]
where S: Source,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<S> Source for Box<S>
where S: Source + ?Sized,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<S> Source for Rc<S>
where S: Source + ?Sized,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<S> Source for Arc<S>
where S: Source + ?Sized,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<S> Source for Vec<S>
where S: Source,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

source§

impl<const N: usize, S> Source for [S; N]
where S: Source,

source§

fn visit<'kvs>( &'kvs self, visitor: &mut dyn VisitSource<'kvs> ) -> Result<(), Error>

source§

fn get(&self, key: Key<'_>) -> Option<Value<'_>>

source§

fn count(&self) -> usize

Implementors§