Try and fail to make nodes object-safe in node graph

This commit is contained in:
Dennis
2022-04-02 20:50:53 +02:00
committed by Keavon Chambers
parent 800fb4dbc1
commit 3243b80cf2
12 changed files with 200 additions and 64 deletions
+44 -3
View File
@@ -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>>,