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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#![cfg_attr(rustfmt, rustfmt::skip)]
//! See [the dedicated secion of the guide](https://getditto.github.io/safer_ffi/dyn_traits/futures.html).

use {
    ::core::{
        future::Future,
        pin::Pin,
        task::{Context, Poll},
    },
    ::safer_ffi::{
        prelude::*,
    },
    super::{
        *,
    },
};

/// An FFI-safe `Poll<()>`.
#[derive_ReprC]
#[repr(i8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub
enum PollFuture {
    Completed = 0,
    Pending = -1,
}

/// Models a `Future` resolving to `()`.
#[derive_ReprC(dyn)]
pub
trait FfiFuture {
    fn dyn_poll (self: Pin<&mut Self>, ctx: &'_ mut Context<'_>)
      -> PollFuture
    ;
}

impl<F : Future<Output = ()>> FfiFuture for F {
    fn dyn_poll (self: Pin<&mut Self>, ctx: &'_ mut Context<'_>)
      -> PollFuture
    {
        match Future::poll(self, ctx) {
            | Poll::Pending => PollFuture::Pending,
            | Poll::Ready(()) => PollFuture::Completed,
        }
    }
}

match_! {([] [Send]) {( $([ $($Send:ident)? ])* ) => (
    $(
        impl VirtualPtr<dyn '_ + $($Send +)? FfiFuture> {
            pub
            async fn into_future (mut self)
            {
                ::futures::future::poll_fn(
                    move |cx| match Pin::new(&mut self).dyn_poll(cx) {
                        | PollFuture::Pending => Poll::Pending,
                        | PollFuture::Completed => Poll::Ready(()),
                    }
                )
                .await
            }
        }
    )*
)}}

pub use executor::*;
mod executor;

#[cfg(test)]
mod tests;