move more tools for the client into subcrates
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
2
pbs-tools/src/tokio/mod.rs
Normal file
2
pbs-tools/src/tokio/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod tokio_writer_adapter;
|
||||
pub use tokio_writer_adapter::TokioWriterAdapter;
|
26
pbs-tools/src/tokio/tokio_writer_adapter.rs
Normal file
26
pbs-tools/src/tokio/tokio_writer_adapter.rs
Normal file
@ -0,0 +1,26 @@
|
||||
use std::io::Write;
|
||||
|
||||
use tokio::task::block_in_place;
|
||||
|
||||
/// Wrapper around a writer which implements Write
|
||||
///
|
||||
/// wraps each write with a 'block_in_place' so that
|
||||
/// any (blocking) writer can be safely used in async context in a
|
||||
/// tokio runtime
|
||||
pub struct TokioWriterAdapter<W: Write>(W);
|
||||
|
||||
impl<W: Write> TokioWriterAdapter<W> {
|
||||
pub fn new(writer: W) -> Self {
|
||||
Self(writer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> Write for TokioWriterAdapter<W> {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
|
||||
block_in_place(|| self.0.write(buf))
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), std::io::Error> {
|
||||
block_in_place(|| self.0.flush())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user