From bb9de276aeb1ea55cd326912148bf7b2e24f1cda Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 8 Mar 2018 14:54:31 -0800 Subject: [PATCH] Add an explicit spawn fn to TaskExecutor (#200) --- src/runtime.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/runtime.rs b/src/runtime.rs index 28cbdd3e6..1b4a83ddb 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -363,6 +363,50 @@ impl Runtime { // ===== impl TaskExecutor ===== +impl TaskExecutor { + /// Spawn a future onto the Tokio runtime. + /// + /// This spawns the given future onto the runtime's executor, usually a + /// thread pool. The thread pool is then responsible for polling the future + /// until it completes. + /// + /// See [module level][mod] documentation for more details. + /// + /// [mod]: index.html + /// + /// # Examples + /// + /// ```rust + /// # extern crate tokio; + /// # extern crate futures; + /// # use futures::{future, Future, Stream}; + /// use tokio::runtime::Runtime; + /// + /// # fn dox() { + /// // Create the runtime + /// let mut rt = Runtime::new().unwrap(); + /// let executor = rt.executor(); + /// + /// // Spawn a future onto the runtime + /// executor.spawn(future::lazy(|| { + /// println!("now running on a worker thread"); + /// Ok(()) + /// })); + /// # } + /// # pub fn main() {} + /// ``` + /// + /// # Panics + /// + /// This function panics if the spawn fails. Failure occurs if the executor + /// is currently at capacity and is unable to spawn a new future. + pub fn spawn(&self, future: F) + where F: Future + Send + 'static, + { + self.inner.spawn(future).unwrap(); + } +} + impl future::Executor for TaskExecutor where T: Future + Send + 'static, {