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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! A scoped [`tokio`] Runtime that can be used to create [`Scope`]s which can spawn futures which
//! can access stack data. That is, the futures spawned by the [`Scope`] do not require the `'static`
//! lifetime bound. This can be done safely by ensuring that the [`Scope`] doesn't exit until all
//! spawned futures have finished executing. Be aware, that when a [`Scope`] exits it will block
//! until every future spawned by the [`Scope`] completes. Therefore, one should take caution when
//! created scopes within an asynchronous context, such as from within another spawned future.
//!
//! # Example
//! ```
//! # extern crate tokio_scoped;
//! # extern crate futures;
//! # use futures::lazy;
//!
//! let mut v = String::from("Hello");
//! tokio_scoped::scope(|scope| {
//!     // Use the scope to spawn the future.
//!     scope.spawn(lazy(|| {
//!         v.push('!');
//!         Ok(())
//!     }));
//! });
//! // The scope won't exit until all spawned futures are complete.
//! assert_eq!(v.as_str(), "Hello!");
//! ```
//!
//! See also [`crossbeam::scope`]
//!
//! [`tokio`]: https://tokio.rs/
//! [`crossbeam::scope`]: https://docs.rs/crossbeam/0.4.1/crossbeam/fn.scope.html
extern crate futures;
extern crate tokio;

use futures::sync::mpsc;
use futures::{Future, Stream};
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::ops::Deref;
use tokio::runtime::TaskExecutor;

/// Creates a [`ScopedRuntime`] and calls the `scope` method with `f` on it.
///
/// # Example
/// ```
/// # extern crate tokio_scoped;
/// # extern crate futures;
/// # use futures::lazy;
///
/// let mut v = String::from("Hello");
/// tokio_scoped::scope(|scope| {
///     // Use the scope to spawn the future.
///     scope.spawn(lazy(|| {
///         v.push('!');
///         Ok(())
///     }));
/// });
/// // The scope won't exit until all spawned futures are complete.
/// assert_eq!(v.as_str(), "Hello!");
/// ```
pub fn scope<'a, F, R>(f: F) -> R
where
    F: FnOnce(&mut Scope<'a>) -> R,
{
    let rt = tokio::runtime::Runtime::new().unwrap();
    let mut scope = Scope::new(rt.executor());
    f(&mut scope)
}

/// Borrows the Runtime to construct a `ScopeBuilder` which can be used to create a scope.
///
/// # Example
/// ```
/// # extern crate tokio_scoped;
/// # extern crate futures;
/// # extern crate tokio;
/// # use futures::lazy;
///
/// let mut v = String::from("Hello");
/// let rt = tokio::runtime::Runtime::new().unwrap();
/// tokio_scoped::scoped(&rt).scope(|scope| {
///     // Use the scope to spawn the future.
///     scope.spawn(lazy(|| {
///         v.push('!');
///         Ok(())
///     }));
/// });
/// // The scope won't exit until all spawned futures are complete.
/// assert_eq!(v.as_str(), "Hello!");
/// ```
pub fn scoped(rt: &tokio::runtime::Runtime) -> ScopeBuilder<'_> {
    ScopeBuilder::from_runtime(rt)
}

/// Wrapper type around a tokio Runtime which can be used to create `Scope`s. This type takes
/// ownership of the Runtime. For an alternative approach that
///
/// See also the [`scope`] function.
///
/// [`scope`]: /tokio-scoped/fn.scope.html
#[derive(Debug)]
pub struct ScopedRuntime {
    rt: tokio::runtime::Runtime,
}

/// Struct used to build scopes from a borrowed Runtime. Generally users should use the [`scoped`]
/// function instead of building `ScopeBuilder` instances directly.
///
/// [`scoped`]: /tokio-scoped/fn.scoped.html
#[derive(Debug)]
pub struct ScopeBuilder<'a> {
    rt: &'a tokio::runtime::Runtime,
}

#[derive(Debug)]
pub struct Scope<'a> {
    exec: TaskExecutor,
    send: ManuallyDrop<mpsc::Sender<()>>,
    // When the `Scope` is dropped, we wait on this receiver to close. No messages are sent through
    // the receiver, however, the `Sender` objects get cloned into each spawned future (see
    // `ScopedFuture`). This is how we ensure they all exit eventually.
    recv: Option<mpsc::Receiver<()>>,
    _marker: PhantomData<&'a ()>,
}

