]> git.proxmox.com Git - proxmox-backup.git/blame - src/tools/tokio_writer_adapter.rs
move more helpers to pbs-tools
[proxmox-backup.git] / src / tools / tokio_writer_adapter.rs
CommitLineData
f1d76ecf
DC
1use std::io::Write;
2
3use 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
10pub struct TokioWriterAdapter<W: Write>(W);
11
12impl<W: Write> TokioWriterAdapter<W> {
13 pub fn new(writer: W) -> Self {
14 Self(writer)
15 }
16}
17
18impl<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}