From 90969420a2ac1562e3ac523e14bae064e169bf80 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Tue, 21 Jan 2020 21:58:20 -0500 Subject: docs: fix incorrectly rendered doc tests; tighten phrasing (#2150) --- tokio/src/task/task_local.rs | 74 ++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'tokio/src/task') diff --git a/tokio/src/task/task_local.rs b/tokio/src/task/task_local.rs index 6423175b..a3e7f03f 100644 --- a/tokio/src/task/task_local.rs +++ b/tokio/src/task/task_local.rs @@ -6,22 +6,22 @@ use std::pin::Pin; use std::task::{Context, Poll}; use std::{fmt, thread}; -/// Declare a new task local storage key of type [`tokio::task::LocalKey`]. +/// Declares a new task-local key of type [`tokio::task::LocalKey`]. /// /// # Syntax /// -/// The macro wraps any number of static declarations and makes them task locals. -/// Publicity and attributes for each static are allowed. +/// The macro wraps any number of static declarations and makes them local to the current task. +/// Publicity and attributes for each static is preserved. For example: /// /// # Examples /// /// ``` /// # use tokio::task_local; /// task_local! { -/// pub static FOO: u32; +/// pub static ONE: u32; /// /// #[allow(unused)] -/// static BAR: f32; +/// static TWO: f32; /// } /// # fn main() {} /// ``` @@ -61,59 +61,58 @@ macro_rules! __task_local_inner { /// A key for task-local data. /// -/// This type is generated by `task_local!` macro and unlike `thread_local!` it has -/// no concept of lazily initialization. Instead, it is designed to provide task local -/// storage the future that is passed to `set`. +/// This type is generated by the `task_local!` macro. /// -/// # Initialization and Destruction -/// -/// Initialization is done via `set` which is an `async fn` that wraps another -/// [`std::future::Future`] and will set the value on each `Future::poll` call. -/// Once the `set` future is dropped the corresponding task local value is also -/// dropped. +/// Unlike [`std::thread::LocalKey`], `tokio::task::LocalKey` will +/// _not_ lazily initialize the value on first access. Instead, the +/// value is first initialized when the future containing +/// the task-local is first polled by a futures executor, like Tokio. /// /// # Examples /// /// ``` /// # async fn dox() { /// tokio::task_local! { -/// static FOO: u32; +/// static NUMBER: u32; /// } /// -/// FOO.scope(1, async move { -/// assert_eq!(FOO.get(), 1); +/// NUMBER.scope(1, async move { +/// assert_eq!(NUMBER.get(), 1); /// }).await; /// -/// FOO.scope(2, async move { -/// assert_eq!(FOO.get(), 2); +/// NUMBER.scope(2, async move { +/// assert_eq!(NUMBER.get(), 2); /// -/// FOO.scope(3, async move { -/// assert_eq!(FOO.get(), 3); +/// NUMBER.scope(3, async move { +/// assert_eq!(NUMBER.get(), 3); /// }).await; /// }).await; /// # } /// ``` +/// [`std::thread::LocalKey`]: https://doc.rust-lang.org/std/thread/struct.LocalKey.html pub struct LocalKey { #[doc(hidden)] pub inner: thread::LocalKey>>, } impl LocalKey { - /// Sets a value `T` as the task local value for the future `F`. + /// Sets a value `T` as the task-local value for the future `F`. + /// + /// On completion of `scope`, the task-local will be dropped. /// - /// This will run the provided future to completion and set the - /// provided value as the task local under this key. Once the returned - /// future is dropped so will the value passed be dropped. + /// ### Examples /// + /// ``` /// # async fn dox() { /// tokio::task_local! { - /// static FOO: u32; + /// static NUMBER: u32; /// } /// - /// FOO.scope(1, async move { - /// println!("task local value: {}", FOO.get()); + /// NUMBER.scope(1, async move { + /// println!("task local value: {}", NUMBER.get()); /// }).await; /// # } + /// ``` pub async fn scope(&'static self, value: T, f: F) -> F::Output where F: Future, @@ -126,13 +125,12 @@ impl LocalKey { .await } - /// Access this task-local key, running the provided closure with a reference - /// passed to the value. + /// Accesses the current task-local and runs the provided closure. /// /// # Panics /// - /// This function will panic if not called within a future that has not been - /// set via `LocalKey::set`. + /// This function will panic if not called within the context + /// of a future containing a task-local with the corresponding key. pub fn with(&'static self, f: F) -> R where F: FnOnce(&T) -> R, @@ -143,9 +141,11 @@ impl LocalKey { ) } - /// Access this task-local key, running the provided closure with a reference - /// passed to the value. Unlike `with` this function will return a `Result` - /// instead of panicking. + /// Accesses the current task-local and runs the provided closure. + /// + /// If the task-local with the accociated key is not present, this + /// method will return an `AccessError`. For a panicking variant, + /// see `with`. pub fn try_with(&'static self, f: F) -> Result where F: FnOnce(&T) -> R, @@ -161,8 +161,8 @@ impl LocalKey { } impl LocalKey { - /// Get a copy of the task-local value if it implements - /// the `Copy` trait. + /// Returns a copy of the task-local value + /// if the task-local value implements `Copy`. pub fn get(&'static self) -> T { self.with(|v| *v) } -- cgit v1.2.3