]>
Commit | Line | Data |
---|---|---|
f1d76ecf DC |
1 | use std::io::Write; |
2 | ||
3 | use tokio::task::block_in_place; | |
4 | ||
5 | /// Wrapper around a writer which implements Write | |
6 | /// | |
7 | /// wraps each write with a 'block_in_place' so that | |
8 | /// any (blocking) writer can be safely used in async context in a | |
9 | /// tokio runtime | |
10 | pub struct TokioWriterAdapter<W: Write>(W); | |
11 | ||
12 | impl<W: Write> TokioWriterAdapter<W> { | |
13 | pub fn new(writer: W) -> Self { | |
14 | Self(writer) | |
15 | } | |
16 | } | |
17 | ||
18 | impl<W: Write> Write for TokioWriterAdapter<W> { | |
19 | fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> { | |
20 | block_in_place(|| self.0.write(buf)) | |
21 | } | |
22 | ||
23 | fn flush(&mut self) -> Result<(), std::io::Error> { | |
24 | block_in_place(|| self.0.flush()) | |
25 | } | |
26 | } |