1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#![forbid(unsafe_code)]

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

/// Represents an asyncronous computation.
///
/// This is different from `std::future::Future` in that it
/// is a fixed size struct with a boxed future inside.
pub struct DynFuture<T> {
    inner: Pin<Box<dyn Future<Output = T> + Send>>,
}

impl<T> DynFuture<T> {
    /// Creates a new `DynFuture` from a `std::future::Future`.
    pub fn new(f: impl Future<Output = T> + Send + 'static) -> Self {
        Self { inner: Box::pin(f) }
    }
}

impl<T> Future for DynFuture<T> {
    type Output = T;

    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        self.get_mut().inner.as_mut().poll(cx)
    }
}