1

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>ソケットに a を書き込んで、バッファが部分的にしか書き込まれていない場合を処理できるようにしたいと思います。どうすればそれを達成できますか?

戻り値を適切に処理するコードについての説明は必要ありません。この質問はバッファ タイプに関するものです。どのバッファタイプを使用する必要があるかわかりません。

4

1 に答える 1