intrepid_core/
future.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5/// A future that resolves to a `Result<Frame>`
6pub struct FrameFuture {
7    inner: Pin<Box<dyn Future<Output = crate::Result<crate::Frame>> + Send>>,
8}
9
10impl FrameFuture {
11    /// Create a new `FrameFuture` from a boxed future.
12    pub fn new(inner: Pin<Box<dyn Future<Output = crate::Result<crate::Frame>> + Send>>) -> Self {
13        Self { inner }
14    }
15
16    /// Create a new `FrameFuture` from a future.
17    pub fn from_async_block<F>(future: F) -> Self
18    where
19        F: Future<Output = crate::Result<crate::Frame>> + Send + 'static,
20    {
21        Self::new(Box::pin(future))
22    }
23
24    /// A convenience method to create a new `FrameFuture` from a `Frame`.
25    pub fn from_frame(frame: crate::Frame) -> Self {
26        Self::from_result(Ok(frame))
27    }
28
29    /// A convenience method to create a new `FrameFuture` from a `Result<Frame>`.
30    pub fn from_result(result: crate::Result<crate::Frame>) -> Self {
31        Self::from_async_block(async move { result })
32    }
33
34    /// A convenience method to create a new `FrameFuture` from a `Vec<FrameFuture>`.
35    pub fn from_frame_futures(futures: Vec<FrameFuture>) -> Self {
36        Self::from_async_block(async move {
37            let mut frames = Vec::new();
38
39            for future in futures {
40                frames.push(future.await?);
41            }
42
43            Ok(frames.into())
44        })
45    }
46
47    /// An empty `FrameFuture`.
48    pub fn empty() -> Self {
49        Self::from_frame(crate::Frame::default())
50    }
51}
52
53impl Future for FrameFuture {
54    type Output = crate::Result<crate::Frame>;
55
56    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
57        self.get_mut().inner.as_mut().poll(cx)
58    }
59}