From f5e8c48dfbb5ce845c763ab1ee93d20cecf45a13 Mon Sep 17 00:00:00 2001 From: 0HyperCube <78500760+0HyperCube@users.noreply.github.com> Date: Sun, 28 Aug 2022 20:52:46 +0100 Subject: [PATCH] Use .then() instead of .after() for sequencing nodes (#760) * Add .then() for sequencing nodes * Remove all uses af after Co-authored-by: Keavon Chambers --- node-graph/gcore/src/ops.rs | 13 ++++---- node-graph/gcore/src/structural.rs | 50 ++++++++++++++++-------------- node-graph/gstd/src/any.rs | 2 +- node-graph/gstd/src/raster.rs | 12 +++---- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/node-graph/gcore/src/ops.rs b/node-graph/gcore/src/ops.rs index 3aca9eba..c5d58825 100644 --- a/node-graph/gcore/src/ops.rs +++ b/node-graph/gcore/src/ops.rs @@ -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); } diff --git a/node-graph/gcore/src/structural.rs b/node-graph/gcore/src/structural.rs index 4dcd7f65..226853a7 100644 --- a/node-graph/gcore/src/structural.rs +++ b/node-graph/gcore/src/structural.rs @@ -64,54 +64,56 @@ impl<'n, Input, First: 'n, Second: 'n> ComposeNode { ComposeNode:: { first, second, _phantom: PhantomData } } } -pub trait After: Sized { - fn after(self, first: First) -> ComposeNode + +pub trait Then: Sized { + fn then(self, second: Second) -> ComposeNode where - First: Node, - Self: Node, + Self: Node, + Second: Node, { - ComposeNode:: { - first, - second: self, + ComposeNode:: { + first: self, + second, _phantom: PhantomData, } } } -impl, I> After for Second {} -pub trait AfterRef: Sized { - fn after<'n, First: 'n, Input>(&'n self, first: First) -> ComposeNode +impl, Inter, Input> Then for First {} + +pub trait ThenRef: Sized { + fn after<'n, Second: 'n>(&'n self, second: Second) -> ComposeNode<&'n Self, Second, Input> where - First: Node + Copy, - &'n Self: Node, + &'n Self: Node + Copy, + Second: Node, Self: 'n, { - ComposeNode:: { - first, - second: self, + ComposeNode::<&'n Self, Second, Input> { + first: self, + second, _phantom: PhantomData, } } } -impl<'n, Second: 'n, I> AfterRef for Second where &'n Second: Node {} +impl<'n, First: 'n, Inter, Input> ThenRef for First where &'n First: Node {} #[cfg(feature = "async")] -pub trait AfterBox { - fn after<'n, First: 'n, Input>(self, first: First) -> ComposeNode +pub trait ThenBox { + fn then<'n, Second: 'n>(self, second: Second) -> ComposeNode where - First: Node + Copy, - alloc::boxed::Box: Node, + alloc::boxed::Box: Node, + Second: Node + Copy, Self: Sized, { - ComposeNode:: { - first, - second: self, + ComposeNode:: { + first: self, + second, _phantom: PhantomData, } } } #[cfg(feature = "async")] -impl<'n, Second: 'n, I> AfterBox for alloc::boxed::Box where &'n alloc::boxed::Box: Node {} +impl<'n, First: 'n, Inter, Input> ThenBox for alloc::boxed::Box where &'n alloc::boxed::Box: Node {} pub struct ConsNode(pub Root); diff --git a/node-graph/gstd/src/any.rs b/node-graph/gstd/src/any.rs index b3fdbaf6..1b7d907d 100644 --- a/node-graph/gstd/src/any.rs +++ b/node-graph/gstd/src/any.rs @@ -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] diff --git a/node-graph/gstd/src/raster.rs b/node-graph/gstd/src/raster.rs index ed4d6b31..2c684604 100644 --- a/node-graph/gstd/src/raster.rs +++ b/node-graph/gstd/src/raster.rs @@ -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 + 'n>() -> impl Node, 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 + 'n>() -> impl Node> { let file = file_node(); let image_loader = FnNode::new(|data: Vec| 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 + 'n>() -> impl Node() -> 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");