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
use crate::anyhow::Result;
use crate::runtime::Block;
use crate::runtime::BlockMeta;
use crate::runtime::BlockMetaBuilder;
use crate::runtime::Kernel;
use crate::runtime::MessageIo;
use crate::runtime::MessageIoBuilder;
use crate::runtime::StreamIo;
use crate::runtime::StreamIoBuilder;
use crate::runtime::WorkIo;

/// Drop samples, printing tags.
///
/// Console output is prefixed with the `name` to help differentiate the output from multiple tag debug blocks.
///
/// # Inputs
///
/// `in`: Stream to drop
///
/// # Outputs
///
/// No outputs
///
/// # Usage
/// ```
/// use futuresdr::blocks::TagDebug;
/// use futuresdr::runtime::Flowgraph;
/// use futuresdr::num_complex::Complex32;
///
/// let mut fg = Flowgraph::new();
///
/// let sink = fg.add_block(TagDebug::<Complex32>::new("foo"));
/// ```
pub struct TagDebug<T: Send + 'static> {
    name: String,
    n_received: usize,
    _type: std::marker::PhantomData<T>,
}

impl<T: Send + 'static> TagDebug<T> {
    /// Create Tag Debug block
    pub fn new(name: impl Into<String>) -> Block {
        Block::new(
            BlockMetaBuilder::new("TagDebug").build(),
            StreamIoBuilder::new().add_input::<T>("in").build(),
            MessageIoBuilder::new().build(),
            TagDebug::<T> {
                _type: std::marker::PhantomData,
                name: name.into(),
                n_received: 0,
            },
        )
    }
}

#[doc(hidden)]
#[async_trait]
impl<T: Send + 'static> Kernel for TagDebug<T> {
    async fn work(
        &mut self,
        io: &mut WorkIo,
        sio: &mut StreamIo,
        _mio: &mut MessageIo<Self>,
        _meta: &mut BlockMeta,
    ) -> Result<()> {
        let i = sio.input(0).slice::<T>();

        let n = i.len();
        sio.input(0)
            .tags()
            .iter()
            .filter(|x| x.index < n)
            .for_each(|x| {
                println!(
                    "TagDebug {}: buf {}/abs {} -- {:?}",
                    &self.name,
                    x.index,
                    self.n_received + x.index,
                    x.tag
                )
            });

        sio.input(0).consume(n);
        self.n_received += n;

        if sio.input(0).finished() {
            io.finished = true;
        }

        Ok(())
    }
}