Remove input from the node definition

This commit is contained in:
Dennis 2022-06-05 01:15:22 +02:00 committed by Keavon Chambers
parent f6e4dbf3e3
commit fa5345d33f
4 changed files with 99 additions and 84 deletions

View File

@ -1,39 +1,43 @@
use core::marker::PhantomData;
use crate::Node;
pub struct FnNode<T: Fn(In) -> O, In, O>(T, PhantomData<In>, PhantomData<O>);
impl<'n, T: Fn(In) -> O, In, O: 'n> Node<'n, In> for FnNode<T, In, O> {
type Output = O;
fn eval(&'n self, input: In) -> Self::Output {
self.0(input)
}
}
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>(
pub struct FnNode<'n, T: Fn(<N as Node>::Output) -> O, N: Node<'n>, O: 'n>(
T,
State,
PhantomData<In>,
&'n N,
PhantomData<O>,
);
impl<'n, T: Fn(In, &State) -> O, In, O: 'n, State> Node<'n, In>
for FnNodeWithState<T, In, O, State>
impl<'n, T: Fn(<N as Node>::Output) -> O, N: Node<'n>, O> Node<'n> for FnNode<'n, T, N, O> {
type Output = O;
fn eval(&'n self) -> Self::Output {
self.0(self.1.eval())
}
}
impl<'n, T: Fn(<N as Node>::Output) -> O, N: Node<'n>, O> FnNode<'n, T, N, O> {
pub fn new(f: T, input: &'n N) -> Self {
FnNode(f, input, PhantomData)
}
}
pub struct FnNodeWithState<'n, T: Fn(N::Output, &State) -> O, N: Node<'n>, O, State>(
T,
&'n N,
State,
PhantomData<O>,
);
impl<'n, T: Fn(N::Output, &State) -> O, N: Node<'n>, O: 'n, State> Node<'n>
for FnNodeWithState<'n, T, N, O, State>
{
type Output = O;
fn eval(&'n self, input: In) -> Self::Output {
self.0(input, &self.1)
fn eval(&'n self) -> Self::Output {
self.0(self.1.eval(), &self.2)
}
}
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())
impl<'n, T: Fn(N::Output, &State) -> O, N: Node<'n>, O, State> FnNodeWithState<'n, T, N, O, State> {
pub fn new(f: T, input: &'n N, state: State) -> Self {
FnNodeWithState(f, input, state, PhantomData)
}
}

View File

