diff --git a/node-graph/src/main.rs b/node-graph/src/main.rs index 429f4899..70f0e2f0 100644 --- a/node-graph/src/main.rs +++ b/node-graph/src/main.rs @@ -1,29 +1,54 @@ -#![deny(rust_2018_idioms)] -use std::any::Any; +#![feature(generic_associated_types)] +//#![deny(rust_2018_idioms)] +use std::{any::Any, borrow::Borrow}; mod iter; -mod nodes; +pub mod nodes; use iter::insert_after_nth; use nodes::*; -pub trait Node<'n, OUT> { - fn eval(&'n self, input: impl Iterator + Clone) -> OUT; - // fn source code - // positon +pub trait Node { + type Out<'a> + where + Self: 'a; + type Input<'a> + where + Self: 'a; + fn eval<'a, T: Borrow>>(&'a self, input: T) -> Self::Out<'a>; } -trait After<'n, OUT, SECOND: Node<'n, OUT>> { - fn after>( - &'n self, - first: &'n FIRST, - ) -> ComposeNode<'n, FIRST, SECOND, INTERMEDIATE>; +pub trait AnyRef: Node { + fn any<'a>(&'a self, input: &'a dyn Any) -> Self::Out<'a> + where + Self::Input<'a>: 'static + Copy; +} +impl AnyRef for T { + fn any<'a>(&'a self, input: &'a dyn Any) -> Self::Out<'a> + where + Self::Input<'a>: 'static + Copy, + { + self.eval::<&Self::Input<'a>>(input.downcast_ref::>().unwrap()) + } } +/* +trait After { + type Out<'a> + where + Self: 'a; + fn after<'a>( + &'a self, + first: &'a FIRST, + ) -> ComposeNode<'a, FIRST, SECOND, INTERMEDIATE>; +}*/ + fn main() { - use std::iter; let int = IntNode::<32>; + let add: u32 = AddNode::::default().any(&(int.eval(&()), int.eval(&())) as &dyn Any); + /* let curry: CurryNthArgNode<'_, _, _, u32, u32, 0> = CurryNthArgNode::new(&AddNode, &int); let composition = curry.after(&curry); let n = ValueNode::new(10_u32); let curry: CurryNthArgNode<'_, _, _, u32, _, 0> = CurryNthArgNode::new(&composition, &n); - println!("{}", curry.eval(iter::empty())) + */ + println!("{}", add) } diff --git a/node-graph/src/nodes.rs b/node-graph/src/nodes.rs index a700342e..90ef5649 100644 --- a/node-graph/src/nodes.rs +++ b/node-graph/src/nodes.rs @@ -1,30 +1,29 @@ use std::{ - any::Any, collections::hash_map::DefaultHasher, hash::Hasher, iter, iter::Sum, + any::Any, borrow::Borrow, collections::hash_map::DefaultHasher, hash::Hasher, iter, iter::Sum, marker::PhantomData, }; -use crate::{insert_after_nth, After, Node}; +use crate::{insert_after_nth, /*After,*/ Node}; use once_cell::sync::OnceCell; pub struct IntNode; -impl<'n, const N: u32> Node<'n, u32> for IntNode { - fn eval(&'n self, _input: impl Iterator) -> u32 { +impl Node for IntNode { + type Out<'a> = u32; + type Input<'a> = (); + fn eval<'a, I: Borrow>>(&self, _input: I) -> u32 { N } } #[derive(Default)] pub struct ValueNode(T); -impl<'n, T> Node<'n, &'n T> for ValueNode { - fn eval(&'n self, _input: impl Iterator) -> &T { +impl Node for ValueNode { + type Out<'a> = &'a T where T: 'a; + type Input<'a> = () where T: 'a; + fn eval<'n, I: Borrow>>(&'n self, _input: I) -> &T { &self.0 } } -impl<'n, T: Copy> Node<'n, T> for ValueNode { - fn eval(&'n self, _input: impl Iterator) -> T { - self.0 - } -} impl ValueNode { pub fn new(value: T) -> ValueNode { @@ -32,29 +31,34 @@ impl ValueNode { } } -pub struct AddNode; -impl<'n, T: Sum + 'static + Copy> Node<'n, T> for AddNode { - fn eval(&'n self, input: impl Iterator) -> T { - input.map(|x| *(x.downcast_ref::().unwrap())).sum::() +#[derive(Default)] +pub struct AddNode(PhantomData); +impl Node for AddNode { + type Out<'a> = T::Output; + type Input<'a> = (T, T); + fn eval<'a, I: Borrow>>(&'a self, input: I) -> T::Output { + input.borrow().0 + input.borrow().1 } } /// Caches the output of a given Node and acts as a proxy -pub struct CacheNode<'n, NODE: Node<'n, OUT>, OUT: Clone> { - node: &'n NODE, - cache: OnceCell, +pub struct CacheNode<'n, 'c, CachedNode: Node + 'c> { + node: &'n CachedNode, + cache: OnceCell>, } -impl<'n, NODE: Node<'n, OUT>, OUT: Clone> Node<'n, &'n OUT> for CacheNode<'n, NODE, OUT> { - fn eval(&'n self, input: impl Iterator + Clone) -> &'n OUT { +impl<'n: 'c, 'c, CashedNode: Node> Node for CacheNode<'n, 'c, CashedNode> { + type Out<'a> = &'a CashedNode::Out<'c> where 'c: 'a; + type Input<'a> = CashedNode::Input<'c> where 'c: 'a; + fn eval<'a, I: Borrow>>(&'a self, input: I) -> Self::Out<'a> { self.cache.get_or_init(|| self.node.eval(input)) } } -impl<'n, NODE: Node<'n, OUT>, OUT: Clone> CacheNode<'n, NODE, OUT> { - fn clear(&'n mut self) { +impl<'n, 'c, NODE: Node> CacheNode<'n, 'c, NODE> { + pub fn clear(&'n mut self) { self.cache = OnceCell::new(); } - fn new(node: &'n NODE) -> CacheNode<'n, NODE, OUT> { + pub fn new(node: &'n NODE) -> CacheNode<'n, 'c, NODE> { CacheNode { node, cache: OnceCell::new(), @@ -110,6 +114,8 @@ impl<'n, NODE: Node<'n, OUT>, OUT: Clone> SmartCacheNode<'n, NODE, OUT> { } }*/ +/* + pub struct CurryNthArgNode< 'n, CurryNode: Node<'n, OUT>, @@ -151,27 +157,38 @@ impl<'n, CurryNode: Node<'n, Out>, ArgNode: Node<'n, Arg>, Arg: Clone, Out, cons } } } - -pub struct ComposeNode<'n, FIRST, SECOND, INTERMEDIATE> +*/ +/* +pub struct ComposeNode<'n, FIRST, SECOND> where - FIRST: Node<'n, INTERMEDIATE>, + FIRST: Node, { first: &'n FIRST, second: &'n SECOND, _phantom_data: PhantomData, } -impl<'n, FIRST, SECOND, OUT: 'n, INTERMEDIATE: 'static + Clone> Node<'n, OUT> - for ComposeNode<'n, FIRST, SECOND, INTERMEDIATE> +impl<'n, FIRST, SECOND> Node for ComposeNode<'n, FIRST, SECOND> where - FIRST: Node<'n, INTERMEDIATE>, - SECOND: Node<'n, OUT>, + FIRST: Node, + SECOND: Node, { - fn eval(&'n self, input: impl Iterator + Clone) -> OUT { - let curry = CurryNthArgNode::<'_, _, _, _, _, 0>::new(self.second, self.first); - CurryNthArgNode::<'_, _, _, _, _, 0>::new(curry, ValueNode::new(input)).eval(input) + fn eval<'a, T: &Self::Input<'a>>(&'a self, input: T) -> &Self::Out<'a> { + self.second.eval(self.first.eval(input)) + //let curry = CurryNthArgNode::<'_, _, _, _, _, 0>::new(self.second, self.first); + //CurryNthArgNode::<'_, _, _, _, _, 0>::new(curry, ValueNode::new(input)).eval(input) } + + type Out<'a> = SECOND::Out<'a> + where + Self: 'a; + + type Input<'a> = FIRST::Input<'a> + where + Self: 'a; } +*/ +/* impl<'n, FIRST, SECOND, INTERMEDIATE: 'static> ComposeNode<'n, FIRST, SECOND, INTERMEDIATE> where @@ -198,3 +215,4 @@ impl<'n, OUT, SECOND: Node<'n, OUT>> After<'n, OUT, SECOND> for SECOND { } } } +*/