Make node trait consume self

This commit is contained in:
Dennis
2022-08-19 18:58:17 +02:00
committed by Keavon Chambers
parent fa0b896825
commit ce9e02f36d
10 changed files with 286 additions and 188 deletions

View File

@@ -2,23 +2,28 @@ use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::sync::atomic::AtomicBool;
use crate::ops::CloneNode;
use crate::structural::ComposeNode;
use crate::Node;
#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct IntNode<const N: u32>;
impl<'n, const N: u32> Node<'n, ()> for IntNode<N> {
impl<const N: u32> Node<()> for IntNode<N> {
type Output = u32;
fn eval(&self, _: ()) -> u32 {
fn eval(self, _: ()) -> u32 {
N
}
}
#[derive(Default)]
#[derive(Default, Debug)]
pub struct ValueNode<T>(pub T);
impl<'n, T: 'n> Node<'n, ()> for ValueNode<T> {
impl<'n, T: 'n> Node<()> for ValueNode<T> {
type Output = T;
fn eval(self, _: ()) -> Self::Output {
self.0
}
}
impl<'n, T: 'n> Node<()> for &'n ValueNode<T> {
type Output = &'n T;
fn eval(&'n self, _: ()) -> Self::Output {
fn eval(self, _: ()) -> Self::Output {
&self.0
}
}
@@ -29,39 +34,60 @@ impl<T> ValueNode<T> {
}
}
impl<'n, T: Clone + 'n> ValueNode<T> {
pub const fn clone(self) -> ComposeNode<'n, (), ValueNode<T>, CloneNode> {
ComposeNode::new(self, CloneNode)
}
}
impl<T> From<T> for ValueNode<T> {
fn from(value: T) -> Self {
ValueNode::new(value)
}
}
impl<T: Clone> Clone for ValueNode<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T: Clone + Copy> Copy for ValueNode<T> {}
#[derive(Default)]
pub struct DefaultNode<T>(PhantomData<T>);
impl<'n, T: Default + 'n> Node<'n, ()> for DefaultNode<T> {
impl<T: Default> Node<()> for DefaultNode<T> {
type Output = T;
fn eval(&self, _: ()) -> T {
fn eval(self, _: ()) -> T {
T::default()
}
}
impl<'n, T: Default + 'n> Node<()> for &'n DefaultNode<T> {
type Output = T;
fn eval(self, _: ()) -> T {
T::default()
}
}
#[repr(C)]
/// Return the unit value
#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct UnitNode;
impl<'n> Node<'n, ()> for UnitNode {
impl Node<()> for UnitNode {
type Output = ();
fn eval(&'n self, _: ()) -> Self::Output {}
fn eval(self, _: ()) -> Self::Output {}
}
impl<'n> Node<()> for &'n UnitNode {
type Output = ();
fn eval(self, _: ()) -> Self::Output {}
}
pub struct InputNode<T>(MaybeUninit<T>, AtomicBool);
impl<'n, T: 'n> Node<'n, ()> for InputNode<T> {
impl<'n, T: 'n> Node<()> for InputNode<T> {
type Output = T;
fn eval(self, _: ()) -> Self::Output {
if self.1.load(core::sync::atomic::Ordering::SeqCst) {
unsafe { self.0.assume_init() }
} else {
panic!("tried to access an input before setting it")
}
}
}
impl<'n, T: 'n> Node<()> for &'n InputNode<T> {
type Output = &'n T;
fn eval(&'n self, _: ()) -> Self::Output {
fn eval(self, _: ()) -> Self::Output {
if self.1.load(core::sync::atomic::Ordering::SeqCst) {
unsafe { self.0.assume_init_ref() }
} else {