runtime: update the documentation around Handle (#2328)

This PR was prompted by having encountered a few cases of people not noticing that Runtime::handle can be cloned, and therefore not realizing it could be moved to another thread.
This commit is contained in:
Alice Ryhl 2020-03-17 22:59:26 +01:00 committed by GitHub
parent 06a4d895ec
commit 602ad2e0ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 7 deletions

View File

@ -7,7 +7,12 @@ cfg_rt_core! {
use std::future::Future; use std::future::Future;
} }
/// Handle to the runtime /// Handle to the runtime.
///
/// The handle is internally reference-counted and can be freely cloned. A handle can be
/// obtained using the [`Runtime::handle`] method.
///
/// [`Runtime::handle`]: crate::runtime::Runtime::handle()
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Handle { pub struct Handle {
pub(super) spawner: Spawner, pub(super) spawner: Spawner,
@ -26,7 +31,7 @@ pub struct Handle {
} }
impl Handle { impl Handle {
/// Enter the runtime context /// Enter the runtime context.
pub fn enter<F, R>(&self, f: F) -> R pub fn enter<F, R>(&self, f: F) -> R
where where
F: FnOnce() -> R, F: FnOnce() -> R,
@ -38,20 +43,21 @@ impl Handle {
/// ///
/// # Panic /// # Panic
/// ///
/// A Runtime must have been started or this will panic /// This will panic if called outside the context of a Tokio runtime.
/// ///
/// # Examples /// # Examples
/// ///
/// This allows for the current handle to be gotten when running in a `#` /// This can be used to obtain the handle of the surrounding runtime from an async
/// block or function running on that runtime.
/// ///
/// ``` /// ```
/// # use tokio::runtime::Runtime; /// # use tokio::runtime::Runtime;
///
/// # fn dox() { /// # fn dox() {
/// # let rt = Runtime::new().unwrap(); /// # let rt = Runtime::new().unwrap();
/// # rt.spawn(async { /// # rt.spawn(async {
/// use tokio::runtime::Handle; /// use tokio::runtime::Handle;
/// ///
/// // Inside an async block or function.
/// let handle = Handle::current(); /// let handle = Handle::current();
/// handle.spawn(async { /// handle.spawn(async {
/// println!("now running in the existing Runtime"); /// println!("now running in the existing Runtime");

View File

@ -419,7 +419,7 @@ impl Runtime {
}) })
} }
/// Enter the runtime context /// Enter the runtime context.
pub fn enter<F, R>(&self, f: F) -> R pub fn enter<F, R>(&self, f: F) -> R
where where
F: FnOnce() -> R, F: FnOnce() -> R,
@ -429,7 +429,8 @@ impl Runtime {
/// Return a handle to the runtime's spawner. /// Return a handle to the runtime's spawner.
/// ///
/// The returned handle can be used to spawn tasks that run on this runtime. /// The returned handle can be used to spawn tasks that run on this runtime, and can
/// be cloned to allow moving the `Handle` to other threads.
/// ///
/// # Examples /// # Examples
/// ///

View File

@ -16,6 +16,10 @@ doc_rt_core! {
/// When a runtime is shutdown, all outstanding tasks are dropped, /// When a runtime is shutdown, all outstanding tasks are dropped,
/// regardless of the lifecycle of that task. /// regardless of the lifecycle of that task.
/// ///
/// This function must be called from the context of a Tokio runtime. Tasks running on
/// the Tokio runtime are always inside its context, but you can also enter the context
/// using the [`Handle::enter`](crate::runtime::Handle::enter()) method.
///
/// # Examples /// # Examples
/// ///
/// In this example, a server is started and `spawn` is used to start a new task /// In this example, a server is started and `spawn` is used to start a new task