docs: fix incorrectly rendered doc tests; tighten phrasing (#2150)

This commit is contained in:
David Barsky 2020-01-21 21:58:20 -05:00 committed by GitHub
parent bffbaab30d
commit 90969420a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<T: 'static> {
#[doc(hidden)]
pub inner: thread::LocalKey<RefCell<Option<T>>>,
}
impl<T: 'static> LocalKey<T> {
/// 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`.
///
/// 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.
/// On completion of `scope`, the task-local will 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<F>(&'static self, value: T, f: F) -> F::Output
where
F: Future,
@ -126,13 +125,12 @@ impl<T: 'static> LocalKey<T> {
.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<F, R>(&'static self, f: F) -> R
where
F: FnOnce(&T) -> R,
@ -143,9 +141,11 @@ impl<T: 'static> LocalKey<T> {
)
}
/// Access this task-local key, running the provided closure with a reference
/// passed to the value. Unlike `with` this function will return a `Result<R, AccessError>`
/// 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<F, R>(&'static self, f: F) -> Result<R, AccessError>
where
F: FnOnce(&T) -> R,
@ -161,8 +161,8 @@ impl<T: 'static> LocalKey<T> {
}
impl<T: Copy + 'static> LocalKey<T> {
/// 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)
}