File size: 4,494 Bytes
2409829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::Node;
use std::cell::{Cell, RefCell, RefMut};
use std::marker::PhantomData;

#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct IntNode<const N: u32>;

impl<'i, const N: u32, I> Node<'i, I> for IntNode<N> {
	type Output = u32;
	#[inline(always)]
	fn eval(&'i self, _input: I) -> Self::Output {
		N
	}
}

#[derive(Default, Debug, Clone, Copy)]
pub struct ValueNode<T>(pub T);

impl<'i, T: 'i, I> Node<'i, I> for ValueNode<T> {
	type Output = &'i T;
	#[inline(always)]
	fn eval(&'i self, _input: I) -> Self::Output {
		&self.0
	}
}

impl<T> ValueNode<T> {
	pub const fn new(value: T) -> ValueNode<T> {
		ValueNode(value)
	}
}

impl<T> From<T> for ValueNode<T> {
	fn from(value: T) -> Self {
		ValueNode::new(value)
	}
}

#[derive(Default, Debug, Clone, Copy)]
pub struct AsRefNode<T: AsRef<U>, U>(pub T, PhantomData<U>);

impl<'i, T: 'i + AsRef<U>, U: 'i> Node<'i, ()> for AsRefNode<T, U> {
	type Output = &'i U;
	#[inline(always)]
	fn eval(&'i self, _input: ()) -> Self::Output {
		self.0.as_ref()
	}
}

impl<T: AsRef<U>, U> AsRefNode<T, U> {
	pub const fn new(value: T) -> AsRefNode<T, U> {
		AsRefNode(value, PhantomData)
	}
}

#[derive(Default, Debug, Clone)]
pub struct RefCellMutNode<T>(pub RefCell<T>);

impl<'i, T: 'i> Node<'i, ()> for RefCellMutNode<T> {
	type Output = RefMut<'i, T>;
	#[inline(always)]
	fn eval(&'i self, _input: ()) -> Self::Output {
		let a = self.0.borrow_mut();
		a
	}
}

impl<T> RefCellMutNode<T> {
	pub const fn new(value: T) -> RefCellMutNode<T> {
		RefCellMutNode(RefCell::new(value))
	}
}

#[derive(Default)]
pub struct OnceCellNode<T>(pub Cell<T>);

impl<'i, T: Default + 'i, I> Node<'i, I> for OnceCellNode<T> {
	type Output = T;
	#[inline(always)]
	fn eval(&'i self, _input: I) -> Self::Output {
		self.0.replace(T::default())
	}
}

impl<T> OnceCellNode<T> {
	pub const fn new(value: T) -> OnceCellNode<T> {
		OnceCellNode(Cell::new(value))
	}
}

#[derive(Clone, Copy)]
pub struct ClonedNode<T: Clone>(pub T);

impl<'i, T: Clone + 'i, I> Node<'i, I> for ClonedNode<T> {
	type Output = T;
	#[inline(always)]
	fn eval(&'i self, _input: I) -> Self::Output {
		self.0.clone()
	}
}

impl<T: Clone> ClonedNode<T> {
	pub const fn new(value: T) -> ClonedNode<T> {
		ClonedNode(value)
	}
}

impl<T: Clone> From<T> for ClonedNode<T> {
	fn from(value: T) -> Self {
		ClonedNode::new(value)
	}
}

#[derive(Clone, Copy)]
/// The DebugClonedNode logs every time it is evaluated.
/// This is useful for debugging.
pub struct DebugClonedNode<T: Clone>(pub T);

impl<'i, T: Clone + 'i> Node<'i, ()> for DebugClonedNode<T> {
	type Output = T;
	#[inline(always)]
	fn eval(&'i self, _input: ()) -> Self::Output {
		#[cfg(not(target_arch = "spirv"))]
		// KEEP THIS `debug!()` - It acts as the output for the debug node itself
		log::debug!("DebugClonedNode::eval");

		self.0.clone()
	}
}

impl<T: Clone> DebugClonedNode<T> {
	pub const fn new(value: T) -> DebugClonedNode<T> {
		DebugClonedNode(value)
	}
}

#[derive(Clone, Copy)]
pub struct CopiedNode<T: Copy>(pub T);

impl<'i, T: Copy + 'i, I> Node<'i, I> for CopiedNode<T> {
	type Output = T;
	#[inline(always)]
	fn eval(&'i self, _input: I) -> Self::Output {
		self.0
	}
}

impl<T: Copy> CopiedNode<T> {
	pub const fn new(value: T) -> CopiedNode<T> {
		CopiedNode(value)
	}
}

#[derive(Default)]
pub struct DefaultNode<T>(PhantomData<T>);

impl<'i, T: Default + 'i, I> Node<'i, I> for DefaultNode<T> {
	type Output = T;
	fn eval(&'i self, _input: I) -> Self::Output {
		T::default()
	}
}

impl<T> DefaultNode<T> {
	pub fn new() -> Self {
		Self(PhantomData)
	}
}

#[repr(C)]
/// Return the unit value
#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct ForgetNode;

impl<'i, T: 'i> Node<'i, T> for ForgetNode {
	type Output = ();
	fn eval(&'i self, _input: T) -> Self::Output {}
}

impl ForgetNode {
	pub const fn new() -> Self {
		ForgetNode
	}
}

#[cfg(test)]
mod test {
	use super::*;

	#[test]
	fn test_int_node() {
		let node = IntNode::<5>;
		assert_eq!(node.eval(()), 5);
	}
	#[test]
	fn test_value_node() {
		let node = ValueNode::new(5);
		assert_eq!(node.eval(()), &5);
		let type_erased = &node as &dyn for<'a> Node<'a, (), Output = &'a i32>;
		assert_eq!(type_erased.eval(()), &5);
	}
	#[test]
	fn test_default_node() {
		let node = DefaultNode::<u32>::new();
		assert_eq!(node.eval(42), 0);
	}
	#[test]
	#[allow(clippy::unit_cmp)]
	fn test_unit_node() {
		let node = ForgetNode::new();
		assert_eq!(node.eval(()), ());
	}
}