gpt4 book ai didi

udp - 使用 mio 使用 UDP 并收到错误 "MutBuf is not implemented for the type MutSliceBuf"

转载 作者:行者123 更新时间:2023-11-29 08:17:03 24 4
gpt4 key购买 nike

出于学习目的,我目前正在尝试编写一个小程序,该程序将为 UDP 数据包实现 echo-server,它将在特定端口集(比如 10000-60000)上工作。因此,为此发送 50k 线程垃圾邮件并不是一件好事,我需要使用异步 IO,而 mio 非常适合此任务。但是我从一开始就遇到了这个代码的问题:

extern crate mio;
extern crate bytes;
use mio::udp::*;
use bytes::MutSliceBuf;

fn main() {
let addr = "127.0.0.1:10000".parse().unwrap();

let socket = UdpSocket::bound(&addr).unwrap();

let mut buf = [0; 128];
socket.recv_from(&mut MutSliceBuf::wrap(&mut buf));
}

它几乎是 mio 的 test_udp_socket.rs 的完整复制粘贴。但是当 mio 的测试成功通过时,我尝试编译这段代码,但出现以下错误:

src/main.rs:12:12: 12:55 error: the trait `bytes::buf::MutBuf` is not implemented for the type `bytes::buf::slice::MutSliceBuf<'_>` [E0277]
src/main.rs:12 socket.recv_from(&mut MutSliceBuf::wrap(&mut buf));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:12:12: 12:55 help: run `rustc --explain E0277` to see a detailed explanation

但是从 bytes crate(也是它的本地副本)检查 src/buf/slice.rs 的代码,我们可以清楚地看到实现了这个特性:

impl<'a> MutBuf for MutSliceBuf<'a> {
fn remaining(&self) -> usize {
self.bytes.len() - self.pos
}

fn advance(&mut self, mut cnt: usize) {
cnt = cmp::min(cnt, self.remaining());
self.pos += cnt;
}

unsafe fn mut_bytes<'b>(&'b mut self) -> &'b mut [u8] {
&mut self.bytes[self.pos..]
}
}

这可能是一些微不足道的事情,但我找不到它...可能是什么问题导致了这个错误?

我正在使用 rustc 1.3.0 (9a92aaf19 2015-09-15), crate mio 和字节直接从 github 获取。

最佳答案

使用 cargo

[dependencies]
mio = "*"
bytes = "*"

这适合我。使用 Github 依赖,

[dependencies.mio]
git = "https://github.com/carllerche/mio.git"

给出你提​​到的错误。

奇怪的是,0.4版本依赖

bytes = "0.2.11"

而 master 依赖于

git = "https://github.com/carllerche/bytes"
rev = "7edb577d0a"

这只是版本 0.2.10。奇怪。

问题是你最终编译了 两个 bytes 依赖项,所以错误更像是

the trait `mio::bytes::buf::MutBuf` is not implemented for the type `self::bytes::buf::slice::MutSliceBuf<'_>`

我认为解决这个问题的最简单方法是只使用 crates.io 中的两个包。

[dependencies]
mio = "*"
bytes = "*"

另一种方法是使用

[dependencies.bytes]
git = "https://github.com/carllerche/bytes"
rev = "7edb577d0a"

在您自己的 Cargo.toml 中,这样您就可以共享版本。

关于udp - 使用 mio 使用 UDP 并收到错误 "MutBuf is not implemented for the type MutSliceBuf",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32663870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com