如何使用async_std :: io :: bufreader使用SELECT?
我需要使用 https://github.com/ yaa110/async-tun 。
我还需要控制可以通过另一个将来修改的哈希图访问的访问。
use futures::StreamExt;
const MTU_SIZE: usize = 1500;
let tun: Tun = match TunBuilder::new()
.name("")
.tap(false)
.packet_info(false)
.up()
.mtu(MTU_SIZE as i32)
.try_build()
.await
{
Ok(tun) => tun,
Err(e) => panic!("couldn't create tun device: {e:?}"),
};
use async_std::io::{BufReader, File};
let mut tun_reader: BufReader<&File> = tun.reader();
loop {
select! {
p = tun_reader.select_next_some() => { // read from hashmap }
e = ... => { // possibly add entries to hashmap }
}
}
这是不起作用的,因为bufreader&lt;&amp; file&gt;
不实现stream
。大多数示例使用.lines()。fuse()
创建与选择
一起使用的东西,但是我不需要行;我只需要一个[u8; mtu_size]
。
显然,我对Rust如何处理期货的方式感到震惊,因此指出我在该领域的介绍性资源是完全可以的。
在下面的@cdhowie午睡和一些建议之后,我认为我的循环看起来应该像
loop {
let mut packet: Packet = [0u8; 1500];
let read_task = tun_reader.read_exact(&mut packet).fuse();
pin_mut!(read_task);
select!{
p = read_task => { // read from hashmap }
e = ... => { // possibly add entries to hashmap }
}
}
I need to read a specific number of bytes from a TUN device set up using https://github.com/yaa110/async-tun.
I also need to control access to a hashmap that can be modified by another future.
use futures::StreamExt;
const MTU_SIZE: usize = 1500;
let tun: Tun = match TunBuilder::new()
.name("")
.tap(false)
.packet_info(false)
.up()
.mtu(MTU_SIZE as i32)
.try_build()
.await
{
Ok(tun) => tun,
Err(e) => panic!("couldn't create tun device: {e:?}"),
};
use async_std::io::{BufReader, File};
let mut tun_reader: BufReader<&File> = tun.reader();
loop {
select! {
p = tun_reader.select_next_some() => { // read from hashmap }
e = ... => { // possibly add entries to hashmap }
}
}
This does not work because BufReader<&File>
does not implement Stream
. Most examples use .lines().fuse()
to create something that works with select
, however I don't need lines; I just need a single [u8; MTU_SIZE]
.
I am clearly in over my head with how rust handles futures, so pointing me to introductory resources in this area is totally OK.
After a nap and some advice from @cdhowie below, I think my loop should look something like
loop {
let mut packet: Packet = [0u8; 1500];
let read_task = tun_reader.read_exact(&mut packet).fuse();
pin_mut!(read_task);
select!{
p = read_task => { // read from hashmap }
e = ... => { // possibly add entries to hashmap }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您已导入
async_std :: io :: prelude ::*
那么您将可以访问readext
,它具有从asyncread read read
值中获得期货的方法,例如作为bufreader
。在这种情况下,您可能想要tun_reader.read_exact()
,将其传递给[u8; mtu_size]
。If you've imported
async_std::io::prelude::*
then you'll have access toReadExt
, which has methods for obtaining futures from an asyncRead
value, such as aBufReader
. In this case you likely wanttun_reader.read_exact()
, passing it a mutable slice into a[u8; MTU_SIZE]
.