Implement node registry (#822)

This commit is contained in:
TrueDoctor
2022-10-26 00:32:50 +02:00
committed by GitHub
parent 3fe552ff20
commit 097899611f
10 changed files with 213 additions and 45 deletions

View File

@@ -48,7 +48,7 @@ where
}
}
pub trait AsBoxNode<'n, T>
pub trait AsRefNode<'n, T>
where
&'n Self: Node<T>,
Self: 'n,
@@ -57,7 +57,7 @@ where
fn eval_box(&'n self, input: T) -> <Self>::Output;
}
impl<'n, N: 'n, I> AsBoxNode<'n, I> for N
impl<'n, N: 'n, I> AsRefNode<'n, I> for N
where
&'n N: Node<I, Output = N::Output>,
N: Node<I>,
@@ -69,7 +69,7 @@ where
}
}
impl<'n, T> Node<T> for &'n (dyn AsBoxNode<'n, T, Output = T> + 'n) {
impl<'n, T> Node<T> for &'n (dyn AsRefNode<'n, T, Output = T> + 'n) {
type Output = T;
fn eval(self, input: T) -> Self::Output {
self.eval_box(input)

View File

@@ -1,7 +1,7 @@
use core::marker::PhantomData;
use core::ops::Add;
use crate::Node;
use crate::{Node, RefNode};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AddNode;
@@ -153,18 +153,24 @@ impl<'n, T: Clone + 'n> Node<T> for DupNode {
/// Return the Input Argument
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IdNode;
impl<'n, T: 'n> Node<T> for IdNode {
impl<T> Node<T> for IdNode {
type Output = T;
fn eval(self, input: T) -> Self::Output {
input
}
}
impl<'n, T: 'n> Node<T> for &'n IdNode {
impl<'n, T> Node<T> for &'n IdNode {
type Output = T;
fn eval(self, input: T) -> Self::Output {
input
}
}
impl<T> RefNode<T> for IdNode {
type Output = T;
fn eval_ref(&self, input: T) -> Self::Output {
input
}
}
pub struct MapResultNode<MN, I, E>(pub MN, pub PhantomData<(I, E)>);
@@ -177,7 +183,7 @@ impl<MN: Node<I>, I, E> Node<Result<I, E>> for MapResultNode<MN, I, E> {
impl<'n, MN: Node<I> + Copy, I, E> Node<Result<I, E>> for &'n MapResultNode<MN, I, E> {
type Output = Result<MN::Output, E>;
fn eval(self, input: Result<I, E>) -> Self::Output {
input.map(|x| (&self.0).eval(x))
input.map(|x| self.0.eval(x))
}
}

View File

@@ -1,6 +1,6 @@
use core::marker::PhantomData;
use crate::{Node, RefNode};
use crate::{AsRefNode, Node, RefNode};
#[derive(Debug)]
pub struct ComposeNode<First, Second, Input> {
@@ -26,17 +26,19 @@ where
}
impl<'n, Input, Inter, First, Second> Node<Input> for &'n ComposeNode<First, Second, Input>
where
First: RefNode<Input, Output = Inter> + Copy,
Second: RefNode<Inter> + Copy,
First: AsRefNode<'n, Input, Output = Inter>,
Second: AsRefNode<'n, Inter>,
&'n First: Node<Input, Output = Inter>,
&'n Second: Node<Inter>,
{
type Output = <Second as RefNode<Inter>>::Output;
type Output = <Second as AsRefNode<'n, Inter>>::Output;
fn eval(self, input: Input) -> Self::Output {
// evaluate the first node with the given input
// and then pipe the result from the first computation
// into the second node
let arg: Inter = (self.first).eval_ref(input);
(self.second).eval_ref(arg)
let arg: Inter = (self.first).eval_box(input);
(self.second).eval_box(arg)
}
}
impl<Input, Inter, First, Second> RefNode<Input> for ComposeNode<First, Second, Input>
@@ -136,3 +138,8 @@ impl<'n, Root: Node<T> + Copy, T: From<()>, Input> Node<Input> for &'n ConsNode<
(input, arg)
}
}
impl<Root, T: From<()>> ConsNode<Root, T> {
pub fn new(root: Root) -> Self {
ConsNode(root, PhantomData)
}
}