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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! This module contains tools for dealing with async functions. These are similar to the
//! `FnOnce` trait from `std`, but they allow the lifetime of the returned future to be
//! bound by the lifetime of the argument. The different variants correspond to the
//! different reference types (`&T`, `&mut T`) but all variants consume `self` like `FnOnce`.
//!
//! For simplicity, only one argument is allowed. In addition, the returned future and its
//! output are assumed to be `Send + 'static`.
//!
//! Note: typically you will not need to use these tools. The exist primarily for use by
//! the expanded macro code from `#[act_zero]`.

use std::future::Future;
use std::marker::PhantomData;

use futures::channel::oneshot;
use futures::future::BoxFuture;
use futures::FutureExt;

/// Trait for async methods which take `&T` as the argument type.
pub trait AsyncFnOnce<T> {
    /// Output type of the returned future.
    type Output: Send + 'static;
    /// Call this function.
    fn call(self, arg: &T) -> BoxFuture<Self::Output>;
    /// Call this function when `self` is boxed.
    fn call_boxed(self: Box<Self>, arg: &T) -> BoxFuture<Self::Output>;
    /// Similar to `FutureExt::map`, except the callback also has access to the argument.
    fn map<G, R>(self, g: G) -> AsyncMap<Self, G>
    where
        G: FnOnce(Self::Output, &T) -> R,
        Self: Sized,
    {
        AsyncMap { fun: self, g }
    }
    /// Bind the output of this async function to this oneshot channel: when the future
    /// completes, the output will be sent on the channel.
    fn bind_output(self, tx: oneshot::Sender<Self::Output>) -> BindOutput<Self, Self::Output>
    where
        Self: Sized,
    {
        BindOutput { fun: self, tx }
    }
}

/// Trait for async methods which take `&mut T` as the argument type.
pub trait AsyncMutFnOnce<T> {
    /// Output type of the returned future.
    type Output: Send + 'static;
    /// Call this function.
    fn call(self, arg: &mut T) -> BoxFuture<Self::Output>;
    /// Call this function when `self` is boxed.
    fn call_boxed(self: Box<Self>, arg: &mut T) -> BoxFuture<Self::Output>;
    /// Similar to `FutureExt::map`, except the callback also has access to the argument.
    fn map<G, R>(self, g: G) -> AsyncMap<Self, G>
    where
        G: FnOnce(Self::Output, &mut T) -> R,
        Self: Sized,
    {
        AsyncMap { fun: self, g }
    }
    /// Bind the output of this async function to this oneshot channel: when the future
    /// completes, the output will be sent on the channel.
    fn bind_output(self, tx: oneshot::Sender<Self::Output>) -> BindOutput<Self, Self::Output>
    where
        Self: Sized,
    {
        BindOutput { fun: self, tx }
    }
}

impl<T, F> AsyncFnOnce<T> for Box<F>
where
    F: AsyncFnOnce<T> + ?Sized,
{
    type Output = F::Output;
    fn call(self, arg: &T) -> BoxFuture<Self::Output> {
        self.call_boxed(arg)
    }
    fn call_boxed(self: Box<Self>, arg: &T) -> BoxFuture<Self::Output> {
        (*self).call(arg)
    }
}

impl<T, F> AsyncMutFnOnce<T> for Box<F>
where
    F: AsyncMutFnOnce<T>,
{
    type Output = F::Output;
    fn call(self, arg: &mut T) -> BoxFuture<Self::Output> {
        self.call_boxed(arg)
    }
    fn call_boxed(self: Box<Self>, arg: &mut T) -> BoxFuture<Self::Output> {
        (*self).call(arg)
    }
}

/// Return type of `AsyncFnOnce::map` and `AsyncMutFnOnce::map`.
pub struct AsyncMap<F, G> {
    fun: F,
    g: G,
}

impl<F, G, T, R> AsyncFnOnce<T> for AsyncMap<F, G>
where
    F: AsyncFnOnce<T>,
    G: FnOnce(F::Output, &T) -> R + Send + 'static,
    R: Send + 'static,
    T: Sync,
{
    type Output = R;
    fn call(self, arg: &T) -> BoxFuture<R> {
        let AsyncMap { fun, g } = self;
        let fut = fun.call(arg);
        async move {
            let res = fut.await;
            g(res, arg)
        }
        .boxed()
    }
    fn call_boxed(self: Box<Self>, arg: &T) -> BoxFuture<Self::Output> {
        (*self).call(arg)
    }
}

impl<F, G, T, R> AsyncMutFnOnce<T> for AsyncMap<F, G>
where
    F: AsyncMutFnOnce<T> + Send + 'static,
    G: FnOnce(F::Output, &mut T) -> R + Send + 'static,
    R: Send + 'static,
    T: Send,
{
    type Output = R;
    fn call(self, arg: &mut T) -> BoxFuture<R> {
        let AsyncMap { fun, g } = self;
        async move {
            let fut = fun.call(arg);
            let res = fut.await;
            g(res, arg)
        }
        .boxed()
    }
    fn call_boxed(self: Box<Self>, arg: &mut T) -> BoxFuture<Self::Output> {
        (*self).call(arg)
    }
}

