forked from proxmox-mirrors/proxmox
proxmox-async: imported pbs-tools/src/tokio/tokio_writer_adapter.rs
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
parent
743f7df2a5
commit
112b239d50
@ -1,5 +1,7 @@
|
||||
rust-proxmox-async (0.1.0) stable; urgency=medium
|
||||
|
||||
* imported pbs-tools/src/tokio/tokio_writer_adapter.rs
|
||||
|
||||
* imported pbs-tools/src/stream.rs
|
||||
|
||||
* imported pbs-tools/src/broadcast_future.rs
|
||||
|
@ -2,3 +2,4 @@ pub mod blocking;
|
||||
pub mod broadcast_future;
|
||||
pub mod runtime;
|
||||
pub mod stream;
|
||||
pub mod tokio_writer_adapter;
|
||||
|
26
proxmox-async/src/tokio_writer_adapter.rs
Normal file
26
proxmox-async/src/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())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user