impl<'a> Scope<'a> {
    fn new(exec: TaskExecutor) -> Scope<'a> {
        let (s, r) = mpsc::channel(0);
        Scope {
            exec,
            send: ManuallyDrop::new(s),
            recv: Some(r),
            _marker: PhantomData,
        }
    }
}

impl ScopedRuntime {
    pub fn new(rt: tokio::runtime::Runtime) -> Self {
        ScopedRuntime { rt }
    }

    /// Creates a scope bound by the lifetime of `self` that can be used to spawn scoped futures.
    pub fn scope<'a, F, R>(&'a self, f: F) -> R
    where
        F: FnOnce(&mut Scope<'a>) -> R,
    {
        let mut scope = Scope::new(self.rt.executor());
        f(&mut scope)
    }

    /// Consumes the `ScopedRuntime` and returns the inner [`Runtime`] variable.
    ///
    /// [`Runtime`]: https://docs.rs/tokio/0.1.8/tokio/runtime/struct.Runtime.html
    pub fn into_inner(self) -> tokio::runtime::Runtime {
        self.rt
    }
}

impl<'a> ScopeBuilder<'a> {
    pub fn from_runtime(rt: &'a tokio::runtime::Runtime) -> ScopeBuilder<'a> {
        ScopeBuilder { rt }
    }

    pub fn scope<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut Scope<'a>) -> R,
    {
        let mut scope = Scope::new(self.rt.executor());
        f(&mut scope)
    }
}

struct ScopedFuture {
    f: Box<Future<Item = (), Error = ()> + Send + 'static>,
    _marker: mpsc::Sender<()>,
}

impl Future for ScopedFuture {
    type Item = ();
    type Error = ();
    fn poll(&mut self) -> futures::Poll<(), ()> {
        self.f.poll()
    }
}

impl<'a> Scope<'a> {
    fn scoped_future<'s, F>(&'s self, f: F) -> ScopedFuture
    where
        F: Future<Item = (), Error = ()> + Send + 'a,
        'a: 's,
    {
        let boxed: Box<Future<Item = (), Error = ()> + Send + 'a> = Box::new(f);
        // This transmute should be safe, as we use the `ScopedFuture` abstraction to prevent the
        // scope from exiting until every spawned `ScopedFuture` object is dropped, signifying that
        // they have completed their execution.
        let boxed: Box<Future<Item = (), Error = ()> + Send + 'static> =
            unsafe { std::mem::transmute(boxed) };
        ScopedFuture {
            f: boxed,
            _marker: self.send.deref().clone(),
        }
    }

    /// Spawn the received future on the [`ScopedRuntime`] which was used to create `self`.
    ///
    /// [`ScopedRuntime`]: /tokio-scoped/struct.ScopedRuntime.html
    pub fn spawn<'s, F>(&'s mut self, future: F) -> &mut Self
    where
        F: Future<Item = (), Error = ()> + Send + 'a,
        'a: 's,
    {
        let scoped_f = self.scoped_future(future);
        self.exec.spawn(scoped_f);
        self
    }

    /// Blocks the "current thread" of the runtime until `future` resolves. Other spawned futures
    /// can make progress while this future is running.
    pub fn block_on<'s, R, E, F>(&'s mut self, future: F) -> Result<R, E>
    where
        F: Future<Item = R, Error = E> + Send + 'a,
        R: Send + 'a,
        E: Send + 'a,
        'a: 's,
    {
        let (tx, rx) = futures::sync::oneshot::channel();
        let future = future.then(move |r| tx.send(r).map_err(|_| unreachable!()));
        let boxed: Box<Future<Item = (), Error = ()> + Send + 'a> = Box::new(future);
        let boxed: Box<Future<Item = (), Error = ()> + Send + 'static> =
            unsafe { std::mem::transmute(boxed) };
        self.exec.spawn(boxed);
        rx.wait().unwrap()
    }

    /// Creates an `inner` scope which can access variables created within the outer scope.
    pub fn scope<'inner, F, R>(&'inner self, f: F) -> R
    where
        F: FnOnce(&mut Scope<'inner>) -> R,
        'a: 'inner,
    {
        let mut scope = Scope::new(self.exec.clone());
        f(&mut scope)
    }

    /// Get a `TaskExecutor` to the underlying `Runtime` instance.
    pub fn executor(&self) -> TaskExecutor {
        self.exec.clone()
    }
}

impl<'a> Drop for Scope<'a> {
    fn drop(&mut self) {
        unsafe {
            ManuallyDrop::drop(&mut self.send);
        }

        let recv = self.recv.take().unwrap();
        let n = recv.wait().next();
        assert_eq!(n, None);
    }
}

