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
use std::{
io::Write,
sync::mpsc::{channel, Receiver, Sender},
};
pub enum State<W> {
Writer(W),
Waiting(Receiver<W>),
}
/// A synchronization type to order writes to a writer.
pub struct SequentialWriter<W>
where
W: Write,
{
state: State<W>,
next: Sender<W>,
}
impl<W> SequentialWriter<W>
where
W: Write + Send + Sync,
{
pub fn new(state: State<W>) -> (Self, Receiver<W>) {
let (sender, receiver) = channel();
(
Self {
state,
next: sender,
},
receiver,
)
}
/// # Blocks
///
/// This function blocks while waiting to receive the writer handle. This has the potential to
/// block indefinitely in the case where the `SequentialWriter` is never written to.
///
/// # Panics
///
/// This function should only panic if the previous `Sender` has closed without sending a
/// writer
pub fn send(self, bytes: &[u8]) -> std::io::Result<()> {
let mut writer = match self.state {
State::Writer(w) => w,
State::Waiting(r) => r.recv().expect("Failed to get writer from the receiver"),
};
writer.write_all(bytes)?;
writer.flush()?;
let _ = self.next.send(writer);
Ok(())
}
}