mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2025-08-04 05:18:19 +00:00
Restructure node graph proxy architecture
This commit is contained in:
parent
cb337fd338
commit
90e465b35c
11 changed files with 128 additions and 81 deletions
|
@ -3,8 +3,10 @@ name = "graphene-core"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
description = "Api definitions for graphene"
|
||||
authors = ["Dennis Kobert <dennis@kobert.dev>"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
|
||||
[dependencies]
|
||||
dyn-any = {path = "../dyn-any", features = ["derive"]}
|
||||
|
|
|
@ -3,6 +3,7 @@ pub mod ops;
|
|||
pub mod structural;
|
||||
pub mod value;
|
||||
|
||||
use dyn_any::{downcast_ref, DynAny, StaticType};
|
||||
use std::any::Any;
|
||||
|
||||
#[rustfmt::skip]
|
||||
|
@ -12,42 +13,32 @@ pub trait Node< 'n, Input> {
|
|||
fn eval(&'n self, input: &'n Input) -> Self::Output;
|
||||
}
|
||||
|
||||
pub trait Exec<'n> {
|
||||
type Output: 'n;
|
||||
fn exec(&'n self) -> Self::Output;
|
||||
}
|
||||
impl<'n, T: Exec<'n>> Node<'n, ()> for T {
|
||||
type Output = <Self as Exec<'n>>::Output;
|
||||
fn eval(&'n self, _input: &()) -> Self::Output {
|
||||
self.exec()
|
||||
pub trait Exec<'n>: Node<'n, ()> {
|
||||
fn exec(&'n self) -> Self::Output {
|
||||
self.eval(&())
|
||||
}
|
||||
}
|
||||
impl<'n, T: Node<'n, ()>> Exec<'n> for T {}
|
||||
|
||||
pub trait Cache {
|
||||
fn clear(&mut self);
|
||||
}
|
||||
|
||||
pub type DynNode<'n, T> = &'n (dyn Node<'n, (), Output = T> + 'n);
|
||||
pub type DynAnyNode<'n> = &'n (dyn Node<'n, (), Output = &'n dyn DynAny<'n>> + 'n);
|
||||
|
||||
pub trait DynamicInput<'n> {
|
||||
fn set_kwarg_by_name(
|
||||
&mut self,
|
||||
name: &str,
|
||||
value: &'n dyn Node<'n, (), Output = &'n (dyn Any + 'static)>,
|
||||
);
|
||||
fn set_arg_by_index(
|
||||
&mut self,
|
||||
index: usize,
|
||||
value: &'n dyn Node<'n, (), Output = &'n (dyn Any + 'static)>,
|
||||
);
|
||||
fn set_kwarg_by_name(&mut self, name: &str, value: DynAnyNode<'n>);
|
||||
fn set_arg_by_index(&mut self, index: usize, value: DynAnyNode<'n>);
|
||||
}
|
||||
|
||||
pub trait AnyRef<'n, I>: Node<'n, I> {
|
||||
fn any(&'n self, input: &'n dyn Any) -> Self::Output
|
||||
where
|
||||
I: 'static + Copy;
|
||||
pub trait AnyRef<'n, I: StaticType<'n>>: Node<'n, I> {
|
||||
fn any(&'n self, input: &'n dyn DynAny<'n>) -> Self::Output;
|
||||
}
|
||||
|
||||
impl<'n, T: Node<'n, I>, I> AnyRef<'n, I> for T {
|
||||
fn any(&'n self, input: &'n dyn Any) -> Self::Output
|
||||
where
|
||||
I: 'static + Copy,
|
||||
{
|
||||
self.eval(input.downcast_ref::<I>().unwrap_or_else(|| {
|
||||
impl<'n, T: Node<'n, I>, I: StaticType<'n>> AnyRef<'n, I> for T {
|
||||
fn any(&'n self, input: &'n dyn DynAny<'n>) -> Self::Output {
|
||||
self.eval(downcast_ref::<I>(input).unwrap_or_else(|| {
|
||||
panic!(
|
||||
"Node was evaluated with wrong input. The input has to be of type: {}",
|
||||
std::any::type_name::<I>(),
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
use std::{any::Any, marker::PhantomData};
|
||||
|
||||
use crate::{Exec, Node};
|
||||
use crate::Node;
|
||||
|
||||
pub struct IntNode<const N: u32>;
|
||||
impl<'n, const N: u32> Exec<'n> for IntNode<N> {
|
||||
impl<'n, const N: u32> Node<'n, ()> for IntNode<N> {
|
||||
type Output = u32;
|
||||
fn exec(&self) -> u32 {
|
||||
fn eval(&self, _input: &()) -> u32 {
|
||||
N
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ValueNode<'n, T>(T, PhantomData<&'n ()>);
|
||||
impl<'n, T: 'n> Exec<'n> for ValueNode<'n, T> {
|
||||
impl<'n, T: 'n> Node<'n, ()> for ValueNode<'n, T> {
|
||||
type Output = &'n T;
|
||||
fn exec(&'n self) -> &'n T {
|
||||
fn eval(&self, _input: &()) -> &T {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
@ -26,9 +26,9 @@ impl<'n, T> ValueNode<'n, T> {
|
|||
|
||||
#[derive(Default)]
|
||||
pub struct DefaultNode<T>(PhantomData<T>);
|
||||
impl<'n, T: Default + 'n> Exec<'n> for DefaultNode<T> {
|
||||
impl<'n, T: Default + 'n> Node<'n, ()> for DefaultNode<T> {
|
||||
type Output = T;
|
||||
fn exec(&self) -> T {
|
||||
fn eval(&self, _input: &()) -> T {
|
||||
T::default()
|
||||
}
|
||||
}
|
||||
|
@ -38,13 +38,14 @@ impl<T> DefaultNode<T> {
|
|||
}
|
||||
}
|
||||
|
||||
use dyn_any::{DynAny, StaticType};
|
||||
pub struct AnyRefNode<'n, N: Node<'n, I, Output = &'n O>, I, O>(
|
||||
&'n N,
|
||||
PhantomData<&'n I>,
|
||||
PhantomData<&'n O>,
|
||||
);
|
||||
impl<'n, N: Node<'n, I, Output = &'n O>, I, O: 'static> Node<'n, I> for AnyRefNode<'n, N, I, O> {
|
||||
type Output = &'n (dyn Any + 'static);
|
||||
impl<'n, N: Node<'n, I, Output = &'n O>, I, O: DynAny<'n>> Node<'n, I> for AnyRefNode<'n, N, I, O> {
|
||||
type Output = &'n (dyn DynAny<'n>);
|
||||
fn eval(&'n self, input: &'n I) -> Self::Output {
|
||||
let value: &O = self.0.eval(input);
|
||||
value
|
||||
|
@ -57,10 +58,10 @@ impl<'n, N: Node<'n, I, Output = &'n O>, I, O: 'static> AnyRefNode<'n, N, I, O>
|
|||
}
|
||||
|
||||
pub struct DefaultRefNode<'n, T>(ValueNode<'n, T>);
|
||||
impl<'n, T: 'n> Exec<'n> for DefaultRefNode<'n, T> {
|
||||
impl<'n, T: 'n> Node<'n, ()> for DefaultRefNode<'n, T> {
|
||||
type Output = &'n T;
|
||||
fn exec(&'n self) -> &'n T {
|
||||
self.0.exec()
|
||||
fn eval(&'n self, _input: &'n ()) -> &'n T {
|
||||
self.0.eval(&())
|
||||
}
|
||||
}
|
||||
impl<'n, T: Default> Default for DefaultRefNode<'n, T> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue