【发布时间】:2015-10-24 07:53:41
【问题描述】:
我想使用 mio 在 Rust 中编写一个异步服务器,但我遇到了缓冲区类型的问题。我尝试了不同的缓冲区类型,但无法正常工作。我当前的代码是:
extern crate mio;
extern crate bytes;
use std::io;
use std::io::{Error, ErrorKind};
use std::net::SocketAddr;
use std::str::FromStr;
use std::io::Cursor;
use self::mio::PollOpt;
use self::mio::EventLoop;
use self::mio::EventSet;
use self::mio::Token;
use self::mio::Handler;
use self::mio::io::TryRead;
use self::mio::io::TryWrite;
//use self::mio::buf::ByteBuf;
//use self::mio::buf::Buf;
use self::mio::tcp::*;
use self::bytes::buf::Buf;
use self::bytes::buf::byte::ByteBuf;
struct EventHandler;
impl Handler for EventHandler {
type Timeout = ();
type Message = ();
fn ready(&mut self, event_loop: &mut EventLoop<EventHandler>, token: Token, events: EventSet) {
}
}
pub struct Connection {
sock: TcpStream,
send_queue: Vec<ByteBuf>,
}
impl Connection {
pub fn writable(&mut self, event_loop: &mut EventLoop<EventHandler>) -> Result<(), String> {
while !self.send_queue.is_empty() {
if !self.send_queue.first().unwrap().has_remaining() {
self.send_queue.pop();
}
let buf = self.send_queue.first_mut().unwrap();
match self.sock.try_write_buf(&mut buf) {
Ok(None) => {
return Ok(());
}
Ok(Some(n)) => {
continue;
}
Err(e) => {
return Err(format!("{}", e));
}
}
}
Ok(())
}
}
fn main() {
println!("Hello, world!");
}
Cargo.toml 包含以下依赖项:
mio = "*"
bytes = "*"
目前在 Cargo.lock 中转换为字节 0.2.11 和 mio 0.4.3。
我得到的错误是:
main.rs:45:29: 45:52 error: the trait `bytes::buf::Buf` is not implemented
for the type `&mut bytes::buf::byte::ByteBuf` [E0277]
main.rs:45 match self.sock.try_write_buf(&mut buf) {
我希望能够将Vec<u8> 写入套接字并处理仅部分写入缓冲区的情况。我怎样才能做到这一点?
我不需要解释正确处理返回值的代码,这个问题是关于缓冲区类型的。我不知道我必须使用哪种缓冲区类型。
【问题讨论】:
-
我认为仅凭您发布的信息无法为您提供帮助。我们至少需要您的
Cargo.toml来重现问题,并且您还应该说明您正在使用的 Rust 版本以及您遇到的确切错误。 -
编辑了问题并添加了错误消息。我知道我没有使用正确的类型,因为 rustc 在抱怨,但我不知道应该使用什么类型来涵盖我的用例。 Rustc 版本 rustc 1.5.0-nightly (9d3e79ad3 2015-10-10)