检测未来时,特征实现在哪里?

问题描述 投票:0回答:1

我很难理解 tracing crate 中的这个简单示例

use tracing::Instrument;

let my_future = async {
    // ...
};

my_future
    .instrument(tracing::info_span!("my_future"))
    .await

我是异步编程新手,这可能是一个菜鸟问题。

根据我到目前为止的学习,Future 应该有一个

tracing::Instrument
特征的实现(这种说法可能是错误的,因为为另一个特征实现一个特征听起来很尴尬,请纠正我)。实现在哪里?

因为我找不到实现,所以在

instrument()
上调用
my_future
方法在我看来就像是一个魔法。

请帮忙!

rust async-await rust-tracing
1个回答
0
投票

有一个适用于所有 T

 的实现,其中 
T: Sized
,其中显然包括期货。 
instrumented()
特征方法返回一个
Instrumented<T>
,它
仅在Future
实现
时实现
T

换句话说,你可以对非期货调用

instrumented()

,但不能等待结果。

© www.soinside.com 2019 - 2024. All rights reserved.