mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-16 06:38:03 +08:00
Replace GAT with lifetime on trait in node graph
This commit is contained in:
@@ -1,39 +1,34 @@
|
||||
use std::{borrow::Borrow, marker::PhantomData};
|
||||
|
||||
use const_default::ConstDefault;
|
||||
use std::{any::Any, marker::PhantomData};
|
||||
|
||||
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 {
|
||||
impl<'n, const N: u32> Exec<'n> for IntNode<N> {
|
||||
type Output = u32;
|
||||
fn exec(&self) -> 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 {
|
||||
pub struct ValueNode<'n, T>(T, PhantomData<&'n ()>);
|
||||
impl<'n, T: 'n> Exec<'n> for ValueNode<'n, T> {
|
||||
type Output = &'n T;
|
||||
fn exec(&'n self) -> &'n T {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
impl<T> ValueNode<T> {
|
||||
pub const fn new(value: T) -> ValueNode<T> {
|
||||
ValueNode(value)
|
||||
impl<'n, T> ValueNode<'n, T> {
|
||||
pub const fn new(value: T) -> ValueNode<'n, T> {
|
||||
ValueNode(value, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
#[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 {
|
||||
impl<'n, T: Default + 'n> Exec<'n> for DefaultNode<T> {
|
||||
type Output = T;
|
||||
fn exec(&self) -> T {
|
||||
T::default()
|
||||
}
|
||||
}
|
||||
@@ -43,23 +38,33 @@ impl<T> DefaultNode<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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);
|
||||
fn eval(&'n self, input: &'n I) -> Self::Output {
|
||||
let value: &O = self.0.eval(input);
|
||||
value
|
||||
}
|
||||
}
|
||||
impl<'n, N: Node<'n, I, Output = &'n O>, I, O: 'static> AnyRefNode<'n, N, I, O> {
|
||||
pub fn new(n: &'n N) -> AnyRefNode<'n, N, I, O> {
|
||||
AnyRefNode(n, PhantomData, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DefaultRefNode<'n, T>(ValueNode<'n, T>);
|
||||
impl<'n, T: 'n> Exec<'n> for DefaultRefNode<'n, T> {
|
||||
type Output = &'n T;
|
||||
fn exec(&'n self) -> &'n 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> {
|
||||
impl<'n, T: Default> Default for DefaultRefNode<'n, T> {
|
||||
fn default() -> DefaultRefNode<'n, T> {
|
||||
DefaultRefNode(ValueNode::new(T::default()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user