-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmain.rs
182 lines (162 loc) · 4.4 KB
/
main.rs
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
use futuresdr::blocks::Copy;
use futuresdr::blocks::MessageCopy;
use futuresdr::blocks::MessageSink;
use futuresdr::blocks::MessageSourceBuilder;
use futuresdr::blocks::NullSink;
use futuresdr::blocks::VectorSource;
use futuresdr::macros::async_trait;
use futuresdr::macros::connect;
use futuresdr::macros::message_handler;
use futuresdr::runtime::BlockMeta;
use futuresdr::runtime::BlockMetaBuilder;
use futuresdr::runtime::Flowgraph;
use futuresdr::runtime::Kernel;
use futuresdr::runtime::MessageIo;
use futuresdr::runtime::MessageIoBuilder;
use futuresdr::runtime::Pmt;
use futuresdr::runtime::Result;
use futuresdr::runtime::Runtime;
use futuresdr::runtime::StreamIo;
use futuresdr::runtime::StreamIoBuilder;
use futuresdr::runtime::TypedBlock;
use futuresdr::runtime::WorkIo;
fn main() -> anyhow::Result<()> {
let mut fg = Flowgraph::new();
let src = VectorSource::new(vec![0u32, 1, 2, 3]);
let cpy0 = Copy::<u32>::new();
let cpy1 = Copy::<u32>::new();
let cpy2 = Copy::<u32>::new();
let cpy3 = Copy::<u32>::new();
let snk = NullSink::<u32>::new();
// > indicates stream connections
// default port names (out/in) can be omitted
// blocks can be chained
connect!(fg,
src.out > cpy0.in;
cpy0 > cpy1;
cpy1 > cpy2 > cpy3 > snk
);
let msg_source = MessageSourceBuilder::new(
Pmt::String("foo".to_string()),
std::time::Duration::from_millis(100),
)
.n_messages(20)
.build();
let msg_copy0 = MessageCopy::new();
let msg_copy1 = MessageCopy::new();
let msg_sink = MessageSink::new();
// | indicates message connections
connect!(fg,
msg_source | msg_copy0;
msg_copy0 | msg_copy1 | msg_sink
);
// add a block with no inputs or outputs
let dummy = Dummy::new();
connect!(fg, dummy);
// add a block with space in the port name
let strange = Strange::new();
let snk = NullSink::<u8>::new();
connect!(fg,
strange."foo bar" > snk);
Runtime::new().run(fg)?;
Ok(())
}
pub struct Dummy;
impl Dummy {
#[allow(clippy::new_ret_no_self)]
pub fn new() -> TypedBlock<Self> {
TypedBlock::new(
BlockMetaBuilder::new("Dummy").build(),
StreamIoBuilder::new().build(),
MessageIoBuilder::new().build(),
Self,
)
}
}
#[async_trait]
impl Kernel for Dummy {
async fn work(
&mut self,
io: &mut WorkIo,
_sio: &mut StreamIo,
_mio: &mut MessageIo<Self>,
_meta: &mut BlockMeta,
) -> Result<()> {
io.finished = true;
Ok(())
}
}
pub struct Strange;
impl Strange {
#[allow(clippy::new_ret_no_self)]
pub fn new() -> TypedBlock<Self> {
TypedBlock::new(
BlockMetaBuilder::new("Strange").build(),
StreamIoBuilder::new().add_output::<u8>("foo bar").build(),
MessageIoBuilder::new().build(),
Self,
)
}
}
#[async_trait]
impl Kernel for Strange {
async fn work(
&mut self,
io: &mut WorkIo,
_sio: &mut StreamIo,
_mio: &mut MessageIo<Self>,
_meta: &mut BlockMeta,
) -> Result<()> {
io.finished = true;
Ok(())
}
}
pub struct Handler;
impl Handler {
#[allow(clippy::new_ret_no_self)]
pub fn new() -> TypedBlock<Self> {
TypedBlock::new(
BlockMetaBuilder::new("Handler").build(),
StreamIoBuilder::new().build(),
MessageIoBuilder::new()
.add_input("handler", Self::my_handler)
.add_input("other", Self::my_other_handler)
.build(),
Self,
)
}
#[message_handler]
async fn my_handler(
&mut self,
_io: &mut WorkIo,
_mio: &mut MessageIo<Self>,
_meta: &mut BlockMeta,
_p: Pmt,
) -> Result<Pmt> {
println!("asdf");
Ok(Pmt::Null)
}
#[message_handler]
async fn my_other_handler(
&mut self,
_io: &mut WorkIo,
_mio: &mut MessageIo<Self>,
_meta: &mut BlockMeta,
_p: Pmt,
) -> Result<Pmt> {
Ok(Pmt::U32(0))
}
}
#[async_trait]
impl Kernel for Handler {
async fn work(
&mut self,
io: &mut WorkIo,
_sio: &mut StreamIo,
_mio: &mut MessageIo<Self>,
_meta: &mut BlockMeta,
) -> Result<()> {
io.finished = true;
Ok(())
}
}