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:
parent
c28d618291
commit
f5e8c48dfb
|
|
@ -167,27 +167,27 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
pub fn dup_node() {
|
pub fn dup_node() {
|
||||||
let value = ValueNode(4u32);
|
let value = ValueNode(4u32);
|
||||||
let dup = DupNode.after(value);
|
let dup = value.then(DupNode);
|
||||||
assert_eq!(dup.eval(()), (4, 4));
|
assert_eq!(dup.eval(()), (4, 4));
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
pub fn id_node() {
|
pub fn id_node() {
|
||||||
let value = IdNode.after(ValueNode(4u32));
|
let value = ValueNode(4u32).then(IdNode);
|
||||||
assert_eq!(value.eval(()), 4);
|
assert_eq!(value.eval(()), 4);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
pub fn clone_node() {
|
pub fn clone_node() {
|
||||||
let cloned = CloneNode.after(&ValueNode(4u32));
|
let cloned = (&ValueNode(4u32)).then(CloneNode);
|
||||||
assert_eq!(cloned.eval(()), 4);
|
assert_eq!(cloned.eval(()), 4);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
pub fn fst_node() {
|
pub fn fst_node() {
|
||||||
let fst = FstNode.after(ValueNode((4u32, "a")));
|
let fst = ValueNode((4u32, "a")).then(FstNode);
|
||||||
assert_eq!(fst.eval(()), 4);
|
assert_eq!(fst.eval(()), 4);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
pub fn snd_node() {
|
pub fn snd_node() {
|
||||||
let fst = SndNode.after(ValueNode((4u32, "a")));
|
let fst = ValueNode((4u32, "a")).then(SndNode);
|
||||||
assert_eq!(fst.eval(()), "a");
|
assert_eq!(fst.eval(()), "a");
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -195,7 +195,8 @@ mod test {
|
||||||
let a = ValueNode(42u32);
|
let a = ValueNode(42u32);
|
||||||
let b = ValueNode(6u32);
|
let b = ValueNode(6u32);
|
||||||
let cons_a = ConsNode(a);
|
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);
|
assert_eq!(sum.eval(()), 48);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,54 +64,56 @@ impl<'n, Input, First: 'n, Second: 'n> ComposeNode<First, Second, Input> {
|
||||||
ComposeNode::<First, Second, Input> { first, second, _phantom: PhantomData }
|
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
|
where
|
||||||
First: Node<Input, Output = Inter>,
|
Self: Node<Input, Output = Inter>,
|
||||||
Self: Node<Inter>,
|
Second: Node<Inter>,
|
||||||
{
|
{
|
||||||
ComposeNode::<First, Self, Input> {
|
ComposeNode::<Self, Second, Input> {
|
||||||
first,
|
first: self,
|
||||||
second: self,
|
second,
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<Second: Node<I>, I> After<I> for Second {}
|
|
||||||
|
|
||||||
pub trait AfterRef<Inter>: Sized {
|
impl<First: Node<Input, Output = Inter>, Inter, Input> Then<Inter, Input> for First {}
|
||||||
fn after<'n, First: 'n, Input>(&'n self, first: First) -> ComposeNode<First, &'n Self, Input>
|
|
||||||
|
pub trait ThenRef<Inter, Input>: Sized {
|
||||||
|
fn after<'n, Second: 'n>(&'n self, second: Second) -> ComposeNode<&'n Self, Second, Input>
|
||||||
where
|
where
|
||||||
First: Node<Input, Output = Inter> + Copy,
|
&'n Self: Node<Input, Output = Inter> + Copy,
|
||||||
&'n Self: Node<Inter>,
|
Second: Node<Inter>,
|
||||||
Self: 'n,
|
Self: 'n,
|
||||||
{
|
{
|
||||||
ComposeNode::<First, &'n Self, Input> {
|
ComposeNode::<&'n Self, Second, Input> {
|
||||||
first,
|
first: self,
|
||||||
second: self,
|
second,
|
||||||
_phantom: PhantomData,
|
_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")]
|
#[cfg(feature = "async")]
|
||||||
pub trait AfterBox<Inter> {
|
pub trait ThenBox<Inter, Input> {
|
||||||
fn after<'n, First: 'n, Input>(self, first: First) -> ComposeNode<First, Self, Input>
|
fn then<'n, Second: 'n>(self, second: Second) -> ComposeNode<Self, Second, Input>
|
||||||
where
|
where
|
||||||
First: Node<Input, Output = Inter> + Copy,
|
alloc::boxed::Box<Self>: Node<Input, Output = Inter>,
|
||||||
alloc::boxed::Box<Self>: Node<Inter>,
|
Second: Node<Inter> + Copy,
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
ComposeNode::<First, Self, Input> {
|
ComposeNode::<Self, Second, Input> {
|
||||||
first,
|
first: self,
|
||||||
second: self,
|
second,
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature = "async")]
|
#[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);
|
pub struct ConsNode<Root>(pub Root);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,7 @@ mod test {
|
||||||
let value = value.as_owned();
|
let value = value.as_owned();
|
||||||
|
|
||||||
/*let computation = ComposeNode::new(value, add);
|
/*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();*/
|
let result: u32 = *dyn_any::downcast(computation.eval(&())).unwrap();*/
|
||||||
}*/
|
}*/
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use core::marker::PhantomData;
|
||||||
use graphene_core::ops::FlatMapResultNode;
|
use graphene_core::ops::FlatMapResultNode;
|
||||||
use graphene_core::raster::color::Color;
|
use graphene_core::raster::color::Color;
|
||||||
use graphene_core::structural::{ComposeNode, ConsNode};
|
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 image::Pixel;
|
||||||
use std::path::Path;
|
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>> {
|
pub fn file_node<'n, P: AsRef<Path> + 'n>() -> impl Node<P, Output = Result<Vec<u8>, Error>> {
|
||||||
let fs = ValueNode(StdFs).clone();
|
let fs = ValueNode(StdFs).clone();
|
||||||
let fs = ConsNode(fs);
|
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>> {
|
pub fn image_node<'n, P: AsRef<Path> + 'n>() -> impl Node<P, Output = Result<Image, Error>> {
|
||||||
let file = file_node();
|
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_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 convert_image = FnNode::new(|image: image::ImageBuffer<_, _>| {
|
||||||
let data = image
|
let data = image
|
||||||
.enumerate_pixels()
|
.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>> {
|
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 image = image_node::<&str>();
|
||||||
let gray = MapImageNode::new(GrayscaleNode);
|
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 export = export_image_node();
|
||||||
|
|
||||||
let picture = grayscale_picture.eval("test-image-1.png").expect("Failed to load image");
|
let picture = grayscale_picture.eval("test-image-1.png").expect("Failed to load image");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue