Rework DynAnyNode design to work with the borrow stack (#796)

This commit is contained in:
TrueDoctor
2022-10-15 03:02:58 +02:00
committed by GitHub
parent 99d92ef887
commit 06acd45a81
8 changed files with 198 additions and 80 deletions

View File

@@ -1,39 +1,53 @@
#[cfg(test)]
mod tests {
use graphene_core::structural::*;
use std::marker::PhantomData;
use graphene_core::value::ValueNode;
use graphene_core::{structural::*, RefNode};
use borrow_stack::BorrowStack;
use graphene_std::any::{Any, DynAnyNode, DynAnyNodeTrait};
use dyn_any::{downcast, DynAny, IntoDynAny};
use graphene_std::any::{Any, DowncastNode, DynAnyNode, TypeErasedNode};
use graphene_std::ops::AddNode;
#[test]
fn borrow_stack() {
let stack = borrow_stack::FixedSizeStack::new(256);
unsafe {
let dynanynode: DynAnyNode<'_, _, ()> = DynAnyNode::new(&ValueNode(2_u32));
let refn = Box::new(dynanynode) as Box<dyn DynAnyNodeTrait>;
stack.push(refn);
}
unsafe {
let dynanynode: DynAnyNode<'_, _, &u32> = DynAnyNode::new(&ConsNode(ValueNode(2_u32)));
let refn = Box::new(dynanynode) as Box<dyn DynAnyNodeTrait>;
stack.push(refn);
}
unsafe {
let dynanynode: DynAnyNode<'_, _, (&u32, u32)> = DynAnyNode::new(&AddNode);
let refn = Box::new(dynanynode) as Box<dyn DynAnyNodeTrait>;
stack.push(refn);
let dynanynode: DynAnyNode<_, (), _, _> = DynAnyNode::new(ValueNode(2_u32));
stack.push(dynanynode.into_box());
}
stack.push_fn(|nodes| {
let downcast: DowncastNode<_, &u32> = DowncastNode::new(&nodes[0]);
let dynanynode: DynAnyNode<ConsNode<_, Any<'_>>, u32, _, _> = DynAnyNode::new(ConsNode(downcast, PhantomData));
dynanynode.into_box()
});
stack.push_fn(|_| {
let dynanynode: DynAnyNode<_, (u32, &u32), _, _> = DynAnyNode::new(AddNode);
dynanynode.into_box()
});
stack.push_fn(|nodes| {
let compose_node = nodes[1].after(&nodes[2]);
TypeErasedNode(Box::new(compose_node))
});
let mut input = Box::new(()) as Any;
let result = unsafe { &stack.get()[1] }.eval_ref(4_u32.into_dyn());
assert_eq!(*downcast::<(u32, &u32)>(result).unwrap(), (4_u32, &2_u32));
let result = unsafe { &stack.get()[1] }.eval_ref(4_u32.into_dyn());
let add = unsafe { &stack.get()[2] }.eval_ref(result);
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_dispatch(input);
}
input = value.eval_ref(input);
}*/
assert_eq!(*dyn_any::downcast::<u32>(input).unwrap(), 4)
//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());
}