use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
pub struct FrameFuture {
inner: Pin<Box<dyn Future<Output = crate::Result<crate::Frame>> + Send>>,
}
impl FrameFuture {
pub fn new(inner: Pin<Box<dyn Future<Output = crate::Result<crate::Frame>> + Send>>) -> Self {
Self { inner }
}
pub fn from_async_block<F>(future: F) -> Self
where
F: Future<Output = crate::Result<crate::Frame>> + Send + 'static,
{
Self::new(Box::pin(future))
}
pub fn from_frame(frame: crate::Frame) -> Self {
Self::from_result(Ok(frame))
}
pub fn from_result(result: crate::Result<crate::Frame>) -> Self {
Self::from_async_block(async move { result })
}
pub fn from_frame_futures(futures: Vec<FrameFuture>) -> Self {
Self::from_async_block(async move {
let mut frames = Vec::new();
for future in futures {
frames.push(future.await?);
}
Ok(frames.into())
})
}
pub fn empty() -> Self {
Self::from_frame(crate::Frame::default())
}
}
impl Future for FrameFuture {
type Output = crate::Result<crate::Frame>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_mut().inner.as_mut().poll(cx)
}
}