diff options
Diffstat (limited to 'tokio/src/runtime/handle.rs')
-rw-r--r-- | tokio/src/runtime/handle.rs | 117 |
1 files changed, 61 insertions, 56 deletions
diff --git a/tokio/src/runtime/handle.rs b/tokio/src/runtime/handle.rs index 53347778..f8119e65 100644 --- a/tokio/src/runtime/handle.rs +++ b/tokio/src/runtime/handle.rs @@ -1,13 +1,15 @@ -#[cfg(feature = "rt-core")] -use crate::runtime::basic_scheduler; -#[cfg(feature = "rt-full")] -use crate::runtime::thread_pool; use crate::runtime::{blocking, io, time}; -#[cfg(feature = "rt-core")] -use crate::task::JoinHandle; -#[cfg(feature = "rt-core")] -use std::future::Future; +cfg_rt_core! { + use crate::runtime::basic_scheduler; + use crate::task::JoinHandle; + + use std::future::Future; +} + +cfg_rt_threaded! { + use crate::runtime::thread_pool; +} /// Handle to the runtime #[derive(Debug, Clone)] @@ -31,57 +33,11 @@ pub(super) enum Kind { Shell, #[cfg(feature = "rt-core")] Basic(basic_scheduler::Spawner), - #[cfg(feature = "rt-full")] + #[cfg(feature = "rt-threaded")] ThreadPool(thread_pool::Spawner), } impl Handle { - /// 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 - /// - /// ``` - /// use tokio::runtime::Runtime; - /// - /// # fn dox() { - /// // Create the runtime - /// let rt = Runtime::new().unwrap(); - /// let handle = rt.handle(); - /// - /// // Spawn a future onto the runtime - /// handle.spawn(async { - /// println!("now running on a worker thread"); - /// }); - /// # } - /// ``` - /// - /// # 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. - #[cfg(feature = "rt-core")] - pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> - where - F: Future + Send + 'static, - F::Output: Send + 'static, - { - match &self.kind { - Kind::Shell => panic!("spawning not enabled for runtime"), - #[cfg(feature = "rt-core")] - Kind::Basic(spawner) => spawner.spawn(future), - #[cfg(feature = "rt-full")] - Kind::ThreadPool(spawner) => spawner.spawn(future), - } - } - /// Enter the runtime context pub fn enter<F, R>(&self, f: F) -> R where @@ -94,9 +50,58 @@ impl Handle { Kind::Shell => f(), #[cfg(feature = "rt-core")] Kind::Basic(spawner) => spawner.enter(f), - #[cfg(feature = "rt-full")] + #[cfg(feature = "rt-threaded")] Kind::ThreadPool(spawner) => spawner.enter(f), }) }) } } + +cfg_rt_core! { + impl Handle { + /// 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 + /// + /// ``` + /// use tokio::runtime::Runtime; + /// + /// # fn dox() { + /// // Create the runtime + /// let rt = Runtime::new().unwrap(); + /// let handle = rt.handle(); + /// + /// // Spawn a future onto the runtime + /// handle.spawn(async { + /// println!("now running on a worker thread"); + /// }); + /// # } + /// ``` + /// + /// # 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<F>(&self, future: F) -> JoinHandle<F::Output> + where + F: Future + Send + 'static, + F::Output: Send + 'static, + { + match &self.kind { + Kind::Shell => panic!("spawning not enabled for runtime"), + #[cfg(feature = "rt-core")] + Kind::Basic(spawner) => spawner.spawn(future), + #[cfg(feature = "rt-threaded")] + Kind::ThreadPool(spawner) => spawner.spawn(future), + } + } + } +} |