Skip to content

Commit 1d40bf1

Browse files
committed
Add Handle::spawn_fn
Acts as a convenience to avoid `futures::lazy`. Closes #40
1 parent c2e80eb commit 1d40bf1

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/reactor/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::sync::Arc;
1212
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
1313
use std::time::{Instant, Duration};
1414

15-
use futures::{Future, IntoFuture, Async};
15+
use futures::{self, Future, IntoFuture, Async};
1616
use futures::task::{self, Unpark, Task, Spawn};
1717
use mio;
1818
use slab::Slab;
@@ -576,6 +576,19 @@ impl Handle {
576576
};
577577
inner.borrow_mut().spawn(Box::new(f));
578578
}
579+
580+
/// Spawns a closure on this event loop.
581+
///
582+
/// This function is a convenience wrapper around the `spawn` function above
583+
/// for running a closure wrapped in `futures::lazy`. It will spawn the
584+
/// function `f` provided onto the event loop, and continue to run the
585+
/// future returned by `f` on the evnet loop as well.
586+
pub fn spawn_fn<F, R>(&self, f: F)
587+
where F: FnOnce() -> R + 'static,
588+
R: IntoFuture<Item=(), Error=()> + 'static,
589+
{
590+
self.spawn(futures::lazy(f))
591+
}
579592
}
580593

581594
impl TimeoutState {

0 commit comments

Comments
 (0)