Rewrite node graph using generic associated types

This commit is contained in:
Dennis
2022-03-29 23:34:45 +02:00
committed by Keavon Chambers
parent 7a79a892b4
commit 49aa948971
2 changed files with 90 additions and 47 deletions

View File

@@ -1,29 +1,54 @@
#![deny(rust_2018_idioms)]
use std::any::Any;
#![feature(generic_associated_types)]
//#![deny(rust_2018_idioms)]
use std::{any::Any, borrow::Borrow};
mod iter;
mod nodes;
pub mod nodes;
use iter::insert_after_nth;
use nodes::*;
pub trait Node<'n, OUT> {
fn eval(&'n self, input: impl Iterator<Item = &'n dyn Any> + Clone) -> OUT;
// fn source code
// positon
pub trait Node {
type Out<'a>
where
Self: 'a;
type Input<'a>
where
Self: 'a;
fn eval<'a, T: Borrow<Self::Input<'a>>>(&'a self, input: T) -> Self::Out<'a>;
}
trait After<'n, OUT, SECOND: Node<'n, OUT>> {
fn after<INTERMEDIATE, FIRST: Node<'n, INTERMEDIATE>>(
&'n self,
first: &'n FIRST,
) -> ComposeNode<'n, FIRST, SECOND, INTERMEDIATE>;
pub trait AnyRef: Node {
fn any<'a>(&'a self, input: &'a dyn Any) -> Self::Out<'a>
where
Self::Input<'a>: 'static + Copy;
}
impl<T: Node> AnyRef for T {
fn any<'a>(&'a self, input: &'a dyn Any) -> Self::Out<'a>
where
Self::Input<'a>: 'static + Copy,
{
self.eval::<&Self::Input<'a>>(input.downcast_ref::<Self::Input<'a>>().unwrap())
}
}
/*
trait After<SECOND: Node> {
type Out<'a>
where
Self: 'a;
fn after<'a><FIRST: Node>(
&'a self,
first: &'a FIRST,
) -> ComposeNode<'a, FIRST, SECOND, INTERMEDIATE>;
}*/
fn main() {
use std::iter;
let int = IntNode::<32>;
let add: u32 = AddNode::<u32>::default().any(&(int.eval(&()), int.eval(&())) as &dyn Any);
/*
let curry: CurryNthArgNode<'_, _, _, u32, u32, 0> = CurryNthArgNode::new(&AddNode, &int);
let composition = curry.after(&curry);
let n = ValueNode::new(10_u32);
let curry: CurryNthArgNode<'_, _, _, u32, _, 0> = CurryNthArgNode::new(&composition, &n);
println!("{}", curry.eval(iter::empty()))
*/
println!("{}", add)
}