hala_future/
poll.rs

1use std::{future::IntoFuture, pin::Pin};
2
3use futures::FutureExt;
4
5/// A future will polling a wrapped future once within the current async context.
6pub struct PollOnce<Fut> {
7    fut: Pin<Box<Fut>>,
8}
9
10impl<T> From<T> for PollOnce<T::IntoFuture>
11where
12    T: IntoFuture,
13{
14    fn from(value: T) -> Self {
15        Self::new(value.into_future())
16    }
17}
18
19impl<Fut> PollOnce<Fut> {
20    pub fn new(fut: Fut) -> Self {
21        Self { fut: Box::pin(fut) }
22    }
23}
24
25impl<Fut, R> std::future::Future for PollOnce<Fut>
26where
27    Fut: std::future::Future<Output = R>,
28{
29    type Output = std::task::Poll<R>;
30    fn poll(
31        mut self: std::pin::Pin<&mut Self>,
32        cx: &mut std::task::Context<'_>,
33    ) -> std::task::Poll<Self::Output> {
34        std::task::Poll::Ready(self.fut.poll_unpin(cx))
35    }
36}
37
38#[macro_export]
39macro_rules! poll_once {
40    ($fut: expr) => {
41        $crate::poll::PollOnce::new($fut).await
42    };
43}