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
use futures::AsyncReadExt;
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;
#[cfg_attr(docsrs, doc(cfg(not(target_arch = "wasm32"))))]
pub struct FileSource<T: Send + 'static> {
file_name: String,
file: Option<async_fs::File>,
repeat: bool,
_type: std::marker::PhantomData<T>,
}
impl<T: Send + 'static> FileSource<T> {
pub fn new<S: Into<String>>(file_name: S, repeat: bool) -> Block {
Block::new(
BlockMetaBuilder::new("FileSource").build(),
StreamIoBuilder::new().add_output::<T>("out").build(),
MessageIoBuilder::new().build(),
FileSource::<T> {
file_name: file_name.into(),
file: None,
repeat,
_type: std::marker::PhantomData,
},
)
}
}
#[doc(hidden)]
#[async_trait]
impl<T: Send + 'static> Kernel for FileSource<T> {
async fn work(
&mut self,
io: &mut WorkIo,
sio: &mut StreamIo,
_mio: &mut MessageIo<Self>,
_meta: &mut BlockMeta,
) -> Result<()> {
let out = sio.output(0).slice_unchecked::<u8>();
let item_size = std::mem::size_of::<T>();
debug_assert_eq!(out.len() % item_size, 0);
let mut i = 0;
while i < out.len() {
match self.file.as_mut().unwrap().read(&mut out[i..]).await {
Ok(0) => {
if self.repeat {
self.file =
Some(async_fs::File::open(self.file_name.clone()).await.unwrap());
} else {
io.finished = true;
break;
}
}
Ok(written) => {
i += written;
}
Err(e) => panic!("FileSource: Error reading from file: {e:?}"),
}
}
sio.output(0).produce(i / item_size);
Ok(())
}
async fn init(
&mut self,
_sio: &mut StreamIo,
_mio: &mut MessageIo<Self>,
_meta: &mut BlockMeta,
) -> Result<()> {
self.file = Some(async_fs::File::open(self.file_name.clone()).await.unwrap());
Ok(())
}
}