futuresdr/blocks/
null_sink.rs

1use crate::prelude::*;
2
3/// Drop samples.
4///
5/// # Inputs
6///
7/// `in`: Stream to drop
8///
9/// # Outputs
10///
11/// No outputs
12///
13/// # Usage
14/// ```
15/// use futuresdr::blocks::NullSink;
16/// use futuresdr::runtime::Flowgraph;
17/// use num_complex::Complex;
18///
19/// let mut fg = Flowgraph::new();
20///
21/// let sink = fg.add_block(NullSink::<Complex<f32>>::new());
22/// ```
23#[derive(Block)]
24pub struct NullSink<T: CpuSample, I: CpuBufferReader<Item = T> = DefaultCpuReader<T>> {
25    n_received: usize,
26    #[input]
27    input: I,
28}
29
30impl<T, I> NullSink<T, I>
31where
32    T: CpuSample,
33    I: CpuBufferReader<Item = T>,
34{
35    /// Create NullSink block
36    pub fn new() -> Self {
37        Self {
38            n_received: 0,
39            input: I::default(),
40        }
41    }
42    /// Get number of received samples
43    pub fn n_received(&self) -> usize {
44        self.n_received
45    }
46}
47
48impl<T, I> Default for NullSink<T, I>
49where
50    T: CpuSample,
51    I: CpuBufferReader<Item = T>,
52{
53    fn default() -> Self {
54        Self::new()
55    }
56}
57
58#[doc(hidden)]
59impl<T, I> Kernel for NullSink<T, I>
60where
61    T: CpuSample,
62    I: CpuBufferReader<Item = T>,
63{
64    async fn work(
65        &mut self,
66        io: &mut WorkIo,
67        _mio: &mut MessageOutputs,
68        _meta: &mut BlockMeta,
69    ) -> Result<()> {
70        let n = self.input().slice().len();
71        if n > 0 {
72            self.n_received += n;
73            self.input().consume(n);
74        }
75
76        if self.input().finished() {
77            io.finished = true;
78        }
79
80        Ok(())
81    }
82}