如何使用async_std :: io :: bufreader使用SELECT?

发布于 2025-02-05 20:33:04 字数 1431 浏览 1 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2025-02-12 20:33:04

如果您已导入async_std :: io :: prelude ::*那么您将可以访问 readext ,它具有从async read read read值中获得期货的方法,例如作为bufreader。在这种情况下,您可能想要tun_reader.read_exact(),将其传递给[u8; mtu_size]

If you've imported async_std::io::prelude::* then you'll have access to ReadExt, which has methods for obtaining futures from an async Read value, such as a BufReader. In this case you likely want tun_reader.read_exact(), passing it a mutable slice into a [u8; MTU_SIZE].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文