diff --git a/src/runtime.rs b/src/runtime.rs index 700a1222a..8fb22f61b 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -107,9 +107,9 @@ use reactor::{self, Reactor, Handle}; use reactor::background::Background; -use tokio_threadpool::{self as threadpool, ThreadPool}; +use tokio_threadpool::{self as threadpool, ThreadPool, Sender}; use futures::Poll; -use futures::future::Future; +use futures::future::{self, Future}; use std::{fmt, io}; @@ -126,6 +126,17 @@ pub struct Runtime { inner: Option, } +/// Executes futures on the runtime +/// +/// All futures spawned using this executor will be submitted to the associated +/// Runtime's executor. This executor is usually a thread pool. +/// +/// For more details, see the [module level](index.html) documentation. +#[derive(Debug, Clone)] +pub struct TaskExecutor { + inner: Sender, +} + /// A future that resolves when the Tokio `Runtime` is shut down. pub struct Shutdown { inner: Box + Send>, @@ -226,7 +237,13 @@ impl Runtime { /// Return a reference to the reactor handle for this runtime instance. pub fn handle(&self) -> &Handle { - self.inner.as_ref().unwrap().reactor.handle() + self.inner().reactor.handle() + } + + /// Return a handle to the runtime's executor. + pub fn executor(&self) -> TaskExecutor { + let inner = self.inner().pool.sender().clone(); + TaskExecutor { inner } } /// Spawn a future onto the Tokio runtime. @@ -336,11 +353,33 @@ impl Runtime { Shutdown { inner } } + fn inner(&self) -> &Inner { + self.inner.as_ref().unwrap() + } + fn inner_mut(&mut self) -> &mut Inner { self.inner.as_mut().unwrap() } } +// ===== impl TaskExecutor ===== + +impl future::Executor for TaskExecutor +where T: Future + Send + 'static, +{ + fn execute(&self, future: T) -> Result<(), future::ExecuteError> { + self.inner.execute(future) + } +} + +impl ::executor::Executor for TaskExecutor { + fn spawn(&mut self, future: Box + Send>) + -> Result<(), ::executor::SpawnError> + { + self.inner.spawn(future) + } +} + // ===== impl Shutdown ===== impl Future for Shutdown {