From 112b239d506f9ea3c4f21f6a2a2c0cacea33c4b8 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 19 Nov 2021 16:16:20 +0100 Subject: [PATCH] proxmox-async: imported pbs-tools/src/tokio/tokio_writer_adapter.rs Signed-off-by: Dietmar Maurer --- proxmox-async/debian/changelog | 2 ++ proxmox-async/src/lib.rs | 1 + proxmox-async/src/tokio_writer_adapter.rs | 26 +++++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 proxmox-async/src/tokio_writer_adapter.rs diff --git a/proxmox-async/debian/changelog b/proxmox-async/debian/changelog index d3560e3d..74dcddb8 100644 --- a/proxmox-async/debian/changelog +++ b/proxmox-async/debian/changelog @@ -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 diff --git a/proxmox-async/src/lib.rs b/proxmox-async/src/lib.rs index 2e95bcca..7eeffa60 100644 --- a/proxmox-async/src/lib.rs +++ b/proxmox-async/src/lib.rs @@ -2,3 +2,4 @@ pub mod blocking; pub mod broadcast_future; pub mod runtime; pub mod stream; +pub mod tokio_writer_adapter; diff --git a/proxmox-async/src/tokio_writer_adapter.rs b/proxmox-async/src/tokio_writer_adapter.rs new file mode 100644 index 00000000..7b7f5dcf --- /dev/null +++ b/proxmox-async/src/tokio_writer_adapter.rs @@ -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); + +impl TokioWriterAdapter { + pub fn new(writer: W) -> Self { + Self(writer) + } +} + +impl Write for TokioWriterAdapter { + fn write(&mut self, buf: &[u8]) -> Result { + block_in_place(|| self.0.write(buf)) + } + + fn flush(&mut self) -> Result<(), std::io::Error> { + block_in_place(|| self.0.flush()) + } +}