/// Return type of `AsyncFnOnce::bind_output` and `AsyncMutFnOnce::bind_output`.
pub struct BindOutput<F, R> {
    fun: F,
    tx: oneshot::Sender<R>,
}

impl<F, T, R> AsyncFnOnce<T> for BindOutput<F, R>
where
    F: AsyncFnOnce<T, Output = R>,
    R: Send + 'static,
{
    type Output = ();
    fn call(self, arg: &T) -> BoxFuture<()> {
        let BindOutput { fun, tx } = self;
        fun.call(arg)
            .map(move |res| {
                tx.send(res).ok();
            })
            .boxed()
    }
    fn call_boxed(self: Box<Self>, arg: &T) -> BoxFuture<Self::Output> {
        (*self).call(arg)
    }
}

impl<F, T, R> AsyncMutFnOnce<T> for BindOutput<F, R>
where
    F: AsyncMutFnOnce<T, Output = R>,
    R: Send + 'static,
{
    type Output = ();
    fn call(self, arg: &mut T) -> BoxFuture<()> {
        let BindOutput { fun, tx } = self;
        fun.call(arg)
            .map(move |res| {
                tx.send(res).ok();
            })
            .boxed()
    }
    fn call_boxed(self: Box<Self>, arg: &mut T) -> BoxFuture<Self::Output> {
        (*self).call(arg)
    }
}

/// Rust does not support async closures, and is incapable of inferring the correct lifetimes
/// for closures returning an `async move { ... }` block which captures a non-'static argument
/// to the closure.
/// To solve this problem, we introduce this explicit closure type, which takes a stand-alone
/// `async fn` expecting two arguments, and binds the second argument to a closure "upvar"
/// passed in at construction.
pub struct Closure<R, F, P> {
    fun: F,
    upvar: P,
    phantom: PhantomData<fn() -> R>,
}

impl<R, F, P> Closure<R, F, P> {
    /// Constructor.
    /// `fun` should implement `ClosureFn` or `ClosureFnMut` for this to be useful.
    pub fn new(fun: F, upvar: P) -> Self {
        Self {
            fun,
            upvar,
            phantom: PhantomData,
        }
    }
}

/// We can't even directly express the lifetime bounds of an `async fn` directly, so we
/// are forced to introduce a trait just to write the required bounds.
/// This trait is for the case when the first argument is `&T`.
pub trait ClosureFn<'a, T, P, R> {
    /// Type of the future returned by this async fn.
    type Future: Future<Output = R> + Send + 'a;
    /// Call this async fn with two arguments.
    fn call_closure(self, arg1: &'a T, arg2: P) -> Self::Future;
}

/// We can't even directly express the lifetime bounds of an `async fn` directly, so we
/// are forced to introduce a trait just to write the required bounds.
/// This trait is for the case when the first argument is `&mut T`.
pub trait ClosureFnMut<'a, T, P, R> {
    /// Type of the future returned by this async fn.
    type Future: Future<Output = R> + Send + 'a;
    /// Call this async fn with two arguments.
    fn call_closure(self, arg1: &'a mut T, arg2: P) -> Self::Future;
}

impl<'a, F, T, P, Fut, R> ClosureFn<'a, T, P, R> for F
where
    Fut: Future<Output = R> + Send + 'a,
    T: 'a,
    F: FnOnce(&'a T, P) -> Fut,
{
    type Future = Fut;
    fn call_closure(self, arg1: &'a T, arg2: P) -> Self::Future {
        self(arg1, arg2)
    }
}

impl<'a, F, T, P, Fut, R> ClosureFnMut<'a, T, P, R> for F
where
    Fut: Future<Output = R> + Send + 'a,
    T: 'a,
    F: FnOnce(&'a mut T, P) -> Fut,
{
    type Future = Fut;
    fn call_closure(self, arg1: &'a mut T, arg2: P) -> Self::Future {
        self(arg1, arg2)
    }
}

impl<F, T, P, R> AsyncFnOnce<T> for Closure<R, F, P>
where
    F: for<'a> ClosureFn<'a, T, P, R>,
    R: Send + 'static,
{
    type Output = R;
    fn call(self, arg: &T) -> BoxFuture<R> {
        let Closure { fun, upvar, .. } = self;
        fun.call_closure(arg, upvar).boxed()
    }
    fn call_boxed(self: Box<Self>, arg: &T) -> BoxFuture<Self::Output> {
        (*self).call(arg)
    }
}

impl<F, P, T, R> AsyncMutFnOnce<T> for Closure<R, F, P>
where
    F: for<'a> ClosureFnMut<'a, T, P, R>,
    R: Send + 'static,
{
    type Output = R;
    fn call(self, arg: &mut T) -> BoxFuture<R> {
        let Closure { fun, upvar, .. } = self;
        fun.call_closure(arg, upvar).boxed()
    }
    fn call_boxed(self: Box<Self>, arg: &mut T) -> BoxFuture<Self::Output> {
        (*self).call(arg)
    }
}