#[cfg(test)]
mod testing {
    use super::*;
    use futures::lazy;
    use std::{thread, time::Duration};
    use tokio;
    fn make_runtime() -> ScopedRuntime {
        let rt = tokio::runtime::Runtime::new().expect("Failed to construct Runtime");
        ScopedRuntime::new(rt)
    }

    #[test]
    fn basic_test() {
        let scoped = make_runtime();
        scoped.scope(|scope| {
            scope.spawn(lazy(|| {
                tokio::spawn(lazy(|| {
                    println!("Another!");
                    thread::sleep(Duration::from_millis(5000));
                    println!("Another is done sleeping");
                    Ok(())
                }));

                println!("Sleeping a spawned future");
                // We should be able to spawn more and also verify that they complete...
                thread::sleep(Duration::from_millis(2000));
                println!("Completing!");
                Ok(())
            }));
        });
        println!("Completed");
    }

    #[test]
    fn access_stack() {
        let scoped = make_runtime();
        // Specifically a variable that does _not_ implement Copy.
        let uncopy = String::from("Borrowed!");
        scoped.scope(|scope| {
            scope.spawn(lazy(|| {
                assert_eq!(uncopy.as_str(), "Borrowed!");
                println!("Borrowed successfully: {}", uncopy);
                Ok(())
            }));
        });
    }

    #[test]
    fn access_mut_stack() {
        let scoped = make_runtime();
        let mut uncopy = String::from("Borrowed");
        let mut uncopy2 = String::from("Borrowed");
        scoped.scope(|scope| {
            scope.spawn(lazy(|| {
                let f = scoped
                    .scope(|scope2| scope2.block_on(lazy(|| Ok::<_, ()>(4))))
                    .unwrap();
                assert_eq!(f, 4);
                thread::sleep(Duration::from_millis(1000));
                uncopy.push('!');
                Ok(())
            }));

            scope.spawn(lazy(|| {
                uncopy2.push('f');
                Ok(())
            }));
        });

        assert_eq!(uncopy.as_str(), "Borrowed!");
        assert_eq!(uncopy2.as_str(), "Borrowedf");
    }

    #[test]
    fn access_mut_stack_scope_fn() {
        let mut uncopy = String::from("Borrowed");
        let mut uncopy2 = String::from("Borrowed");
        ::scope(|scope| {
            scope.spawn(lazy(|| {
                uncopy.push('!');
                Ok(())
            }));

            scope.spawn(lazy(|| {
                uncopy2.push('f');
                Ok(())
            }));
        });

        assert_eq!(uncopy.as_str(), "Borrowed!");
        assert_eq!(uncopy2.as_str(), "Borrowedf");
    }

    #[test]
    fn block_on_test() {
        let scoped = make_runtime();
        let mut uncopy = String::from("Borrowed");
        let captured = scoped.scope(|scope| {
            let v = scope
                .block_on(lazy(|| {
                    uncopy.push('!');
                    Ok::<_, ()>(uncopy)
                }))
                .unwrap();
            assert_eq!(v.as_str(), "Borrowed!");
            v
        });
        assert_eq!(captured.as_str(), "Borrowed!");
    }

    #[test]
    fn borrow_many_test() {
        let scoped = make_runtime();
        let mut values = vec![1, 2, 3, 4];
        scoped.scope(|scope| {
            for v in &mut values {
                scope.spawn(lazy(move || {
                    *v += 1;
                    Ok(())
                }));
            }
        });

        assert_eq!(&values, &[2, 3, 4, 5]);
    }

    #[test]
    fn inner_scope_test() {
        let scoped = make_runtime();
        let mut values = vec![1, 2, 3, 4];
        scoped.scope(|scope| {
            let mut v2s = vec![2, 3, 4, 5];
            scope.scope(|scope2| {
                scope2.spawn(lazy(|| {
                    v2s.push(100);
                    values.push(100);
                    Ok(())
                }));
            });
            // The inner scope must exit before we can get here.
            assert_eq!(v2s, &[2, 3, 4, 5, 100]);
            assert_eq!(values, &[1, 2, 3, 4, 100]);
        });
    }

    #[test]
    fn borrowed_scope_test() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        let mut values = vec![1, 2, 3, 4];
        ::scoped(&rt).scope(|scope| {
            scope.spawn(lazy(|| {
                values.push(100);
                Ok(())
            }));
        });
        assert_eq!(values, &[1, 2, 3, 4, 100]);
    }
}