跳到主要内容

LocalKey

搜索

结构体 LocalKey 

Source
pub struct LocalKey<T: 'static> { /* private fields */ }
展开描述

用于 task-local 数据的 key。

此类型由 task_local! 宏生成。

std::thread::LocalKey 不同,tokio::task::LocalKey 不会在首次访问时惰性初始化值。相反,值会在包含 task-local 的 future 第一次被 Tokio 等 futures 执行器 poll 时初始化。

§示例

tokio::task_local! {
    static NUMBER: u32;
}

NUMBER.scope(1, async move {
    assert_eq!(NUMBER.get(), 1);
}).await;

NUMBER.scope(2, async move {
    assert_eq!(NUMBER.get(), 2);

    NUMBER.scope(3, async move {
        assert_eq!(NUMBER.get(), 3);
    }).await;
}).await;

实现§

Source§

impl<T: 'static> LocalKey<T>

Source

pub fn scope<F>(&'static self, value: T, f: F) -> TaskLocalFuture<T, F>
where F: Future,

将值 T 设置为 future F 的 task-local 值。

scope 完成后,task-local 将被丢弃。

§Panics

如果你在同一个 LocalKey 上调用 withtry_with 时 poll 返回的 future,则对 poll 的调用将 panic。

§示例
tokio::task_local! {
    static NUMBER: u32;
}

NUMBER.scope(1, async move {
    println!("task local value: {}", NUMBER.get());
}).await;
Source

pub fn sync_scope<F, R>(&'static self, value: T, f: F) -> R
where F: FnOnce() -> R,

将值 T 设置为闭包 F 的 task-local 值。

sync_scope 完成后,task-local 将被丢弃。

§Panics

如果在同一个 LocalKey 上调用 withtry_with 时调用此方法,则会 panic。

§示例
tokio::task_local! {
    static NUMBER: u32;
}

NUMBER.sync_scope(1, || {
    println!("task local value: {}", NUMBER.get());
});
Source

pub fn with<F, R>(&'static self, f: F) -> R
where F: FnOnce(&T) -> R,

访问当前 task-local 并运行提供的闭包。

§Panics

如果 task-local 没有设置值,此函数将 panic。

Source

pub fn try_with<F, R>(&'static self, f: F) -> Result<R, AccessError>
where F: FnOnce(&T) -> R,

访问当前 task-local 并运行提供的闭包。

如果与该 key 关联的 task-local 不存在,此方法将返回一个 AccessError。关于会 panic 的变体,请参阅 with

Source§

impl<T: Clone + 'static> LocalKey<T>

Source

pub fn get(&'static self) -> T

如果 task-local 值实现了 Clone,则返回该 task-local 值的一个副本。

§Panics

如果 task-local 没有设置值,此函数将 panic。

Source

pub fn try_get(&'static self) -> Result<T, AccessError>

如果 task-local 值实现了 Clone,则返回该 task-local 值的一个副本。

如果与该 key 关联的 task-local 不存在,此方法将返回一个 AccessError。关于会 panic 的变体,请参阅 get

Trait 实现§

Source§

impl<T: 'static> Debug for LocalKey<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化器格式化此值。 更多信息

自动 Trait 实现§

§

impl<T> Freeze for LocalKey<T>

§

impl<T> RefUnwindSafe for LocalKey<T>

§

impl<T> Send for LocalKey<T>

§

impl<T> Sync for LocalKey<T>

§

impl<T> Unpin for LocalKey<T>

§

impl<T> UnsafeUnpin for LocalKey<T>

§

impl<T> UnwindSafe for LocalKey<T>

Blanket 实现§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换的具体行为取决于 From<T> for U 的实现方式。

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。