From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox v5 1/2] proxmox: add test/{io, task} modules
Date: Wed, 17 Feb 2021 14:13:20 +0100 [thread overview]
Message-ID: <20210217131322.9129-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20210217131322.9129-1-d.csapak@proxmox.com>
contains:
* AsyncBlocking{Reader,Writer} for dummy async code
by wrapping a 'standard reader/writer'
* poll_result_once for pulling a future once (copied from pxar)
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
proxmox/src/lib.rs | 3 ++
proxmox/src/test/io.rs | 94 ++++++++++++++++++++++++++++++++++++++++
proxmox/src/test/mod.rs | 2 +
proxmox/src/test/task.rs | 32 ++++++++++++++
4 files changed, 131 insertions(+)
create mode 100644 proxmox/src/test/io.rs
create mode 100644 proxmox/src/test/mod.rs
create mode 100644 proxmox/src/test/task.rs
diff --git a/proxmox/src/lib.rs b/proxmox/src/lib.rs
index b74b399..6e95906 100644
--- a/proxmox/src/lib.rs
+++ b/proxmox/src/lib.rs
@@ -8,6 +8,9 @@ pub mod api;
pub mod sys;
pub mod tools;
+#[cfg(test)]
+pub mod test;
+
/// An identity (nop) macro. Used by the `#[sortable]` proc macro.
#[cfg(feature = "sortable-macro")]
#[macro_export]
diff --git a/proxmox/src/test/io.rs b/proxmox/src/test/io.rs
new file mode 100644
index 0000000..919aac1
--- /dev/null
+++ b/proxmox/src/test/io.rs
@@ -0,0 +1,94 @@
+use std::pin::Pin;
+use std::task::{Context, Poll};
+
+use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};
+
+pub struct AsyncBlockingReader<R> {
+ inner: R,
+}
+
+impl<W> AsyncBlockingReader<W> {
+ pub fn new(inner: W) -> Self {
+ Self {
+ inner
+ }
+ }
+
+ pub fn inner(&self) -> &W {
+ &self.inner
+ }
+}
+
+pub struct AsyncBlockingWriter<W> {
+ inner: W,
+ seek_pos: u64,
+}
+
+impl<W> AsyncBlockingWriter<W> {
+ pub fn new(inner: W) -> Self {
+ Self {
+ inner,
+ seek_pos: 0,
+ }
+ }
+
+ pub fn inner(&self) -> &W {
+ &self.inner
+ }
+}
+
+impl<R: std::io::Read + Unpin> AsyncRead for AsyncBlockingReader<R> {
+ fn poll_read(
+ self: Pin<&mut Self>,
+ _cx: &mut Context<'_>,
+ buf: &mut ReadBuf<'_>,
+ ) -> Poll<std::io::Result<()>> {
+ let this = Pin::get_mut(self);
+ let mut read_buf = buf.initialize_unfilled();
+ match this.inner.read(&mut read_buf) {
+ Ok(len) => {
+ buf.advance(len);
+ Poll::Ready(Ok(()))
+ }
+ Err(err) => Poll::Ready(Err(err)),
+ }
+ }
+}
+
+impl<R: std::io::Write + Unpin> AsyncWrite for AsyncBlockingWriter<R> {
+ fn poll_write(
+ self: Pin<&mut Self>,
+ _cx: &mut Context<'_>,
+ buf: &[u8],
+ ) -> Poll<std::io::Result<usize>> {
+ let this = Pin::get_mut(self);
+ match this.inner.write(buf) {
+ Ok(len) => Poll::Ready(Ok(len)),
+ Err(err) => Poll::Ready(Err(err)),
+ }
+ }
+
+ fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
+ Poll::Ready(Ok(()))
+ }
+
+ fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
+ Poll::Ready(Ok(()))
+ }
+}
+
+impl<R: std::io::Seek + Unpin> AsyncSeek for AsyncBlockingWriter<R> {
+ fn start_seek(self: Pin<&mut Self>, position: std::io::SeekFrom) -> std::io::Result<()> {
+ let this = Pin::get_mut(self);
+ this.seek_pos = this.inner.seek(position)?;
+ Ok(())
+ }
+
+ fn poll_complete(
+ self: Pin<&mut Self>,
+ _cx: &mut Context<'_>,
+ ) -> Poll<std::io::Result<u64>> {
+ let this = Pin::get_mut(self);
+ Poll::Ready(Ok(this.seek_pos))
+ }
+}
diff --git a/proxmox/src/test/mod.rs b/proxmox/src/test/mod.rs
new file mode 100644
index 0000000..82ac3e2
--- /dev/null
+++ b/proxmox/src/test/mod.rs
@@ -0,0 +1,2 @@
+pub mod io;
+pub mod task;
diff --git a/proxmox/src/test/task.rs b/proxmox/src/test/task.rs
new file mode 100644
index 0000000..4f5eca6
--- /dev/null
+++ b/proxmox/src/test/task.rs
@@ -0,0 +1,32 @@
+use std::future::Future;
+use std::pin::Pin;
+use std::task::{Context, Poll};
+
+pub fn poll_result_once<T, R>(mut fut: T) -> std::io::Result<R>
+where
+ T: Future<Output = std::io::Result<R>>,
+{
+ let waker = std::task::RawWaker::new(std::ptr::null(), &WAKER_VTABLE);
+ let waker = unsafe { std::task::Waker::from_raw(waker) };
+ let mut cx = Context::from_waker(&waker);
+ unsafe {
+ match Pin::new_unchecked(&mut fut).poll(&mut cx) {
+ Poll::Pending => Err(crate::sys::error::io_err_other(
+ "got Poll::Pending synchronous context",
+ )),
+ Poll::Ready(r) => r,
+ }
+ }
+}
+
+const WAKER_VTABLE: std::task::RawWakerVTable =
+std::task::RawWakerVTable::new(forbid_clone, forbid_wake, forbid_wake, ignore_drop);
+
+unsafe fn forbid_clone(_: *const ()) -> std::task::RawWaker {
+ panic!("tried to clone waker for synchronous task");
+}
+
+unsafe fn forbid_wake(_: *const ()) {
+ panic!("tried to wake synchronous task");
+}
+unsafe fn ignore_drop(_: *const ()) {}
--
2.20.1
next prev parent reply other threads:[~2021-02-17 13:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-17 13:13 [pbs-devel] [PATCH proxmox/proxmox-backup v5] restore files from pxar sparsely Dominik Csapak
2021-02-17 13:13 ` Dominik Csapak [this message]
2021-02-17 13:13 ` [pbs-devel] [PATCH proxmox v5 2/2] proxmox: add sparse_copy(_async) to tools::io Dominik Csapak
2021-02-17 13:13 ` [pbs-devel] [PATCH proxmox-backup v5 1/1] pxar/extract: if possible create files sparesly Dominik Csapak
2021-02-23 14:08 ` [pbs-devel] applied: [PATCH proxmox/proxmox-backup v5] restore files from pxar sparsely Wolfgang Bumiller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210217131322.9129-2-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal