Add Handle::spawn_fn

Acts as a convenience to avoid `futures::lazy`.

Closes #40
This commit is contained in:
Alex Crichton
2016-09-26 16:52:54 -07:00
parent c2e80ebb30
commit 1d40bf14f7
+14 -1
View File
@@ -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<F, R>(&self, f: F)
where F: FnOnce() -> R + 'static,
R: IntoFuture<Item=(), Error=()> + 'static,
{
self.spawn(futures::lazy(f))
}
}
impl TimeoutState {