diff --git a/node-graph/src/main.rs b/node-graph/src/main.rs index 444768ae..7421f591 100644 --- a/node-graph/src/main.rs +++ b/node-graph/src/main.rs @@ -39,25 +39,32 @@ impl AnyRef for T { } } -trait DefaultNode: Default { +pub trait DefaultNode: Default { fn default_node() -> ValueNode { ValueNode::new(Self::default()) } } impl DefaultNode for T {} -trait After: Sized { +pub trait After: Sized { fn after<'a, First: Node>(&'a self, first: &'a First) -> ComposeNode<'a, First, Self> { ComposeNode::new(first, self) } } impl After for Second {} +pub trait DynamicInput { + fn set_kwarg_by_name(&mut self, name: &str, value: &dyn Any); + fn set_arg_by_index(&mut self, index: usize, value: &dyn Any); +} + fn main() { let int = IntNode::<32>; let add: u32 = AddNode::::default().eval((int.eval(&()), int.eval(&()))); let fnode = FnNode::new(|(a, b): &(i32, i32)| a - b); - let sub = fnode.any(&("a", 2)); + //let sub = fnode.any(&("a", 2)); + let cache = CacheNode::new(&fnode); + let foo = cache.eval(&(2, 3)); /* let curry: CurryNthArgNode<'_, _, _, u32, u32, 0> = CurryNthArgNode::new(&AddNode, &int); @@ -65,5 +72,5 @@ fn main() { let n = ValueNode::new(10_u32); let curry: CurryNthArgNode<'_, _, _, u32, _, 0> = CurryNthArgNode::new(&composition, &n); */ - println!("{}", sub) + println!("{}", foo) } diff --git a/node-graph/src/nodes.rs b/node-graph/src/nodes.rs index 5cb9a65f..d544e112 100644 --- a/node-graph/src/nodes.rs +++ b/node-graph/src/nodes.rs @@ -9,7 +9,7 @@ use std::{ marker::PhantomData, }; -use crate::{insert_after_nth, After, Node}; +use crate::{insert_after_nth, After, DynamicInput, Node}; use once_cell::sync::OnceCell; use parking_lot::RawRwLock; use storage_map::{StorageMap, StorageMapGuard}; @@ -49,6 +49,30 @@ impl Node for AddNode { } } +#[derive(Default)] +/// Destructures a Tuple of two values and returns the first one +pub struct FstNode(PhantomData, PhantomData); +impl Node for FstNode { + type Output<'a> = &'a T where Self: 'a; + type Input<'a> = &'a (T, U) where Self: 'a; + fn eval<'a, I: Borrow>>(&'a self, input: I) -> Self::Output<'a> { + let &(ref a, _) = input.borrow(); + a + } +} + +#[derive(Default)] +/// Destructures a Tuple of two values and returns the first one +pub struct SndNode(PhantomData, PhantomData); +impl Node for SndNode { + type Output<'a> = &'a U where Self: 'a; + type Input<'a> = &'a (T, U) where Self: 'a; + fn eval<'a, I: Borrow>>(&'a self, input: I) -> Self::Output<'a> { + let &(_, ref b) = input.borrow(); + b + } +} + pub struct ComposeNode<'n, FIRST, SECOND> { first: &'n FIRST, second: &'n SECOND, @@ -143,45 +167,25 @@ impl<'n, 'c, CachedNode: Node> CacheNode<'n, 'c, CachedNode> { } } -/* -/// Caches the output of a given Node and acts as a proxy -/// Automatically resets if it receives different input -struct SmartCacheNode<'n, 'c, NODE: Node + 'c> -where - for<'a> NODE::Input<'a>: Hash, -{ - cache: InnerSmartCacheNode<'n, 'c, NODE>, -} -impl<'n: 'c, 'c, NODE: Node> Node for SmartCacheNode<'n, 'c, NODE> -where - for<'a> NODE::Input<'a>: Hash, -{ - type Input<'a> = NODE::Input<'a> where Self: 'a, 'c : 'a; - type Output<'a> = &'a NODE::Output<'a> where Self: 'a, 'c: 'a; - fn eval<'a, I: Borrow>>(&'a self, input: I) -> Self::Output<'a> { - let mut hasher = DefaultHasher::new(); - input.borrow().hash(&mut hasher); - let hash = hasher.finish(); +pub struct ProxyNode(T); +impl Node for ProxyNode { + type Output<'a> = T where Self: 'a; - let node = self.cache.eval(input); - node.eval(input); + type Input<'a> = &'a () where Self: 'a; + + fn eval<'a, I: Borrow>>(&'a self, input: I) -> Self::Output<'a> { todo!() } } +impl DynamicInput for ProxyNode { + fn set_kwarg_by_name(&mut self, name: &str, value: &dyn Any) { + self.0.set_kwarg_by_name(name, value) + } -impl<'n, 'c, NODE: Node> SmartCacheNode<'n, 'c, NODE> -where - for<'a> NODE::Input<'a>: Hash, -{ - pub fn clear(&'n mut self) { - self.cache.clear(); + fn set_arg_by_index(&mut self, index: usize, value: &dyn Any) { + self.0.set_arg_by_index(index, value) } - pub fn new(node: &'n NODE) -> SmartCacheNode<'n, 'c, NODE> { - SmartCacheNode { - cache: InnerSmartCacheNode::new(node), - } - } -}*/ +} /// Caches the output of a given Node and acts as a proxy /// Automatically resets if it receives different input