mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Try and fail to make nodes object-safe in node graph
This commit is contained in:
@@ -6,5 +6,9 @@ description = "Api definitions for graphene"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[features]
|
||||
const_default = ["const-default"]
|
||||
default = ["const_default"]
|
||||
|
||||
[dependencies]
|
||||
const-default = { version = "1.0", optional = true }
|
||||
|
||||
39
node-graph/gcore/src/generic.rs
Normal file
39
node-graph/gcore/src/generic.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use std::{borrow::Borrow, marker::PhantomData};
|
||||
|
||||
use crate::Node;
|
||||
pub struct FnNode<T: Fn(&In) -> O, In, O>(T, PhantomData<In>, PhantomData<O>);
|
||||
impl<T: Fn(&In) -> O, In, O> Node for FnNode<T, In, O> {
|
||||
type Output<'a> = O where Self: 'a;
|
||||
type Input<'a> = In where Self: 'a;
|
||||
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
|
||||
self.0(input.borrow())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Fn(&In) -> O, In, O> FnNode<T, In, O> {
|
||||
pub fn new(f: T) -> Self {
|
||||
FnNode(f, PhantomData::default(), PhantomData::default())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FnNodeWithState<T: Fn(&In, &State) -> O, In, O, State>(
|
||||
T,
|
||||
State,
|
||||
PhantomData<In>,
|
||||
PhantomData<O>,
|
||||
);
|
||||
impl<T: Fn(&In, &State) -> O, In, O, State> Node for FnNodeWithState<T, In, O, State> {
|
||||
type Output<'a> = O where Self: 'a;
|
||||
type Input<'a> = In where Self: 'a;
|
||||
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
|
||||
self.0(input.borrow(), &self.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Fn(&In, &State) -> O, In, O, State> FnNodeWithState<T, In, O, State> {
|
||||
pub fn new(f: T, state: State) -> Self {
|
||||
FnNodeWithState(f, state, PhantomData::default(), PhantomData::default())
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,39 @@
|
||||
#![feature(generic_associated_types)]
|
||||
use std::{any::Any, borrow::Borrow};
|
||||
pub mod generic;
|
||||
pub mod ops;
|
||||
pub mod structural;
|
||||
pub mod value;
|
||||
|
||||
use std::{any::Any, borrow::Borrow, ops::Deref};
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub trait Node {
|
||||
// Self: 'a means that Self has to live at least as long as 'a (the input and output)
|
||||
// this ensures that the node does not spontaneously disappear during evaluation
|
||||
type Output<'a> where Self: 'a;
|
||||
type Input<'a> where Self: 'a;
|
||||
type Input<'i> where Self: 'i;
|
||||
type Output<'o> where Self: 'o;
|
||||
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a>;
|
||||
}
|
||||
|
||||
pub trait SimpleNode<'n, I, O> {
|
||||
fn eval_simple(&self, input: &I) -> &O;
|
||||
}
|
||||
|
||||
impl<T: for<'n> SimpleNode<'n, I, O>, I, O> Node for T {
|
||||
type Input<'i> = &'i I where Self: 'i;
|
||||
type Output<'o> = &'o O where Self: 'o;
|
||||
|
||||
fn eval<'a, In: Borrow<Self::Input<'a>>>(&'a self, input: In) -> Self::Output<'a> {
|
||||
self.eval_simple(input.borrow())
|
||||
}
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub trait OutputNode<'a, T>: Node<Output<'a> = T> where Self: 'a {}
|
||||
#[rustfmt::skip]
|
||||
pub trait ArgNode<'a, T>: OutputNode<'a, T> + Node<Input<'a> = ()> where Self: 'a {}
|
||||
|
||||
pub trait AnyRef: Node {
|
||||
fn any<'a>(&'a self, input: &'a dyn Any) -> Self::Output<'a>
|
||||
where
|
||||
@@ -33,6 +56,24 @@ impl<T: Node> AnyRef for T {
|
||||
}
|
||||
}
|
||||
|
||||
trait Ref<T>: Node {}
|
||||
impl<'a, T: 'a, N: Node<Output<'a> = &'a T> + 'a> Ref<T> for N {}
|
||||
|
||||
pub trait ExecPtr<'n, T>: Node {
|
||||
fn fn_ptr(&self) -> &T;
|
||||
}
|
||||
|
||||
impl<'n, T: 'n, N: Ref<T>> ExecPtr<'n, T> for N
|
||||
where
|
||||
for<'a> &'a (): Borrow<<Self as Node>::Input<'a>>,
|
||||
for<'a> &'a T: From<N::Output<'a>>,
|
||||
{
|
||||
fn fn_ptr(&self) -> &T {
|
||||
let value: &T = self.eval(&()).into();
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Exec: Node
|
||||
where
|
||||
for<'a> &'a (): Borrow<<Self as Node>::Input<'a>>,
|
||||
|
||||
37
node-graph/gcore/src/ops.rs
Normal file
37
node-graph/gcore/src/ops.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::{borrow::Borrow, marker::PhantomData};
|
||||
|
||||
use crate::Node;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AddNode<T>(PhantomData<T>);
|
||||
impl<T: std::ops::Add + 'static + Copy> Node for AddNode<T> {
|
||||
type Output<'a> = <T as std::ops::Add>::Output;
|
||||
type Input<'a> = (T, T);
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> T::Output {
|
||||
input.borrow().0 + input.borrow().1
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
/// Destructures a Tuple of two values and returns the first one
|
||||
pub struct FstNode<T, U>(PhantomData<T>, PhantomData<U>);
|
||||
impl<T: Copy, U> Node for FstNode<T, U> {
|
||||
type Output<'a> = &'a T where Self: 'a;
|
||||
type Input<'a> = &'a (T, U) where Self: 'a;
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'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<T, U>(PhantomData<T>, PhantomData<U>);
|
||||
impl<T, U: Copy> Node for SndNode<T, U> {
|
||||
type Output<'a> = &'a U where Self: 'a;
|
||||
type Input<'a> = &'a (T, U) where Self: 'a;
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
|
||||
let &(_, ref b) = input.borrow();
|
||||
b
|
||||
}
|
||||
}
|
||||
60
node-graph/gcore/src/structural.rs
Normal file
60
node-graph/gcore/src/structural.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use std::{any::Any, borrow::Borrow};
|
||||
|
||||
use crate::{DynamicInput, Node};
|
||||
pub struct ComposeNode<'n, FIRST, SECOND> {
|
||||
first: &'n FIRST,
|
||||
second: &'n SECOND,
|
||||
}
|
||||
|
||||
impl<'n, FIRST, SECOND> Node for ComposeNode<'n, FIRST, SECOND>
|
||||
where
|
||||
FIRST: Node,
|
||||
SECOND: Node,
|
||||
for<'a> FIRST::Output<'a>: Borrow<SECOND::Input<'a>>,
|
||||
{
|
||||
type Input<'a> = FIRST::Input<'a> where Self: 'a;
|
||||
type Output<'a> = SECOND::Output<'a> where Self: 'a;
|
||||
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
|
||||
// evaluate the first node with the given input
|
||||
// and then pipe the result from the first computation
|
||||
// into the second node
|
||||
let arg = self.first.eval(input);
|
||||
self.second.eval(arg)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'n, FIRST, SECOND> ComposeNode<'n, FIRST, SECOND>
|
||||
where
|
||||
FIRST: Node,
|
||||
{
|
||||
pub fn new(first: &'n FIRST, second: &'n SECOND) -> Self {
|
||||
ComposeNode::<'n, FIRST, SECOND> { first, second }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait After: Sized {
|
||||
fn after<'a, First: Node>(&'a self, first: &'a First) -> ComposeNode<'a, First, Self> {
|
||||
ComposeNode::new(first, self)
|
||||
}
|
||||
}
|
||||
impl<Second: Node> After for Second {}
|
||||
|
||||
pub struct ProxyNode<T: DynamicInput>(T);
|
||||
impl<T: DynamicInput> Node for ProxyNode<T> {
|
||||
type Output<'a> = &'a T where Self: 'a;
|
||||
type Input<'a> = &'a () where Self: 'a;
|
||||
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, _input: I) -> Self::Output<'a> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
impl<T: DynamicInput> DynamicInput for ProxyNode<T> {
|
||||
fn set_kwarg_by_name(&mut self, name: &str, value: &dyn Any) {
|
||||
self.0.set_kwarg_by_name(name, value)
|
||||
}
|
||||
|
||||
fn set_arg_by_index(&mut self, index: usize, value: &dyn Any) {
|
||||
self.0.set_arg_by_index(index, value)
|
||||
}
|
||||
}
|
||||
65
node-graph/gcore/src/value.rs
Normal file
65
node-graph/gcore/src/value.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use std::{borrow::Borrow, marker::PhantomData};
|
||||
|
||||
use const_default::ConstDefault;
|
||||
|
||||
use crate::{Exec, Node};
|
||||
|
||||
pub struct IntNode<const N: u32>;
|
||||
impl<const N: u32> Node for IntNode<N> {
|
||||
type Input<'o> = ();
|
||||
type Output<'i> = u32;
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&self, _input: I) -> u32 {
|
||||
N
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ValueNode<T>(T);
|
||||
impl<T> Node for ValueNode<T> {
|
||||
type Input<'i> = () where T: 'i;
|
||||
type Output<'o> = &'o T where T: 'o;
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, _input: I) -> &T {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
impl<T> ValueNode<T> {
|
||||
pub const fn new(value: T) -> ValueNode<T> {
|
||||
ValueNode(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DefaultNode<T>(PhantomData<T>);
|
||||
impl<T: Default> Node for DefaultNode<T> {
|
||||
type Input<'i> = () where T: 'i;
|
||||
type Output<'o> = T where T: 'o;
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, _input: I) -> T {
|
||||
T::default()
|
||||
}
|
||||
}
|
||||
impl<T> DefaultNode<T> {
|
||||
pub const fn new() -> DefaultNode<T> {
|
||||
DefaultNode(PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DefaultRefNode<T>(ValueNode<T>);
|
||||
impl<T: 'static> Node for DefaultRefNode<T> {
|
||||
type Input<'i> = () where T: 'i;
|
||||
type Output<'o> = &'o T where T: 'o;
|
||||
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, _input: I) -> &'a T {
|
||||
self.0.exec()
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "const_default")]
|
||||
impl<T: ConstDefault> DefaultRefNode<T> {
|
||||
pub const fn new() -> DefaultRefNode<T> {
|
||||
DefaultRefNode(ValueNode::new(T::DEFAULT))
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "const_default"))]
|
||||
impl<T: Default> DefaultRefNode<T> {
|
||||
pub fn new() -> DefaultRefNode<T> {
|
||||
DefaultRefNode(ValueNode::new(T::default()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user