mio::poll文档 —— 翻译和注解

翻译自mio文档: Poll

Struct mio::Poll

1
pub struct Poll { /* fields omitted */ }

Polls for readiness events on all registered values.

Poll allows a program to monitor a large number of Evented types, waiting until one or more become “ready” for some class of operations; e.g. reading and writing. An Evented type is considered ready if it is possible to immediately perform a corresponding operation; e.g. read or write.

To use Poll, an Evented type must first be registered with the Poll instance using the register method, supplying readiness interest. The readiness interest tells Poll which specific operations on the handle to monitor for readiness. A Token is also passed to the register function. When Poll returns a readiness event, it will include this token. This associates the event with the Evented handle that generated the event.

Poll用于从所有注册的value里poll处于就绪状态的事件。这个跟Java里NIO里的概念可以类比下:

  • Selector <-> Poll。可以把Evented注册到Poll上,使用它来监听Evented有关的IO事件。是实现异步IO的关键组件。
  • Evented <-> SelectionKey。可以通过SelectionKey获取底层的channel进行读写。Evented可以直接进行读写。
  • Token <-> SelectionKey里attach的对象。用于将事件跟Evented关联起来。
阅读更多

关于reborrow的一个复杂的例子

在查找关于Rust的reborrow的语法时,发现这么一篇文章Stuff the Identity Function Does (in Rust)。然后……看不懂,《Programming Rust》快看完了,这篇文章还是看不懂。
但是有很多不懂之处的文章,往往是最值得读的,因为它提供了一个线索,能把遗漏的知识串连起来,这是很难得的。

还好有Google, 一路搜索过来,大体也搞清楚了。

例子

文章里例子是这样的。有一个递归的数据结构,List:

1
2
3
struct List {
next: Option<Box<List>>,
}

写一个函数来遍历它

1
2
3
4
5
6
7
8
9
10
11
impl List {
fn walk_the_list(&mut self) {
let mut current = self;
loop {
match current.next {
None => return,
Some(ref mut inner) => current = inner,
}
}
}
}

可以在Rust的playgroud里测试一下。你会发现这段代码是通不过编译的。
问题在哪呢?

实际上这短短一段代码使用了很多隐晦的语法。

阅读更多