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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use core::{
    marker::PhantomData,
    pin::pin,
    ptr,
    task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};

use crate::{higher_ranked_trait, higher_ranked_type};

higher_ranked_trait! {
    pub type class SendFuture['ctx, Output]: [for<'lt> core::future::Future<Output = Output> + Send + 'lt]
    where {
        Output: Send,
    }
    for where {
        Output: 'lt,
    }
}

/// Trait for effects.
pub trait Effect<'ctx>: 'static {
    type Future<T: Send>: SendFuture::Trait<'ctx, T>;

    fn wrap<'a, F>(future: F) -> SendFuture::T<'a, 'ctx, Self::Future<F::Output>, F::Output>
    where
        F: core::future::Future + Send + 'a,
        <F as core::future::Future>::Output: Send;

    fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, 'ctx, Self::Future<T>, T>;

    #[cfg(feature = "alloc")]
    #[inline]
    fn wrap_boxed<'a, F>(
        future: core::pin::Pin<Box<F>>,
    ) -> SendFuture::T<'a, 'ctx, Self::Future<F::Output>, F::Output>
    where
        F: core::future::Future + Send + 'a,
        <F as core::future::Future>::Output: Send,
    {
        Self::wrap(future)
    }
}

pub type Future<'a, 'ctx, T, E> = SendFuture::T<'a, 'ctx, <E as Effect<'ctx>>::Future<T>, T>;

pub struct Blocking<B = Spin> {
    _marker: PhantomData<fn() -> B>,
}

pub trait BlockOn: 'static {
    fn block_on<F>(future: F) -> F::Output
    where
        F: core::future::Future + Send,
        <F as core::future::Future>::Output: Send;
}

/// [`BlockOn`] implementer that just spins on the future.
///
/// This is useful for futures that are alwayd ready.
pub enum Spin {}

impl BlockOn for Spin {
    #[inline(always)]
    fn block_on<F>(future: F) -> F::Output
    where
        F: core::future::Future + Send,
    {
        let waker = noop();
        let mut context = Context::from_waker(&waker);

        let mut future = pin!(future);
        loop {
            if let Poll::Ready(value) = future.as_mut().poll(&mut context) {
                return value;
            }
        }
    }
}

#[inline]
pub fn noop() -> Waker {
    const VTABLE: &'static RawWakerVTable = &RawWakerVTable::new(
        // Cloning just returns a new no-op raw waker
        |_| RAW,
        // `wake` does nothing
        |_| {},
        // `wake_by_ref` does nothing
        |_| {},
        // Dropping does nothing as we don't allocate anything
        |_| {},
    );
    const RAW: RawWaker = RawWaker::new(ptr::null(), &VTABLE);
    unsafe { Waker::from_raw(RAW) }
}

higher_ranked_type! {
    pub type ReadyFuture['ctx, Output]: (SendFuture)[Output]
    where {
        Output: Send,
    } = for<'lt> core::future::Ready<Output>
}

impl<'ctx, B: BlockOn> Effect<'ctx> for Blocking<B> {
    type Future<T: Send> = ReadyFuture<'ctx, T>;

    fn wrap<'a, F>(future: F) -> SendFuture::T<'a, 'ctx, Self::Future<F::Output>, F::Output>
    where
        F: core::future::Future + Send + 'a,
        <F as core::future::Future>::Output: Send,
    {
        core::future::ready(B::block_on(future))
    }

    fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, 'ctx, Self::Future<T>, T> {
        core::future::ready(value)
    }
}

mod sealed {
    pub enum BoxedFuture<'lt, Output> {
        Box(core::pin::Pin<Box<dyn core::future::Future<Output = Output> + Send + 'lt>>),
        Ready(core::future::Ready<Output>),
    }

    impl<'lt, Output> core::future::Future for BoxedFuture<'lt, Output> {
        type Output = Output;

        fn poll(
            mut self: core::pin::Pin<&mut Self>,
            cx: &mut core::task::Context<'_>,
        ) -> core::task::Poll<Self::Output> {
            match &mut *self {
                BoxedFuture::Box(future) => future.as_mut().poll(cx),
                BoxedFuture::Ready(future) => core::pin::Pin::new(future).poll(cx),
            }
        }
    }
}

#[cfg(feature = "alloc")]
higher_ranked_type! {
    pub type BoxFuture['ctx, Output]: (SendFuture)[Output]
    where {
        Output: Send,
    } = for<'lt> sealed::BoxedFuture<'lt, Output>
}

#[cfg(feature = "alloc")]
pub enum Async {}

#[cfg(feature = "alloc")]
impl<'ctx> Effect<'ctx> for Async {
    type Future<T: Send> = BoxFuture<'ctx, T>;

    fn wrap<'a, F>(future: F) -> SendFuture::T<'a, 'ctx, Self::Future<F::Output>, F::Output>
    where
        F: core::future::Future + Send + 'a,
        <F as core::future::Future>::Output: Send,
    {
        sealed::BoxedFuture::Box(Box::pin(future))
    }

    fn wrap_boxed<'a, F>(
        future: core::pin::Pin<Box<F>>,
    ) -> SendFuture::T<'a, 'ctx, Self::Future<F::Output>, F::Output>
    where
        F: core::future::Future + Send + 'a,
        <F as core::future::Future>::Output: Send,
    {
        sealed::BoxedFuture::Box(future)
    }

    fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, 'ctx, Self::Future<T>, T> {
        sealed::BoxedFuture::Ready(core::future::ready(value))
    }
}