[英]How do I read_until the tokio::net::TcpStream in a future chain?
I would like to read data from the TcpStream
until I encounter a '\\0'. 我想从
TcpStream
读取数据,直到遇到'\\ 0'。 The issue is that tokio::io::read_until
needs the stream to be BufRead
. 问题是
tokio::io::read_until
需要将流设为BufRead
。
fn poll(&mut self) -> Poll<(), Self::Error> {
match self.listener.poll_accept()? {
Async::Ready((stream, _addr)) => {
let task = tokio::io::read_until(stream, 0, vec![0u8; buffer])
.map_err(|_| ...)
.map(|_| ...);
tokio::spawn(task);
}
Async::NotReady => return Ok(Async::NotReady),
}
}
How can I read data from the TcpStream
this way? 如何以这种方式从
TcpStream
读取数据?
Reading the documentation for BufRead
, you'll see the text: 阅读
BufRead
的文档,您将看到以下文本:
If you have something that implements
Read
, you can use theBufReader
type to turn it into aBufRead
.如果您具有实现
Read
,则可以使用BufReader
类型将其转换为BufRead
。
fn example(stream: TcpStream) {
io::read_until(std::io::BufReader::new(stream), 0, vec![]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.