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
use crate::{error::Result, protobuf::Example, record::Record};
use std::{
fs::File,
io::{BufWriter, Write},
marker::PhantomData,
path::Path,
};
pub type BytesWriter<W> = RecordWriter<Vec<u8>, W>;
pub type ExampleWriter<W> = RecordWriter<Example, W>;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RecordWriter<T, W>
where
T: Record,
{
writer: W,
_phantom: PhantomData<T>,
}
impl<T> RecordWriter<T, BufWriter<File>>
where
T: Record,
{
pub fn create<P>(path: P) -> Result<Self>
where
P: AsRef<Path>,
{
let writer = BufWriter::new(File::create(path)?);
Self::from_writer(writer)
}
}
impl<T, W> RecordWriter<T, W>
where
T: Record,
W: Write,
{
pub fn from_writer(writer: W) -> Result<Self> {
Ok(Self {
writer,
_phantom: PhantomData,
})
}
pub fn send(&mut self, record: T) -> Result<()> {
let bytes = T::to_bytes(record)?;
crate::io::sync::try_write_record(&mut self.writer, bytes)?;
Ok(())
}
pub fn flush(&mut self) -> Result<()> {
self.writer.flush()?;
Ok(())
}
}