Skip to content

Commit

Permalink
rename source::Visitor to VisitSource and value::Visitor to VisitValue
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Feb 13, 2024
1 parent 6b483c6 commit d8dc6a8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 66 deletions.
19 changes: 8 additions & 11 deletions src/kv/mod.rs
Expand Up @@ -71,18 +71,18 @@
//! # }
//! ```
//!
//! All key-values can also be enumerated using a [`source::Visitor`]:
//! All key-values can also be enumerated using a [`VisitSource`]:
//!
//! ```
//! # fn main() -> Result<(), log::kv::Error> {
//! # let record = log::Record::builder().key_values(&[("a", 1), ("b", 2), ("c", 3)]).build();
//! use std::collections::BTreeMap;
//!
//! use log::kv::{self, Source, Key, Value, source::Visitor};
//! use log::kv::{self, Source, Key, Value, VisitSource};
//!
//! struct Collect<'kvs>(BTreeMap<Key<'kvs>, Value<'kvs>>);
//!
//! impl<'kvs> Visitor<'kvs> for Collect<'kvs> {
//! impl<'kvs> VisitSource<'kvs> for Collect<'kvs> {
//! fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), kv::Error> {
//! self.0.insert(key, value);
//!
Expand Down Expand Up @@ -125,17 +125,17 @@
//! # }
//! ```
//!
//! Values also have their own [`value::Visitor`] type. Visitors are a lightweight
//! Values also have their own [`VisitValue`] type. Value visitors are a lightweight
//! API for working with primitives types:
//!
//! ```
//! # fn main() -> Result<(), log::kv::Error> {
//! use log::kv::{self, Source, Key, value::Visitor};
//! use log::kv::{self, Source, Key, VisitValue};
//! # let record = log::Record::builder().key_values(&[("a", 1)]).build();
//!
//! struct IsNumeric(bool);
//!
//! impl<'kvs> Visitor<'kvs> for IsNumeric {
//! impl<'kvs> VisitValue<'kvs> for IsNumeric {
//! fn visit_any(&mut self, _value: kv::Value) -> Result<(), kv::Error> {
//! self.0 = false;
//! Ok(())
Expand Down Expand Up @@ -230,12 +230,9 @@
mod error;
mod key;
pub mod source;

pub mod value;

pub use self::error::Error;
pub use self::key::{Key, ToKey};
pub use self::source::{Source, Visitor};

#[doc(inline)]
pub use self::value::{ToValue, Value};
pub use self::source::{Source, VisitSource};
pub use self::value::{ToValue, Value, VisitValue};
62 changes: 31 additions & 31 deletions src/kv/source.rs
Expand Up @@ -9,7 +9,7 @@ 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 [`Visitor`](trait.Visitor.html) trait to inspect the structured data
/// 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
Expand All @@ -21,13 +21,13 @@ use std::fmt;
///
/// ```
/// # fn main() -> Result<(), log::kv::Error> {
/// use log::kv::{self, Source, Key, Value, source::Visitor};
/// use log::kv::{self, Source, Key, Value, source::VisitSource};
///
/// // A `Visitor` that prints all key-values
/// // Visitors are fed the key-value pairs of each key-values
/// // A `VisitSource` that prints all key-values
/// // VisitSources are fed the key-value pairs of each key-values
/// struct Printer;
///
/// impl<'kvs> Visitor<'kvs> for Printer {
/// impl<'kvs> VisitSource<'kvs> for Printer {
/// fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), kv::Error> {
/// println!("{key}: {value}");
///
Expand All @@ -43,7 +43,7 @@ use std::fmt;
/// ("c", 3),
/// ];
///
/// // Pass an instance of the `Visitor` to a `Source` to visit it
/// // Pass an instance of the `VisitSource` to a `Source` to visit it
/// source.visit(&mut Printer)?;
/// # Ok(())
/// # }
Expand All @@ -59,7 +59,7 @@ pub trait Source {
///
/// A source should yield the same key-values to a subsequent visitor unless
/// that visitor itself fails.
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error>;
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error>;

/// Get the value for a given key.
///
Expand Down Expand Up @@ -95,7 +95,7 @@ fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> Option<Value
found: Option<Value<'v>>,
}

