如何解释read_until的签名以及Tokio中的AsyncRead + BufRead是什么? [英] How do I interpret the signature of read_until and what is AsyncRead + BufRead in Tokio?
问题描述
我试图了解Rust中的异步I/O.以下代码基于Katharina Fey的摘录 2019年1月的谈话对我有用:
I'm trying to understand asynchronous I/O in Rust. The following code is based on a snippet from Katharina Fey's Jan 2019 talk which works for me:
use futures::future::Future;
use std::io::BufReader;
use tokio::io::*;
fn main() {
let reader = BufReader::new(tokio::io::stdin());
let buffer = Vec::new();
println!("Type something:");
let fut = tokio::io::read_until(reader, b'\n', buffer)
.and_then(move |(stdin, buffer)| {
tokio::io::stdout()
.write_all(&buffer)
.map_err(|e| panic!(e))
})
.map_err(|e| panic!(e));
tokio::run(fut);
}
Before finding that code, I attempted to figure it out from the read_until
documentation.
如何解释read_until
的签名以在上面的代码示例中使用它?
How do I interpret the signature of read_until
to use it in a code sample like the one above?
pub fn read_until<A>(a: A, byte: u8, buf: Vec<u8>) -> ReadUntil<A>
where
A: AsyncRead + BufRead,
尤其是,从阅读文档中我如何知道and_then
闭包中传递的参数是什么以及预期的结果?
Specifically, how can I know from reading the documentation, what are the parameters passed into the and_then
closure and the expected result?
推荐答案
and_then
的参数
不幸的是,Rust文档的标准布局使期货很难遵循.
Parameters to and_then
Unfortunately the standard layout of the Rust documentation makes futures quite hard to follow.
从 read_until
文档开始您链接,我可以看到它返回ReadUntil<A>
.我将点击该链接转到 ReadUntil
文档.
Starting from the read_until
documentation you linked, I can see that it returns ReadUntil<A>
. I'll click on that to go to the ReadUntil
documentation.
此返回值描述为:
将来可以用来轻松地将流的内容读入向量,直到达到定界符为止.
A future which can be used to easily read the contents of a stream into a vector until the delimiter is reached.
我希望它能够实现Future
特征-并且我可以看到它能够实现.我还要假设将来解决的Item
是某种媒介,但是我不知道到底是什么,所以我一直在挖掘:
I would expect it to implement the Future
trait — and I can see that it does. I would also assume that the Item
that the future resolves to is some sort of vector, but I don't know exactly what, so I keep digging:
- 首先,我在特质实施"下查看并找到
impl<A> Future for ReadUntil<A>
- 我点击
[+]
扩展器
- First I look under "Trait implementations" and find
impl<A> Future for ReadUntil<A>
- I click the
[+]
expander
最后,我看到了相关的type Item = (A, Vec<u8>)
.这意味着它是一个Future
,它将返回一对值:A
,所以大概是给了我我传入的原始reader
,以及字节向量.
Finally I see the associated type Item = (A, Vec<u8>)
. This means it's a Future
that's going to return a pair of values: the A
, so it is presumably giving me back the original reader
that I passed in, plus a vector of bytes.
当将来解决这个元组时,我想用and_then
附加一些附加处理.这是Future
特性的一部分,因此我可以进一步向下滚动以找到该功能.
When the future resolves to this tuple, I want to attach some additional processing with and_then
. This is part of the Future
trait, so I can scroll down further to find that function.
fn and_then<F, B>(self, f: F) -> AndThen<Self, B, F>
where
F: FnOnce(Self::Item) -> B,
B: IntoFuture<Error = Self::Error>,
Self: Sized,
函数and_then
被记录为带有两个参数,但是self
是在将点语法用于 chain 函数时由编译器隐式传递的,这告诉我们可以编写read_until(A, '\n', buffer).and_then(...)
.文档中的第二个参数f: F
成为我们代码中传递给and_then
的第一个参数.
The function and_then
is documented as taking two parameters, but self
is passed implicitly by the compiler when using dot syntax to chain functions, which tells us that we can write read_until(A, '\n', buffer).and_then(...)
. The second parameter in the documentation, f: F
, becomes the first argument passed to and_then
in our code.
我可以看到f
是一个闭包,因为类型F
显示为FnOnce(Self::Item) -> B
(如果我单击指向
I can see that f
is a closure because the type F
is shown as FnOnce(Self::Item) -> B
(which if I click through links to the Rust book closure chapter.
传入的闭包f
以Self::Item
作为参数.我刚刚发现Item
是(A, Vec<u8>)
,所以我希望写类似.and_then(|(reader, buffer)| { /* ... /* })
The closure f
that is passed in takes Self::Item
as the parameter. I just found out that Item
is (A, Vec<u8>)
, so I expect to write something like .and_then(|(reader, buffer)| { /* ... /* })
这对可以读取哪种类型的读取器施加了限制.创建的 BufReader
实现
This is putting constraints on what type of reader can be read from. The created BufReader
implements BufRead
.
Helpfully, Tokio provides an implementation of AsyncRead
for BufReader
so we don't have to worry about it, we can just go ahead and use the BufReader
.
这篇关于如何解释read_until的签名以及Tokio中的AsyncRead + BufRead是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!