@ -3,24 +3,16 @@
pub mod generic;
pub mod ops;
pub mod structural;
//pub mod structural;
pub mod value;
#[rustfmt::skip]
pub trait Node< 'n, Input> {
type Output : 'n;
pub trait Node<'n> {
type Output: 'n; // TODO: replace with generic associated type
fn eval(&'n self, input: Input) -> Self::Output;
fn eval(&'n self) -> Self::Output;
}
// TODO: Fix exec trait
pub trait Exec<'n>: Node<'n, ()> {
fn exec(&'n self) -> Self::Output {
self.eval(())
}
}
impl<'n, T: Node<'n, ()>> Exec<'n> for T {}
pub trait Cache {
fn clear(&mut self);
}

View File

@ -1,52 +1,84 @@
use core::{marker::PhantomData, ops::Add};
use core::ops::Add;
use crate::Node;
#[repr(C)]
#[derive(Default)]
pub struct AddNode<T>(PhantomData<T>);
impl<'n, T: Add + Copy + 'n> Node<'n, (T, T)> for AddNode<T> {
struct AddNode<'n, T: Add, I1: Node<'n, Output = T>, I2: Node<'n, Output = T>>(
pub &'n I1,
pub &'n I2,
);
impl<'n, T: Add + 'n, I1: Node<'n, Output = T>, I2: Node<'n, Output = T>> Node<'n>
for AddNode<'n, T, I1, I2>
{
type Output = <T as Add>::Output;
fn eval(&'n self, input: (T, T)) -> T::Output {
let (a, b) = input;
a + b
fn eval(&self) -> T::Output {
self.0.eval() + self.1.eval()
}
}
#[repr(C)]
#[derive(Default)]
/// Destructures a Tuple of two values and returns the first one
pub struct FstNode<T, U>(PhantomData<T>, PhantomData<U>);
impl<'n, T: Copy + 'n, U> Node<'n, (T, U)> for FstNode<T, U> {
pub struct CloneNode<'n, N: Node<'n, Output = &'n O>, O: Clone + 'n>(pub &'n N);
impl<'n, N: Node<'n, Output = &'n O>, O: Clone> Node<'n> for CloneNode<'n, N, O> {
type Output = O;
fn eval(&self) -> Self::Output {
self.0.eval().clone()
}
}
#[repr(C)]
pub struct FstNode<'n, N: Node<'n>>(pub &'n N);
impl<'n, T: 'n, U, N: Node<'n, Output = (T, U)>> Node<'n> for FstNode<'n, N> {
type Output = T;
fn eval(&'n self, input: (T, U)) -> Self::Output {
let (a, _) = input;
fn eval(&self) -> Self::Output {
let (a, _) = self.0.eval();
a
}
}
#[repr(C)]
#[derive(Default)]
/// Destructures a Tuple of two values and returns the first one
pub struct SndNode<T, U>(PhantomData<T>, PhantomData<U>);
impl<'n, T, U: Copy + 'n> Node<'n, (T, U)> for SndNode<T, U> {
pub struct SndNode<'n, N: Node<'n>>(pub &'n N);
impl<'n, T, U: 'n, N: Node<'n, Output = (T, U)>> Node<'n> for SndNode<'n, N> {
type Output = U;
fn eval(&'n self, input: (T, U)) -> Self::Output {
let (_, b) = input;
fn eval(&self) -> Self::Output {
let (_, b) = self.0.eval();
b
}
}
#[repr(C)]
#[derive(Default)]
/// Destructures a Tuple of two values and returns the first one
pub struct DupNode<T>(PhantomData<T>);
impl<'n, T: Copy + 'n> Node<'n, T> for DupNode<T> {
type Output = (T, T);
fn eval(&'n self, input: T) -> Self::Output {
(input, input)
pub struct SndRefNode<'n, N: Node<'n>>(pub &'n N);
impl<'n, T: 'n, U: 'n, N: Node<'n, Output = &'n (T, U)>> Node<'n> for SndRefNode<'n, N> {
type Output = &'n U;
fn eval(&self) -> Self::Output {
let (_, ref b) = self.0.eval();
b
}
}
#[repr(C)]
/// Destructures a Tuple of two values and returns the first one
pub struct DupNode<'n, N: Node<'n>>(&'n N);
impl<'n, N: Node<'n>> Node<'n> for DupNode<'n, N> {
type Output = (N::Output, N::Output);
fn eval(&self) -> Self::Output {
(self.0.eval(), self.0.eval()) //TODO: use Copy/Clone implementation
}
}
pub fn foo() {
let value = crate::value::ValueNode::new(2u32);
let dup = DupNode(&value);
fn swap<'n>(input: (&'n u32, &'n u32)) -> (&'n u32, &'n u32) {
(input.1, input.0)
}
//let fnn = crate::generic::FnNode::new(swap, &dup); TODO fix types
let snd = SndNode(&dup);
let add = AddNode(&snd, &value);
let _ = add.eval();
}
#[cfg(target_arch = "spirv")]
pub mod gpu {
//#![deny(warnings)]

View File

@ -3,33 +3,33 @@ use core::marker::PhantomData;
use crate::Node;
pub struct IntNode<const N: u32>;
impl<'n, const N: u32> Node<'n, ()> for IntNode<N> {
impl<'n, const N: u32> Node<'n> for IntNode<N> {
type Output = u32;
fn eval(&self, _input: ()) -> u32 {
fn eval(&self) -> u32 {
N
}
}
#[derive(Default)]
pub struct ValueNode<'n, T>(T, PhantomData<&'n ()>);
impl<'n, T: 'n> Node<'n, ()> for ValueNode<'n, T> {
pub struct ValueNode<T>(pub T);
impl<'n, T: 'n> Node<'n> for ValueNode<T> {
type Output = &'n T;
fn eval(&self, _input: ()) -> &T {
fn eval(&'n self) -> Self::Output {
&self.0
}
}
impl<'n, T> ValueNode<'n, T> {
pub const fn new(value: T) -> ValueNode<'n, T> {
ValueNode(value, PhantomData)
impl<'n, T> ValueNode<T> {
pub const fn new(value: T) -> ValueNode<T> {
ValueNode(value)
}
}
#[derive(Default)]
pub struct DefaultNode<T>(PhantomData<T>);
impl<'n, T: Default + 'n> Node<'n, ()> for DefaultNode<T> {
impl<'n, T: Default + 'n> Node<'n> for DefaultNode<T> {
type Output = T;
fn eval(&self, _input: ()) -> T {
fn eval(&self) -> T {
T::default()
}
}
@ -38,16 +38,3 @@ impl<T> DefaultNode<T> {
DefaultNode(PhantomData)
}
}
pub struct DefaultRefNode<'n, T>(ValueNode<'n, T>);
impl<'n, T: 'n> Node<'n, ()> for DefaultRefNode<'n, T> {
type Output = &'n T;
fn eval(&'n self, _input: ()) -> &'n T {
self.0.eval(())
}
}
impl<'n, T: Default> Default for DefaultRefNode<'n, T> {
fn default() -> DefaultRefNode<'n, T> {
DefaultRefNode(ValueNode::new(T::default()))
}
}