我正在尝试实现一些类似于用AsynchronousByteChannel在Java语言中读取文件的功能,比如
AsynchronousFileChannel channel = AsynchronousFileChannel.open(path...
channel.read(buffer,... new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result) {
...use buffer
}也就是说,尽可能多地读取操作系统,进程,请求更多等等。使用async_std实现这一点的最直接方法是什么?
发布于 2020-12-03 21:55:35
您可以使用async_std::io::Read特征的read方法:
use async_std::prelude::*;
let mut reader = obtain_read_somehow();
let mut buf = [0; 4096]; // or however large you want it
// read returns a Poll<Result> so you have to handle the result
loop {
let byte_count = reader.read(&mut buf).await?;
if byte_count == 0 {
// 0 bytes read means we're done
break;
}
// call whatever handler function on the bytes read
handle(&buf[..byte_count]);
}https://stackoverflow.com/questions/65127131
复制相似问题