From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id C632A695B7
 for <pbs-devel@lists.proxmox.com>; Fri, 12 Feb 2021 15:44:35 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id C46CC2810A
 for <pbs-devel@lists.proxmox.com>; Fri, 12 Feb 2021 15:44:35 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 06DD0280F1
 for <pbs-devel@lists.proxmox.com>; Fri, 12 Feb 2021 15:44:35 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id C44984625A
 for <pbs-devel@lists.proxmox.com>; Fri, 12 Feb 2021 15:44:34 +0100 (CET)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Fri, 12 Feb 2021 15:44:31 +0100
Message-Id: <20210212144433.30668-2-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210212144433.30668-1-d.csapak@proxmox.com>
References: <20210212144433.30668-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.226 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [mod.rs]
Subject: [pbs-devel] [PATCH proxmox v4 1/2] proxmox/tools: add poll_once
 module for testing
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 12 Feb 2021 14:44:35 -0000

copied from the pxar crate, intended for polling a future once
this is helpful for testing async code
(this needs to resolve in the first poll otherwise it will
not work)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox/src/tools/mod.rs | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/proxmox/src/tools/mod.rs b/proxmox/src/tools/mod.rs
index 45f46f9..e938d90 100644
--- a/proxmox/src/tools/mod.rs
+++ b/proxmox/src/tools/mod.rs
@@ -227,3 +227,39 @@ pub fn nodename() -> &'static str {
 
     &NODENAME
 }
+
+#[cfg(test)]
+pub mod poll_once {
+    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