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

View file

@ -105,7 +105,7 @@ mod test {
let value = value.as_owned();
/*let computation = ComposeNode::new(value, add);
let computation = id.after(add.after(value));
let computation = value.then(add).then(id);
let result: u32 = *dyn_any::downcast(computation.eval(&())).unwrap();*/
}*/
#[test]

View file

@ -2,7 +2,7 @@ use core::marker::PhantomData;
use graphene_core::ops::FlatMapResultNode;
use graphene_core::raster::color::Color;
use graphene_core::structural::{ComposeNode, ConsNode};
use graphene_core::{generic::FnNode, ops::MapResultNode, structural::After, value::ValueNode, Node};
use graphene_core::{generic::FnNode, ops::MapResultNode, structural::Then, value::ValueNode, Node};
use image::Pixel;
use std::path::Path;
@ -112,15 +112,15 @@ impl<'a> IntoIterator for &'a Image {
pub fn file_node<'n, P: AsRef<Path> + 'n>() -> impl Node<P, Output = Result<Vec<u8>, Error>> {
let fs = ValueNode(StdFs).clone();
let fs = ConsNode(fs);
let file: ComposeNode<_, _, P> = FileNode(PhantomData).after(fs);
let file: ComposeNode<_, _, P> = fs.then(FileNode(PhantomData));
FlatMapResultNode::new(BufferNode).after(file)
file.then(FlatMapResultNode::new(BufferNode))
}
pub fn image_node<'n, P: AsRef<Path> + 'n>() -> impl Node<P, Output = Result<Image, Error>> {
let file = file_node();
let image_loader = FnNode::new(|data: Vec<u8>| image::load_from_memory(&data).map_err(Error::Image).map(|image| image.into_rgba32f()));
let image: ComposeNode<_, _, P> = FlatMapResultNode::new(image_loader).after(file);
let image: ComposeNode<_, _, P> = file.then(FlatMapResultNode::new(image_loader));
let convert_image = FnNode::new(|image: image::ImageBuffer<_, _>| {
let data = image
.enumerate_pixels()
@ -136,7 +136,7 @@ pub fn image_node<'n, P: AsRef<Path> + 'n>() -> impl Node<P, Output = Result<Ima
}
});
MapResultNode::new(convert_image).after(image)
image.then(MapResultNode::new(convert_image))
}
pub fn export_image_node<'n>() -> impl Node<(Image, &'n str), Output = Result<(), Error>> {
@ -172,7 +172,7 @@ mod test {
let image = image_node::<&str>();
let gray = MapImageNode::new(GrayscaleNode);
let grayscale_picture = MapResultNode::new(&gray).after(image);
let grayscale_picture = image.then(MapResultNode::new(&gray));
let export = export_image_node();
let picture = grayscale_picture.eval("test-image-1.png").expect("Failed to load image");