impl<'k, 'kvs> Visitor<'kvs> for Get<'k, 'kvs> {
impl<'k, 'kvs> VisitSource<'kvs> for Get<'k, 'kvs> {
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
if self.key == key {
self.found = Some(value);
Expand All @@ -115,7 +115,7 @@ fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> Option<Value
fn count_default(source: impl Source) -> usize {
struct Count(usize);

impl<'kvs> Visitor<'kvs> for Count {
impl<'kvs> VisitSource<'kvs> for Count {
fn visit_pair(&mut self, _: Key<'kvs>, _: Value<'kvs>) -> Result<(), Error> {
self.0 += 1;

Expand All @@ -132,7 +132,7 @@ impl<'a, T> Source for &'a T
where
T: Source + ?Sized,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

Expand All @@ -150,7 +150,7 @@ where
K: ToKey,
V: ToValue,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
visitor.visit_pair(self.0.to_key(), self.1.to_value())
}

Expand All @@ -171,7 +171,7 @@ impl<S> Source for [S]
where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
for source in self {
source.visit(visitor)?;
}
Expand All @@ -198,7 +198,7 @@ impl<const N: usize, S> Source for [S; N]
where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
Source::visit(self as &[_], visitor)
}

Expand All @@ -215,7 +215,7 @@ impl<S> Source for Option<S>
where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
if let Some(source) = self {
source.visit(visitor)?;
}
Expand All @@ -233,42 +233,42 @@ where
}

/// A visitor for the key-value pairs in a [`Source`](trait.Source.html).
pub trait Visitor<'kvs> {
pub trait VisitSource<'kvs> {
/// Visit a key-value pair.
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error>;
}

impl<'a, 'kvs, T> Visitor<'kvs> for &'a mut T
impl<'a, 'kvs, T> VisitSource<'kvs> for &'a mut T
where
T: Visitor<'kvs> + ?Sized,
T: VisitSource<'kvs> + ?Sized,
{
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
(**self).visit_pair(key, value)
}
}

impl<'a, 'b: 'a, 'kvs> Visitor<'kvs> for fmt::DebugMap<'a, 'b> {
impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugMap<'a, 'b> {
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
self.entry(&key, &value);
Ok(())
}
}

impl<'a, 'b: 'a, 'kvs> Visitor<'kvs> for fmt::DebugList<'a, 'b> {
impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugList<'a, 'b> {
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
self.entry(&(key, value));
Ok(())
}
}

impl<'a, 'b: 'a, 'kvs> Visitor<'kvs> for fmt::DebugSet<'a, 'b> {
impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugSet<'a, 'b> {
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
self.entry(&(key, value));
Ok(())
}
}

impl<'a, 'b: 'a, 'kvs> Visitor<'kvs> for fmt::DebugTuple<'a, 'b> {
impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugTuple<'a, 'b> {
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
self.field(&key);
self.field(&value);
Expand All @@ -289,7 +289,7 @@ mod std_support {
where
S: Source + ?Sized,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

Expand All @@ -306,7 +306,7 @@ mod std_support {
where
S: Source + ?Sized,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

Expand All @@ -323,7 +323,7 @@ mod std_support {
where
S: Source + ?Sized,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

Expand All @@ -340,7 +340,7 @@ mod std_support {
where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

Expand All @@ -353,9 +353,9 @@ mod std_support {
}
}

impl<'kvs, V> Visitor<'kvs> for Box<V>
impl<'kvs, V> VisitSource<'kvs> for Box<V>
where
V: Visitor<'kvs> + ?Sized,
V: VisitSource<'kvs> + ?Sized,
{
fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
(**self).visit_pair(key, value)
Expand All @@ -368,7 +368,7 @@ mod std_support {
V: ToValue,
S: BuildHasher,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
for (key, value) in self {
visitor.visit_pair(key.to_key(), value.to_value())?;
}
Expand All @@ -389,7 +389,7 @@ mod std_support {
K: ToKey + Borrow<str> + Ord,
V: ToValue,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
for (key, value) in self {
visitor.visit_pair(key.to_key(), value.to_value())?;
}
Expand Down Expand Up @@ -472,7 +472,7 @@ mod tests {

#[test]
fn visitor_is_object_safe() {
fn _check(_: &dyn Visitor) {}
fn _check(_: &dyn VisitSource) {}
}

#[test]
Expand All @@ -483,7 +483,7 @@ mod tests {
}

impl Source for OnePair {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
visitor.visit_pair(self.key.to_key(), self.value.to_value())
}
}
Expand Down

0 comments on commit d8dc6a8

Please sign in to comment.