From fa5345d33f1294aec94242c3bddc9561c665e130 Mon Sep 17 00:00:00 2001 From: Dennis Date: Sun, 5 Jun 2022 01:15:22 +0200 Subject: [PATCH] Remove input from the node definition --- node-graph/gcore/src/generic.rs | 54 +++++++++++----------- node-graph/gcore/src/lib.rs | 16 ++----- node-graph/gcore/src/ops.rs | 80 +++++++++++++++++++++++---------- node-graph/gcore/src/value.rs | 33 +++++--------- 4 files changed, 99 insertions(+), 84 deletions(-) diff --git a/node-graph/gcore/src/generic.rs b/node-graph/gcore/src/generic.rs index 39822d53..87e88069 100644 --- a/node-graph/gcore/src/generic.rs +++ b/node-graph/gcore/src/generic.rs @@ -1,39 +1,43 @@ use core::marker::PhantomData; use crate::Node; -pub struct FnNode O, In, O>(T, PhantomData, PhantomData); -impl<'n, T: Fn(In) -> O, In, O: 'n> Node<'n, In> for FnNode { - type Output = O; - - fn eval(&'n self, input: In) -> Self::Output { - self.0(input) - } -} - -impl O, In, O> FnNode { - pub fn new(f: T) -> Self { - FnNode(f, PhantomData::default(), PhantomData::default()) - } -} - -pub struct FnNodeWithState O, In, O, State>( +pub struct FnNode<'n, T: Fn(::Output) -> O, N: Node<'n>, O: 'n>( T, - State, - PhantomData, + &'n N, PhantomData, ); -impl<'n, T: Fn(In, &State) -> O, In, O: 'n, State> Node<'n, In> - for FnNodeWithState +impl<'n, T: Fn(::Output) -> O, N: Node<'n>, O> Node<'n> for FnNode<'n, T, N, O> { + type Output = O; + + fn eval(&'n self) -> Self::Output { + self.0(self.1.eval()) + } +} + +impl<'n, T: Fn(::Output) -> O, N: Node<'n>, O> FnNode<'n, T, N, O> { + pub fn new(f: T, input: &'n N) -> Self { + FnNode(f, input, PhantomData) + } +} + +pub struct FnNodeWithState<'n, T: Fn(N::Output, &State) -> O, N: Node<'n>, O, State>( + T, + &'n N, + State, + PhantomData, +); +impl<'n, T: Fn(N::Output, &State) -> O, N: Node<'n>, O: 'n, State> Node<'n> + for FnNodeWithState<'n, T, N, O, State> { type Output = O; - fn eval(&'n self, input: In) -> Self::Output { - self.0(input, &self.1) + fn eval(&'n self) -> Self::Output { + self.0(self.1.eval(), &self.2) } } -impl O, In, O, State> FnNodeWithState { - pub fn new(f: T, state: State) -> Self { - FnNodeWithState(f, state, PhantomData::default(), PhantomData::default()) +impl<'n, T: Fn(N::Output, &State) -> O, N: Node<'n>, O, State> FnNodeWithState<'n, T, N, O, State> { + pub fn new(f: T, input: &'n N, state: State) -> Self { + FnNodeWithState(f, input, state, PhantomData) } } diff --git a/node-graph/gcore/src/lib.rs b/node-graph/gcore/src/lib.rs index 3e43e6eb..7d0f9073 100644 --- a/node-graph/gcore/src/lib.rs +++ b/node-graph/gcore/src/lib.rs @@ -3,24 +3,16 @@ pub mod generic; pub mod ops; -pub mod structural; +//pub mod structural; pub mod value; #[rustfmt::skip] -pub trait Node< 'n, Input> { - type Output : 'n; +pub trait Node<'n> { + type Output: 'n; // TODO: replace with generic associated type - fn eval(&'n self, input: Input) -> Self::Output; + fn eval(&'n self) -> Self::Output; } -// TODO: Fix exec trait -pub trait Exec<'n>: Node<'n, ()> { - fn exec(&'n self) -> Self::Output { - self.eval(()) - } -} -impl<'n, T: Node<'n, ()>> Exec<'n> for T {} - pub trait Cache { fn clear(&mut self); } diff --git a/node-graph/gcore/src/ops.rs b/node-graph/gcore/src/ops.rs index c6444ff6..7f3196d7 100644 --- a/node-graph/gcore/src/ops.rs +++ b/node-graph/gcore/src/ops.rs @@ -1,52 +1,84 @@ -use core::{marker::PhantomData, ops::Add}; +use core::ops::Add; use crate::Node; #[repr(C)] -#[derive(Default)] -pub struct AddNode(PhantomData); -impl<'n, T: Add + Copy + 'n> Node<'n, (T, T)> for AddNode { +struct AddNode<'n, T: Add, I1: Node<'n, Output = T>, I2: Node<'n, Output = T>>( + pub &'n I1, + pub &'n I2, +); +impl<'n, T: Add + 'n, I1: Node<'n, Output = T>, I2: Node<'n, Output = T>> Node<'n> + for AddNode<'n, T, I1, I2> +{ type Output = ::Output; - fn eval(&'n self, input: (T, T)) -> T::Output { - let (a, b) = input; - a + b + fn eval(&self) -> T::Output { + self.0.eval() + self.1.eval() } } #[repr(C)] -#[derive(Default)] -/// Destructures a Tuple of two values and returns the first one -pub struct FstNode(PhantomData, PhantomData); -impl<'n, T: Copy + 'n, U> Node<'n, (T, U)> for FstNode { +pub struct CloneNode<'n, N: Node<'n, Output = &'n O>, O: Clone + 'n>(pub &'n N); +impl<'n, N: Node<'n, Output = &'n O>, O: Clone> Node<'n> for CloneNode<'n, N, O> { + type Output = O; + fn eval(&self) -> Self::Output { + self.0.eval().clone() + } +} + +#[repr(C)] +pub struct FstNode<'n, N: Node<'n>>(pub &'n N); +impl<'n, T: 'n, U, N: Node<'n, Output = (T, U)>> Node<'n> for FstNode<'n, N> { type Output = T; - fn eval(&'n self, input: (T, U)) -> Self::Output { - let (a, _) = input; + fn eval(&self) -> Self::Output { + let (a, _) = self.0.eval(); a } } #[repr(C)] -#[derive(Default)] /// Destructures a Tuple of two values and returns the first one -pub struct SndNode(PhantomData, PhantomData); -impl<'n, T, U: Copy + 'n> Node<'n, (T, U)> for SndNode { +pub struct SndNode<'n, N: Node<'n>>(pub &'n N); +impl<'n, T, U: 'n, N: Node<'n, Output = (T, U)>> Node<'n> for SndNode<'n, N> { type Output = U; - fn eval(&'n self, input: (T, U)) -> Self::Output { - let (_, b) = input; + fn eval(&self) -> Self::Output { + let (_, b) = self.0.eval(); b } } + #[repr(C)] -#[derive(Default)] /// Destructures a Tuple of two values and returns the first one -pub struct DupNode(PhantomData); -impl<'n, T: Copy + 'n> Node<'n, T> for DupNode { - type Output = (T, T); - fn eval(&'n self, input: T) -> Self::Output { - (input, input) +pub struct SndRefNode<'n, N: Node<'n>>(pub &'n N); +impl<'n, T: 'n, U: 'n, N: Node<'n, Output = &'n (T, U)>> Node<'n> for SndRefNode<'n, N> { + type Output = &'n U; + fn eval(&self) -> Self::Output { + let (_, ref b) = self.0.eval(); + b } } +#[repr(C)] +/// Destructures a Tuple of two values and returns the first one +pub struct DupNode<'n, N: Node<'n>>(&'n N); +impl<'n, N: Node<'n>> Node<'n> for DupNode<'n, N> { + type Output = (N::Output, N::Output); + fn eval(&self) -> Self::Output { + (self.0.eval(), self.0.eval()) //TODO: use Copy/Clone implementation + } +} + +pub fn foo() { + let value = crate::value::ValueNode::new(2u32); + let dup = DupNode(&value); + fn swap<'n>(input: (&'n u32, &'n u32)) -> (&'n u32, &'n u32) { + (input.1, input.0) + } + //let fnn = crate::generic::FnNode::new(swap, &dup); TODO fix types + let snd = SndNode(&dup); + let add = AddNode(&snd, &value); + let _ = add.eval(); +} + #[cfg(target_arch = "spirv")] pub mod gpu { //#![deny(warnings)] diff --git a/node-graph/gcore/src/value.rs b/node-graph/gcore/src/value.rs index 11bb761d..b38c4c86 100644 --- a/node-graph/gcore/src/value.rs +++ b/node-graph/gcore/src/value.rs @@ -3,33 +3,33 @@ use core::marker::PhantomData; use crate::Node; pub struct IntNode; -impl<'n, const N: u32> Node<'n, ()> for IntNode { +impl<'n, const N: u32> Node<'n> for IntNode { type Output = u32; - fn eval(&self, _input: ()) -> u32 { + fn eval(&self) -> u32 { N } } #[derive(Default)] -pub struct ValueNode<'n, T>(T, PhantomData<&'n ()>); -impl<'n, T: 'n> Node<'n, ()> for ValueNode<'n, T> { +pub struct ValueNode(pub T); +impl<'n, T: 'n> Node<'n> for ValueNode { type Output = &'n T; - fn eval(&self, _input: ()) -> &T { + fn eval(&'n self) -> Self::Output { &self.0 } } -impl<'n, T> ValueNode<'n, T> { - pub const fn new(value: T) -> ValueNode<'n, T> { - ValueNode(value, PhantomData) +impl<'n, T> ValueNode { + pub const fn new(value: T) -> ValueNode { + ValueNode(value) } } #[derive(Default)] pub struct DefaultNode(PhantomData); -impl<'n, T: Default + 'n> Node<'n, ()> for DefaultNode { +impl<'n, T: Default + 'n> Node<'n> for DefaultNode { type Output = T; - fn eval(&self, _input: ()) -> T { + fn eval(&self) -> T { T::default() } } @@ -38,16 +38,3 @@ impl DefaultNode { DefaultNode(PhantomData) } } - -pub struct DefaultRefNode<'n, T>(ValueNode<'n, T>); -impl<'n, T: 'n> Node<'n, ()> for DefaultRefNode<'n, T> { - type Output = &'n T; - fn eval(&'n self, _input: ()) -> &'n T { - self.0.eval(()) - } -} -impl<'n, T: Default> Default for DefaultRefNode<'n, T> { - fn default() -> DefaultRefNode<'n, T> { - DefaultRefNode(ValueNode::new(T::default())) - } -}