跳到主要内容

TaskLocalFuture

搜索

结构体 TaskLocalFuture 

源代码
pub struct TaskLocalFuture<T, F>
where T: 'static,
{ /* 私有字段 */ }
展开描述

在 future F 执行期间为其设置任务本地值 T 的 future。

任务本地的值必须为 'static,并将在 future 完成时被 drop。

由函数 LocalKey::scope 创建。

§示例

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

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

实现§

源代码§

impl<T, F> TaskLocalFuture<T, F>
where T: 'static,

源代码

pub fn take_value(self: Pin<&mut Self>) -> Option<T>

返回此 TaskLocalFuture 存储在任务本地中的值。

函数返回:

  • Some(T) if the task local value exists.
  • None if the task local value has already been taken.

请注意,即使 future 尚未完成,此函数也会尝试获取任务本地值。在这种情况下,在调用 take_value 之后,该值将不再可通过任务本地获得。

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

let fut = KEY.scope(42, async {
    // Do some async work
});

let mut pinned = Box::pin(fut);

// Complete the TaskLocalFuture
let _ = pinned.as_mut().await;

// And here, we can take task local value
let value = pinned.as_mut().take_value();

assert_eq!(value, Some(42));

trait 实现§

源代码§

impl<T, F> Debug for TaskLocalFuture<T, F>
where T: Debug + 'static,

源代码§

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

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

impl<T: 'static, F> Drop for TaskLocalFuture<T, F>

源代码§

fn drop(&mut self)

执行此类型的析构函数。 更多信息
源代码§

impl<T: 'static, F: Future> Future for TaskLocalFuture<T, F>

源代码§

type Output = <F as Future>::Output

Future 完成时产生的值的类型。
源代码§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. 更多信息
源代码§

impl<'__pin, T, F> Unpin for TaskLocalFuture<T, F>
where PinnedFieldsOf<__Origin<'__pin, T, F>>: Unpin, T: 'static,

自动 trait 实现§

§

impl<T, F> Freeze for TaskLocalFuture<T, F>
where T: Freeze, F: Freeze,

§

impl<T, F> RefUnwindSafe for TaskLocalFuture<T, F>

§

impl<T, F> Send for TaskLocalFuture<T, F>
where T: Send, F: Send,

§

impl<T, F> Sync for TaskLocalFuture<T, F>
where T: Sync, F: Sync,

§

impl<T, F> !UnsafeUnpin for TaskLocalFuture<T, F>

§

impl<T, F> UnwindSafe for TaskLocalFuture<T, F>
where T: UnwindSafe, F: UnwindSafe,

blanket 实现§

源代码§

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

源代码§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
源代码§

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

源代码§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
源代码§

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

源代码§

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

Mutably borrows from an owned value. 更多信息
源代码§

impl<T> From<T> for T

源代码§

fn from(t: T) -> T

原样返回参数。

源代码§

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

源代码§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换是 From<T> for U 实现选择执行的操作。

源代码§

impl<F> IntoFuture for F
where F: Future,

源代码§

type Output = <F as Future>::Output

Future 完成时产生的输出。
源代码§

type IntoFuture = F

我们将要把此值转变成哪种 future?
源代码§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. 更多信息
源代码§

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

源代码§

type Error = Infallible

转换出错时返回的类型。
源代码§

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

执行转换。
源代码§

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

源代码§

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

转换出错时返回的类型。
源代码§

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

执行转换。