生成 mio 的 Token 时需要注意哪些限制?

What constraints should I be aware of when generating mio's Tokens?

The mio library for asynchronous I/O relies on the developer to provide instances of the Token type in order to correlate events that have happened back to the source, e.g. a particular TcpStream or Handler::Timeout.

the implementation 可以看出,Token 只是 usize 的包装类型。每次需要 Token 时简单地增加一个计数器很诱人,但它最终会溢出。

在生成 Tokens 传递给 EventLoop 时,我应该牢记哪些规则?一些具体问题:

谢谢!

简短版本:mio 除了在您收到相应事件时将它们传回给您之外,实际上不会对令牌做任何事情,因此您可以使用任何您想要的令牌 mio 关注。单独回答您的问题:

If I have two threads who each have their own EventLoop, can they both use Token=0 to listen for events on two different streams? (i.e. are Tokens bound to a particular EventLoop instance?)

当然可以。

Can I use Token=0 to simultaneously represent both a TcpStream and a pending Timeout, or are they both stored in the same collection of Tokens?

mio 没有令牌集合。如果您不需要唯一的令牌来标识应用程序代码中的内容,您可以在不同的地方自由使用相同的令牌。 (虽然我对这个问题有点困惑,因为据我所知,超时根本不使用 mio Tokens)

Is there any harm in jumping from 0 to 1,000,000? (e.g. Are they being stored in a data structure that's optimized for sequential numbers?)

没有。正如我上面所说,mio 不关心你的代币价值。