Implement node registry (#822)

This commit is contained in:
TrueDoctor
2022-10-26 00:32:50 +02:00
committed by GitHub
parent 3fe552ff20
commit 097899611f
10 changed files with 213 additions and 45 deletions

View File

@@ -1,3 +1,5 @@
pub mod node_registry;
#[cfg(test)]
mod tests {
@@ -7,7 +9,7 @@ mod tests {
use graphene_core::{structural::*, RefNode};
use borrow_stack::BorrowStack;
use dyn_any::{downcast, DynAny, IntoDynAny};
use dyn_any::{downcast, IntoDynAny};
use graphene_std::any::{Any, DowncastNode, DynAnyNode, TypeErasedNode};
use graphene_std::ops::AddNode;
@@ -19,7 +21,8 @@ mod tests {
stack.push(dynanynode.into_box());
}
stack.push_fn(|nodes| {
let downcast: DowncastNode<_, &u32> = DowncastNode::new(&nodes[0]);
let pre_node = nodes.get(0).unwrap();
let downcast: DowncastNode<&TypeErasedNode, &u32> = DowncastNode::new(pre_node);
let dynanynode: DynAnyNode<ConsNode<_, Any<'_>>, u32, _, _> = DynAnyNode::new(ConsNode(downcast, PhantomData));
dynanynode.into_box()
});
@@ -39,17 +42,6 @@ mod tests {
assert_eq!(*downcast::<u32>(add).unwrap(), 6_u32);
let add = unsafe { &stack.get()[3] }.eval_ref(4_u32.into_dyn());
assert_eq!(*downcast::<u32>(add).unwrap(), 6_u32);
/*
for i in 0..3 {
println!("node_id: {}", i);
let value = unsafe { &stack.get()[i] };
input = value.eval_ref(input);
}*/
//assert_eq!(*dyn_any::downcast::<u32>(result).unwrap(), 4)
//assert_eq!(4, *dyn_any::downcast::<u32>(DynamicAddNode.eval((Box::new(2_u32) as Dynamic, Box::new(2_u32) as Dynamic))).unwrap());
}
#[test]

View File

@@ -0,0 +1,120 @@
use std::marker::PhantomData;
use borrow_stack::FixedSizeStack;
use graphene_core::ops::{AddNode, IdNode};
use graphene_core::structural::{ConsNode, Then};
use graphene_core::{AsRefNode, Node};
use graphene_std::{
any::{Any, DowncastNode, DynAnyNode, IntoTypeErasedNode, TypeErasedNode},
document::{ConstructionArgs, ProtoNode, ProtoNodeInput},
};
struct NodeIdentifier {
name: &'static str,
types: &'static [&'static str],
}
const fn annotate<'n, 's: 'n, F>(f: F) -> F
where
F: Fn(ProtoNode, FixedSizeStack<TypeErasedNode<'n>>),
{
f
}
use borrow_stack::BorrowStack;
unsafe fn foo<'n>(proto_node: ProtoNode, stack: &'n FixedSizeStack<TypeErasedNode<'n>>) {
let node_id = proto_node.input.unwrap_node() as usize;
let nodes = stack.get();
let pre_node = nodes.get(node_id).unwrap();
let downcast: DowncastNode<_, &u32> = DowncastNode::new(pre_node);
let dynanynode: DynAnyNode<ConsNode<_, Any<'_>>, u32, _, _> = DynAnyNode::new(ConsNode(downcast, PhantomData));
stack.push(dynanynode.into_box());
}
fn borrow_stack() {
let stack = borrow_stack::FixedSizeStack::new(256);
unsafe {
{
let proto_node = ProtoNode::id();
foo(proto_node, &stack);
let proto_node = ProtoNode::id();
let stack = &stack;
let node_id = proto_node.input.unwrap_node() as usize;
let nodes = stack.get();
let pre_node = nodes.get(node_id).unwrap();
let downcast: DowncastNode<&TypeErasedNode, &u32> = DowncastNode::new(pre_node);
let dynanynode: DynAnyNode<ConsNode<_, Any<'_>>, u32, _, _> = DynAnyNode::new(ConsNode(downcast, PhantomData));
stack.push(dynanynode.into_box());
}
};
}
static NODE_REGISTRY: &[(NodeIdentifier, fn(ProtoNode, &FixedSizeStack<TypeErasedNode<'static>>))] = &[
(
NodeIdentifier {
name: "graphene_core::ops::IdNode",
types: &["Any<'n>"],
},
|proto_node, stack| {
stack.push_fn(|nodes| {
let pre_node = nodes.get(proto_node.input.unwrap_node() as usize).unwrap();
let node = pre_node.then(graphene_core::ops::IdNode);
node.into_type_erased()
})
},
),
(
NodeIdentifier {
name: "graphene_core::ops::AddNode",
types: &["u32", "u32"],
},
|proto_node, stack| {
stack.push_fn(|nodes| {
let pre_node = nodes.get(proto_node.input.unwrap_node() as usize).unwrap();
let node: DynAnyNode<AddNode, (u32, u32), _, _> = DynAnyNode::new(graphene_core::ops::AddNode);
let node = (pre_node).then(node);
node.into_type_erased()
})
},
),
/*(
NodeIdentifier {
name: "graphene_core::structural::ConsNode",
types: &["&TypeErasedNode", "&u32", "u32"],
},
|proto_node, stack| {
let node_id = proto_node.input.unwrap_node() as usize;
stack.push_fn(move |nodes| {
let pre_node = nodes.get(node_id).unwrap();
let downcast: DowncastNode<_, &u32> = DowncastNode::new(pre_node);
let dynanynode: DynAnyNode<ConsNode<_, Any<'_>>, u32, _, _> = DynAnyNode::new(ConsNode(downcast, PhantomData));
dynanynode.into_box()
})
},
),*/
(
NodeIdentifier {
name: "graphene_core::any::DowncastNode",
types: &["&TypeErasedNode", "&u32"],
},
|proto_node, stack| {
stack.push_fn(|nodes| {
let pre_node = nodes.get(proto_node.input.unwrap_node() as usize).unwrap();
let node = pre_node.then(graphene_core::ops::IdNode);
node.into_type_erased()
})
},
),
];
#[cfg(test)]
mod test {
use super::*;
/*#[test]
fn test() {
let nodes = [TypeErasedNode(Box::new(42u32))];
let node = NODE_REGISTRY[0].1(node, &nodes);
assert_eq!(node.eval(()), 42);
}*/
}