1

在构建异步期货时,通常在唤醒器中实现ArcWake特征。我知道这样做是为了避免编写样板文件、创建 vtables 和编写不安全的代码(根据tokio 教程)。

现在,(这感觉像是一个愚蠢的问题)我不明白的是如何实现一个特征避免编写其他代码?当我检查trait 的来源时,它几乎不包含任何内容:只有一个需要实现以实现该 trait 的小函数的声明以及另一个调用该 trait 的函数的实例化。那么,我们避免写作的所有这些东西是从哪里来的呢?样板文件、vtables、不安全代码和所有这些在哪里?仅仅因为我们在唤醒器上实现了一个特征,它是如何被执行的?

4

1 回答 1

1

OK, I think I got it now. The code I couldn't find is here (thanks for the comment on the question that pointed this out!). That code is all called on the tutorial when the future is polled, specifically on the line

 let waker = task::waker(self.clone());

On this line, we're obtaining a waker from an ArcWake by calling the task::waker function which deals with all the complexity for us.

于 2021-08-31T11:04:08.123 回答