Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions traversable/src/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ pub trait VisitorExt: Visitor {
/// });
///
/// // v1 runs first, then v2.
/// let mut combined = v1.or(v2);
/// let mut combined = v1.chain(v2);
/// data.traverse(&mut combined);
/// # }
/// ```
fn or<V>(self, other: V) -> OrVisitor<Self, V>
fn chain<V>(self, other: V) -> Chain<Self, V>
where
Self: Sized,
V: Visitor<Break = Self::Break>,
{
OrVisitor {
Chain {
visitor1: self,
visitor2: other,
}
Expand Down Expand Up @@ -136,19 +136,19 @@ pub trait VisitorMutExt: VisitorMut {
/// ControlFlow::<()>::Continue(())
/// });
///
/// let mut combined = v1.or(v2);
/// let mut combined = v1.chain(v2);
/// data.traverse_mut(&mut combined);
///
/// assert_eq!(data.foo.0, 2);
/// assert_eq!(data.bar.0, 4);
/// # }
/// ```
fn or<V>(self, other: V) -> OrVisitor<Self, V>
fn chain<V>(self, other: V) -> Chain<Self, V>
where
Self: Sized,
V: VisitorMut<Break = Self::Break>,
{
OrVisitor {
Chain {
visitor1: self,
visitor2: other,
}
Expand All @@ -159,14 +159,13 @@ impl<V: VisitorMut> VisitorMutExt for V {}

/// A visitor that runs two visitors in sequence.
///
/// This struct is created by the [`or`](VisitorExt::or) method on [`VisitorExt`] or
/// [`VisitorMutExt`].
pub struct OrVisitor<V1, V2> {
/// This struct is created by [`VisitorExt::chain`] or [`VisitorMutExt::chain`].
pub struct Chain<V1, V2> {
visitor1: V1,
visitor2: V2,
}

impl<V1, V2> Visitor for OrVisitor<V1, V2>
impl<V1, V2> Visitor for Chain<V1, V2>
where
V1: Visitor,
V2: Visitor<Break = V1::Break>,
Expand All @@ -184,7 +183,7 @@ where
}
}

impl<V1, V2> VisitorMut for OrVisitor<V1, V2>
impl<V1, V2> VisitorMut for Chain<V1, V2>
where
V1: VisitorMut,
V2: VisitorMut<Break = V1::Break>,
Expand Down
8 changes: 4 additions & 4 deletions traversable/tests/test_combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn test_visitor_or() {
break_on: None,
};

let mut combined = v1.or(v2);
let mut combined = v1.chain(v2);
let result = data.traverse(&mut combined);
assert!(result.is_continue());

Expand Down Expand Up @@ -126,7 +126,7 @@ fn test_visitor_or_break_v1() {
break_on: None,
};

let mut combined = v1.or(v2);
let mut combined = v1.chain(v2);
let result = data.traverse(&mut combined);
assert_eq!(result, ControlFlow::Break(2));

Expand Down Expand Up @@ -161,7 +161,7 @@ fn test_visitor_or_break_v2() {
break_on: Some(2),
};

let mut combined = v1.or(v2);
let mut combined = v1.chain(v2);
let result = data.traverse(&mut combined);
assert_eq!(result, ControlFlow::Break(2));

Expand Down Expand Up @@ -201,7 +201,7 @@ fn test_visitor_mut_or() {
let v1 = MutVisitor { val_multiplier: 2 };
let v2 = MutVisitor { val_multiplier: 3 };

let mut combined = v1.or(v2);
let mut combined = v1.chain(v2);
let result = data.traverse_mut(&mut combined);
assert!(result.is_continue());

Expand Down