From 1d40bf14f7d04ec6c1d1a8b55692f49de51a0caa Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 26 Sep 2016 16:52:54 -0700 Subject: [PATCH] Add Handle::spawn_fn Acts as a convenience to avoid `futures::lazy`. Closes #40 --- src/reactor/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/reactor/mod.rs b/src/reactor/mod.rs index 4bb11a76b..a5090497d 100644 --- a/src/reactor/mod.rs +++ b/src/reactor/mod.rs @@ -12,7 +12,7 @@ use std::sync::Arc; use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; use std::time::{Instant, Duration}; -use futures::{Future, IntoFuture, Async}; +use futures::{self, Future, IntoFuture, Async}; use futures::task::{self, Unpark, Task, Spawn}; use mio; use slab::Slab; @@ -576,6 +576,19 @@ impl Handle { }; inner.borrow_mut().spawn(Box::new(f)); } + + /// Spawns a closure on this event loop. + /// + /// This function is a convenience wrapper around the `spawn` function above + /// for running a closure wrapped in `futures::lazy`. It will spawn the + /// function `f` provided onto the event loop, and continue to run the + /// future returned by `f` on the evnet loop as well. + pub fn spawn_fn(&self, f: F) + where F: FnOnce() -> R + 'static, + R: IntoFuture + 'static, + { + self.spawn(futures::lazy(f)) + } } impl TimeoutState {