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,4 +1,4 @@
use dyn_any::{DynAny, StaticType};
use dyn_any::{DynAny, StaticType, StaticTypeSized};
pub use graphene_core::{generic, ops /*, structural*/, Node, RefNode};
use std::marker::PhantomData;
@@ -6,7 +6,7 @@ fn fmt_error<I>() -> String {
format!("DynAnyNode: input is not of correct type, expected {}", std::any::type_name::<I>())
}
pub struct DynAnyNode<N, I: StaticType, O: StaticType, ORef: StaticType>(pub N, PhantomData<(I, O, ORef)>);
pub struct DynAnyNode<N, I: StaticType, O: StaticType, ORef: StaticType>(pub N, pub PhantomData<(I, O, ORef)>);
/*impl<'n, I: StaticType, N: RefNode<'n, &'n I, Output = O> + 'n, O: 'n + StaticType> Node<&'n dyn DynAny<'n>> for DynAnyNode<'n, N, I> {
type Output = Box<dyn dyn_any::DynAny<'n> + 'n>;
fn eval(self, input: &'n dyn DynAny<'n>) -> Self::Output {
@@ -49,13 +49,37 @@ where
Box::new((&self.0).eval_ref(*input))
}
}
pub struct TypeErasedNode<'n>(pub Box<dyn AsBoxNode<'n, Any<'n>, Output = Any<'n>>>);
pub struct TypeErasedNode<'n>(pub Box<dyn AsRefNode<'n, Any<'n>, Output = Any<'n>> + 'n>);
impl<'n> Node<Any<'n>> for &'n TypeErasedNode<'n> {
type Output = Any<'n>;
fn eval(self, input: Any<'n>) -> Self::Output {
self.0.eval_box(input)
}
}
impl<'n> Node<Any<'n>> for &'n &'n TypeErasedNode<'n> {
type Output = Any<'n>;
fn eval(self, input: Any<'n>) -> Self::Output {
self.0.eval_box(input)
}
}
pub trait IntoTypeErasedNode<'n> {
fn into_type_erased(self) -> TypeErasedNode<'n>;
}
impl<'n> StaticTypeSized for TypeErasedNode<'n> {
type Static = TypeErasedNode<'static>;
}
impl<'n, N: 'n> IntoTypeErasedNode<'n> for N
where
N: AsRefNode<'n, Any<'n>, Output = Any<'n>>,
&'n N: Node<Any<'n>, Output = Any<'n>>,
{
fn into_type_erased(self) -> TypeErasedNode<'n> {
TypeErasedNode(Box::new(self))
}
}
impl<'n, I: StaticType + 'n, N: 'n, O: 'n + StaticType, ORef: 'n + StaticType> DynAnyNode<N, I, O, ORef>
where
@@ -81,11 +105,19 @@ where
Self: 'a,
N: Node<I, Output = O>,
{
TypeErasedNode(Box::new(self))
self.into_type_erased()
}
}
impl<'n, I: StaticType + 'n, N: 'n, O: 'n + StaticType, ORef: 'n + StaticType> DynAnyNode<&'n N, I, O, ORef>
where
N: Node<I, Output = ORef>,
{
pub fn new_from_ref(n: &'n N) -> Self {
DynAnyNode(n, PhantomData)
}
}
pub struct DowncastNode<N, I: StaticType>(pub N, PhantomData<I>);
pub struct DowncastNode<N, I: StaticType>(pub N, pub PhantomData<I>);
impl<N: Copy + Clone, I: StaticType> Clone for DowncastNode<N, I> {
fn clone(&self) -> Self {
Self(self.0, self.1)
@@ -127,7 +159,7 @@ where
}
}*/
use graphene_core::{ops::Dynamic, AsBoxNode};
use graphene_core::{ops::Dynamic, AsRefNode};
pub struct BoxedComposition<'a, Second> {
pub first: Box<dyn Node<(), Output = Dynamic<'a>>>,
pub second: Second,

View File

@@ -198,6 +198,15 @@ impl NodeInput {
}
}
impl ProtoNodeInput {
pub fn unwrap_node(self) -> NodeId {
match self {
ProtoNodeInput::Node(id) => id,
_ => panic!("tried to unwrap id from non node input"),
}
}
}
impl ProtoNode {
pub fn id() -> Self {
Self {