#[cfg(feature = "math")]
pub mod math;
#[cfg(feature = "random")]
pub mod random;
use crate::{Input, Node, Output, Register};
pub struct Const<T> {
val: T,
output: Output<T>,
}
impl<T: Default> Default for Const<T> {
fn default() -> Self {
Self {
val: T::default(),
output: Output::default(),
}
}
}
impl<T> Const<T> {
pub fn new(val: T) -> Self {
Self {
val,
output: Output::default(),
}
}
}
impl<T: 'static + Clone> Node for Const<T> {
fn register(&self, r: &mut Register) {
r.output("output", &self.output);
}
fn process(&mut self) {}
fn reset(&mut self) {
self.output.set(self.val.clone());
}
}
pub struct Map<T, U, F> {
map: F,
input: Input<T>,
output: Output<U>,
}
impl<T, U, F> Map<T, U, F> {
pub fn new(map: F) -> Self
where
F: FnMut(T) -> U,
{
Self {
map,
input: Input::default(),
output: Output::default(),
}
}
}
impl<T, U, F> Node for Map<T, U, F>
where
T: 'static + Clone,
U: 'static,
F: FnMut(T) -> U,
{
fn register(&self, r: &mut Register) {
r.output("output", &self.output);
r.input("input", &self.input);
}
fn process(&mut self) {
if let Some(val) = self.input.get() {
self.output.set((self.map)(val))
}
}
fn reset(&mut self) {}
}
pub struct FromIter<I: Iterator> {
iter: I,
output: Output<I::Item>,
}
impl<I: Iterator> FromIter<I> {
pub fn new(iter: impl IntoIterator<Item = I::Item, IntoIter = I>) -> Self {
Self {
iter: iter.into_iter(),
output: Output::default(),
}
}
}
impl<I> Node for FromIter<I>
where
I: Iterator,
I::Item: 'static + Clone,
{
fn register(&self, r: &mut Register) {
r.output("output", &self.output);
}
fn process(&mut self) {
self.output.set(self.iter.next());
}
fn reset(&mut self) {
}
}
pub struct Hold<T> {
input: Input<T>,
resample: Input<bool>,
output: Output<T>,
}
impl<T> Default for Hold<T> {
fn default() -> Self {
Self {
input: Input::default(),
resample: Input::default(),
output: Output::default(),
}
}
}
impl<T> Hold<T> {
pub fn new() -> Self {
Self::default()
}
}
impl<T: 'static + Clone> Node for Hold<T> {
fn register(&self, r: &mut Register) {
r.input("input", &self.input);
r.input("resample", &self.resample);
r.output("output", &self.output);
}
fn process(&mut self) {
if self.resample.get().unwrap_or_default() {
if let Some(val) = self.input.get() {
self.output.set(val);
}
}
}
fn reset(&mut self) {}
}
pub struct Offset<T> {
input: Input<T>,
output: Output<T>,
buffer: Box<[Option<T>]>,
idx: usize,
}
impl<T> Offset<T> {
pub fn new(offset: usize) -> Self {
let vec: Vec<Option<T>> = (0..offset).map(|_| None).collect();
Self {
input: Input::default(),
output: Output::default(),
buffer: vec.into_boxed_slice(),
idx: 0,
}
}
}
impl<T: 'static + Clone> Node for Offset<T> {
fn register(&self, r: &mut Register) {
r.input("input", &self.input);
r.output("output", &self.output);
}
fn process(&mut self) {
let val = unsafe { self.buffer.get_unchecked_mut(self.idx) };
self.output.set(val.take());
*val = self.input.get();
self.idx = (self.idx + 1) % self.buffer.len();
}
fn reset(&mut self) {
for val in self.buffer.as_mut() {
*val = None;
}
}
}
pub struct ReplaceNone<T> {
default: T,
input: Input<T>,
output: Output<T>,
}
impl<T: Default> Default for ReplaceNone<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T> ReplaceNone<T> {
pub fn new(default: T) -> Self {
Self {
default,
input: Input::default(),
output: Output::default(),
}
}
}
impl<T: 'static + Clone> Node for ReplaceNone<T> {
fn register(&self, r: &mut Register) {
r.input("input", &self.input);
r.output("output", &self.output);
}
fn process(&mut self) {
self.output
.set(self.input.get().unwrap_or_else(|| self.default.clone()))
}
fn reset(&mut self) {}
}
pub struct Pulse {
n: usize,
current: usize,
output: Output<bool>,
}
impl Pulse {
pub fn new(n: usize) -> Self {
Self {
n,
current: 0,
output: Output::default(),
}
}
}
impl Node for Pulse {
fn register(&self, r: &mut Register) {
r.output("output", &self.output);
}
fn process(&mut self) {
if self.current == 0 {
self.output.set(true);
self.current = self.n;
} else {
self.output.set(false);
self.current -= 1;
}
}
fn reset(&mut self) {
self.current = 0;
}
}