我想使用mio在Rust中編寫一個異步服務器,並且遇到緩衝區類型問題。我嘗試過不同的緩衝區類型,無法使其工作。我當前的代碼是:在mio中緩衝區類型的問題
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 = "*"
目前轉換爲字節0.2.11和MIO 0.4.3在Cargo.lock。
我得到的錯誤是這樣的:
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,以及你得到的是什麼錯誤。 – fjh
編輯了該問題並添加了錯誤消息。我知道我沒有使用正確的類型,因爲rustc抱怨,但我不知道我應該用什麼類型來覆蓋我的用例。 Rustc版本rustc 1.5.0-nightly(9d3e79ad3 2015-10-10) – elszben