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§
Sourcefn visit<'kvs>(
&'kvs self,
visitor: &mut dyn VisitSource<'kvs>,
) -> Result<(), Error>
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§
Sourcefn get(&self, key: Key<'_>) -> Option<Value<'_>>
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.