Use .then() instead of .after() for sequencing nodes (#760)

* Add .then() for sequencing nodes

* Remove all uses af after

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
0HyperCube
2022-08-28 20:52:46 +01:00
committed by Keavon Chambers
parent c28d618291
commit f5e8c48dfb
4 changed files with 40 additions and 37 deletions

View File

@@ -167,27 +167,27 @@ mod test {
#[test]
pub fn dup_node() {
let value = ValueNode(4u32);
let dup = DupNode.after(value);
let dup = value.then(DupNode);
assert_eq!(dup.eval(()), (4, 4));
}
#[test]
pub fn id_node() {
let value = IdNode.after(ValueNode(4u32));
let value = ValueNode(4u32).then(IdNode);
assert_eq!(value.eval(()), 4);
}
#[test]
pub fn clone_node() {
let cloned = CloneNode.after(&ValueNode(4u32));
let cloned = (&ValueNode(4u32)).then(CloneNode);
assert_eq!(cloned.eval(()), 4);
}
#[test]
pub fn fst_node() {
let fst = FstNode.after(ValueNode((4u32, "a")));
let fst = ValueNode((4u32, "a")).then(FstNode);
assert_eq!(fst.eval(()), 4);
}
#[test]
pub fn snd_node() {
let fst = SndNode.after(ValueNode((4u32, "a")));
let fst = ValueNode((4u32, "a")).then(SndNode);
assert_eq!(fst.eval(()), "a");
}
#[test]
@@ -195,7 +195,8 @@ mod test {
let a = ValueNode(42u32);
let b = ValueNode(6u32);
let cons_a = ConsNode(a);
let sum = AddNode.after(cons_a).after(b);
let sum = b.then(cons_a).then(AddNode);
assert_eq!(sum.eval(()), 48);
}

View File

@@ -64,54 +64,56 @@ impl<'n, Input, First: 'n, Second: 'n> ComposeNode<First, Second, Input> {
ComposeNode::<First, Second, Input> { first, second, _phantom: PhantomData }
}
}
pub trait After<Inter>: Sized {
fn after<First, Input>(self, first: First) -> ComposeNode<First, Self, Input>
pub trait Then<Inter, Input>: Sized {
fn then<Second>(self, second: Second) -> ComposeNode<Self, Second, Input>
where
First: Node<Input, Output = Inter>,
Self: Node<Inter>,
Self: Node<Input, Output = Inter>,
Second: Node<Inter>,
{
ComposeNode::<First, Self, Input> {
first,
second: self,
ComposeNode::<Self, Second, Input> {
first: self,
second,
_phantom: PhantomData,
}
}
}
impl<Second: Node<I>, I> After<I> for Second {}
pub trait AfterRef<Inter>: Sized {
fn after<'n, First: 'n, Input>(&'n self, first: First) -> ComposeNode<First, &'n Self, Input>
impl<First: Node<Input, Output = Inter>, Inter, Input> Then<Inter, Input> for First {}
pub trait ThenRef<Inter, Input>: Sized {
fn after<'n, Second: 'n>(&'n self, second: Second) -> ComposeNode<&'n Self, Second, Input>
where
First: Node<Input, Output = Inter> + Copy,
&'n Self: Node<Inter>,
&'n Self: Node<Input, Output = Inter> + Copy,
Second: Node<Inter>,
Self: 'n,
{
ComposeNode::<First, &'n Self, Input> {
first,
second: self,
ComposeNode::<&'n Self, Second, Input> {
first: self,
second,
_phantom: PhantomData,
}
}
}
impl<'n, Second: 'n, I> AfterRef<I> for Second where &'n Second: Node<I> {}
impl<'n, First: 'n, Inter, Input> ThenRef<Inter, Input> for First where &'n First: Node<Input, Output = Inter> {}
#[cfg(feature = "async")]
pub trait AfterBox<Inter> {
fn after<'n, First: 'n, Input>(self, first: First) -> ComposeNode<First, Self, Input>
pub trait ThenBox<Inter, Input> {
fn then<'n, Second: 'n>(self, second: Second) -> ComposeNode<Self, Second, Input>
where
First: Node<Input, Output = Inter> + Copy,
alloc::boxed::Box<Self>: Node<Inter>,
alloc::boxed::Box<Self>: Node<Input, Output = Inter>,
Second: Node<Inter> + Copy,
Self: Sized,
{
ComposeNode::<First, Self, Input> {
first,
second: self,
ComposeNode::<Self, Second, Input> {
first: self,
second,
_phantom: PhantomData,
}
}
}
#[cfg(feature = "async")]
impl<'n, Second: 'n, I> AfterBox<I> for alloc::boxed::Box<Second> where &'n alloc::boxed::Box<Second>: Node<I> {}
impl<'n, First: 'n, Inter, Input> ThenBox<Inter, Input> for alloc::boxed::Box<First> where &'n alloc::boxed::Box<First>: Node<Input, Output = Inter> {}
pub struct ConsNode<Root>(pub Root);