netstack_smoltcp/
runner.rs

1use std::{
2    future::{Future, IntoFuture},
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7/// BoxFuture acts the same as the [BoxFuture in crate futures utils],
8/// which is an owned dynamically typed Future for use in cases where you
9/// can’t statically type your result or need to add some indirection.
10/// But the difference of this structure is that it will conditionally
11/// implement Send according to the properties of type T, which does not
12/// require two sets of API interfaces in single-threaded and multi-threaded.
13///
14/// [BoxFuture in crate futures utils]: https://docs.rs/futures-util/latest/futures_util/future/type.BoxFuture.html
15pub struct BoxFuture<'a, T>(Pin<Box<dyn Future<Output = T> + 'a>>);
16
17impl<'a, T> BoxFuture<'a, T> {
18    pub fn new<F>(f: F) -> BoxFuture<'a, T>
19    where
20        F: IntoFuture<Output = T> + 'a,
21    {
22        BoxFuture(Box::pin(f.into_future()))
23    }
24
25    #[allow(unused)]
26    pub fn wrap(f: Pin<Box<dyn Future<Output = T> + 'a>>) -> BoxFuture<'a, T> {
27        BoxFuture(f)
28    }
29}
30
31unsafe impl<T: Send> Send for BoxFuture<'_, T> {}
32
33impl<T> Future for BoxFuture<'_, T> {
34    type Output = T;
35
36    fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
37        self.0.as_mut().poll(context)
38    }
39}
40
41pub type Runner = BoxFuture<'static, std::io::Result<()>>;