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
use super::{Data, Read, UniqueIdentifier, Update, Write};
use std::{marker::PhantomData, sync::Arc};

/// Rate transitionner
#[derive(Debug)]
pub struct Pulse<T, U: UniqueIdentifier<DataType = T>, V: UniqueIdentifier<DataType = T> = U> {
    default: Arc<T>,
    data: Arc<T>,
    width: usize,
    step: usize,
    input: PhantomData<U>,
    output: PhantomData<V>,
}
impl<T, U, V> Pulse<T, U, V>
where
    U: UniqueIdentifier<DataType = T>,
    V: UniqueIdentifier<DataType = T>,
{
    /// Creates a new sampler with initial condition
    pub fn new(width: usize, default: T) -> Self {
        let default = Arc::new(default);
        Self {
            data: Arc::clone(&default),
            default,
            input: PhantomData,
            output: PhantomData,
            width,
            step: 0,
        }
    }
}
impl<T, U, V> Update for Pulse<T, U, V>
where
    T: Send + Sync,
    U: UniqueIdentifier<DataType = T>,
    V: UniqueIdentifier<DataType = T>,
{
}
impl<T, U, V> Read<U> for Pulse<T, U, V>
where
    T: Send + Sync,
    U: UniqueIdentifier<DataType = T>,
    V: UniqueIdentifier<DataType = T>,
{
    fn read(&mut self, data: Data<U>) {
        self.step = 0;
        self.data = data.into_arc();
    }
}
impl<T, U, V> Write<V> for Pulse<T, U, V>
where
    T: Clone + Default + Send + Sync,
    U: UniqueIdentifier<DataType = T>,
    V: UniqueIdentifier<DataType = T>,
    Data<V>: Default,
{
    fn write(&mut self) -> Option<Data<V>> {
        if self.step < self.width {
            self.step += 1;
            Some(Data::<V>::from(&self.data))
        } else {
            self.step += 1;
            Some(Data::<V>::from(&self.default))
        }
    }
}