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
// Copyright 2023 David Weikersdorfer
use core::marker::PhantomData;
use nodo::prelude::*;
/// A codelet which converts messages using the Into trait.
pub struct Convert<T, S> {
marker: PhantomData<(T, S)>,
}
impl<T, S> Default for Convert<T, S> {
fn default() -> Self {
Convert {
marker: PhantomData,
}
}
}
impl<T, S> Codelet for Convert<T, S>
where
T: Send + Sync,
S: Clone + Send + Sync + From<T>,
{
type Status = DefaultStatus;
type Config = ();
type Rx = DoubleBufferRx<T>;
type Tx = DoubleBufferTx<S>;
type Signals = ();
fn build_bundles(_: &Self::Config) -> (Self::Rx, Self::Tx) {
(
DoubleBufferRx::new_auto_size(),
DoubleBufferTx::new_auto_size(),
)
}
fn step(&mut self, _: Context<Self>, rx: &mut Self::Rx, tx: &mut Self::Tx) -> Outcome {
while let Some(msg) = rx.try_pop() {
tx.push(msg.into())?;
}
SUCCESS
}
}
#[cfg(test)]
mod tests {
// FIXME This test currently does not terminate. Termination needs to be properly implemented.
// use crate::Convert;
// use crate::Sink;
// use crate::Source;
// use nodo::codelet::ScheduleBuilder;
// use nodo::prelude::*;
// use nodo::runtime::Runtime;
// #[test]
// fn test_convert() {
// #[derive(Debug, Clone)]
// struct Foo {
// number: u32,
// }
// #[derive(Debug, Clone)]
// struct Bar {
// text: String,
// }
// impl From<Foo> for Bar {
// fn from(foo: Foo) -> Bar {
// Bar {
// text: format!("{}", foo.number),
// }
// }
// }
// let mut rt = Runtime::new();
// let mut source = Source::new(|| Foo { number: 42 }).into_instance("source", ());
// let mut sink = Sink::new(|bar: Bar| {
// assert_eq!(bar.text, "42");
// SUCCESS
// })
// .into_instance("sink", ());
// let mut into = Convert::instantiate("into", ());
// source.tx.connect(&mut into.rx).unwrap();
// into.tx.connect(&mut sink.rx).unwrap();
// rt.add_codelet_schedule(
// ScheduleBuilder::new()
// .with_max_step_count(10)
// .with(source)
// .with(into)
// .with(sink)
// .into(),
// );
// rt.spin();
// }
}