Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何创建tokio专用传输来覆盖默认的tick实现?_Rust_Rust Tokio - Fatal编程技术网

Rust 如何创建tokio专用传输来覆盖默认的tick实现?

Rust 如何创建tokio专用传输来覆盖默认的tick实现?,rust,rust-tokio,Rust,Rust Tokio,我正在尝试使用传输中的非默认tick()方法编写一个流式管道服务器。我以为这样就行了: use std::io; use mio::net::TcpListener; use tokio_core::reactor::PollEvented; use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::codec::{Framed, Encoder, Decoder}; use tokio_proto::streaming::pipeline::T

我正在尝试使用传输中的非默认
tick()
方法编写一个流式管道服务器。我以为这样就行了:

use std::io;

use mio::net::TcpListener;
use tokio_core::reactor::PollEvented;
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_io::codec::{Framed, Encoder, Decoder};
use tokio_proto::streaming::pipeline::Transport;

use codec::PacketCodec;

type GearmanIO = PollEvented<TcpListener>;
type GearmanFramed = Framed<GearmanIO, PacketCodec>;

impl Transport for GearmanFramed {
    fn tick(&mut self) {
        trace!("tick!");
    }

    fn cancel(&mut self) -> io::Result<()> {
        trace!("cancel!");
    }
}

经过几天的努力,我终于明白了这一点。答案是使用一个newtype来包围
框架
,从而避免默认实现

use std::io;

use tokio_io::{AsyncRead, AsyncWrite};
use tokio_io::codec::{Framed, Encoder, Decoder};
use tokio_proto::streaming::pipeline::Transport;
use futures::{Poll, Sink, StartSend, Stream};

use codec::PacketCodec;

pub struct GearmanFramed<T>(pub Framed<T, PacketCodec>);

impl<T> Transport for GearmanFramed<T>
    where T: AsyncRead + AsyncWrite + 'static
{
    fn tick(&mut self) {
        trace!("tick!");
    }

    fn cancel(&mut self) -> io::Result<()> {
        trace!("cancel!");
        Ok(())
    }
}

impl<T> Stream for GearmanFramed<T>
    where T: AsyncRead
{
    type Item = <PacketCodec as Decoder>::Item;
    type Error = <PacketCodec as Decoder>::Error;
    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        self.0.poll()
    }
}

impl<T> Sink for GearmanFramed<T>
    where T: AsyncWrite
{
    type SinkItem = <PacketCodec as Encoder>::Item;
    type SinkError = <PacketCodec as Encoder>::Error;
    fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
        self.0.start_send(item)
    }
    fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
        self.0.poll_complete()
    }
    fn close(&mut self) -> Poll<(), Self::SinkError> {
        self.0.close()
    }
}
使用std::io;
使用tokio_io::{AsyncRead,AsyncWrite};
使用tokio_io::编解码器:{Framed,Encoder,Decoder};
使用tokio_proto::streaming::pipeline::Transport;
使用未来:{Poll,Sink,StartSend,Stream};
使用codec::PacketCodec;
pub-struct-GearmanFramed(pub-Framed);
齿轮箱的impl传输
其中T:AsyncRead+AsyncWrite+'static
{
fn勾选(&mut self){
跟踪!(“滴答声!”);
}
fn取消(&mut self)->io::结果{
跟踪!(“取消!”);
好(())
}
}
Gearman框架的impl流
其中T:AsyncRead
{
类型Item=::Item;
类型错误=::错误;
fn轮询(&mut self)->轮询{
self.0.poll()
}
}
齿轮箱的impl水槽
其中T:AsyncWrite
{
输入SinkItem=::项;
类型SinkError=::Error;
fn开始发送(&mut self,item:self::SinkItem)->开始发送{
self.0.开始发送(项目)
}
fn轮询\u完成(&mut self)->轮询{
self.0.poll_complete()
}
fn关闭(&mut self)->轮询{
self.0.close()
}
}

FYI,一些元信息:这是“rustygear”中的代码,这是我一直致力于自学的Rust中“gearmand”的重写。我目前正在重构它以使用tokio,因为我的mio代码非常蹩脚。如果您感兴趣,可以在这里跟踪tokio重构:我在这里问了一个更一般的问题
use std::io;

use tokio_io::{AsyncRead, AsyncWrite};
use tokio_io::codec::{Framed, Encoder, Decoder};
use tokio_proto::streaming::pipeline::Transport;
use futures::{Poll, Sink, StartSend, Stream};

use codec::PacketCodec;

pub struct GearmanFramed<T>(pub Framed<T, PacketCodec>);

impl<T> Transport for GearmanFramed<T>
    where T: AsyncRead + AsyncWrite + 'static
{
    fn tick(&mut self) {
        trace!("tick!");
    }

    fn cancel(&mut self) -> io::Result<()> {
        trace!("cancel!");
        Ok(())
    }
}

impl<T> Stream for GearmanFramed<T>
    where T: AsyncRead
{
    type Item = <PacketCodec as Decoder>::Item;
    type Error = <PacketCodec as Decoder>::Error;
    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        self.0.poll()
    }
}

impl<T> Sink for GearmanFramed<T>
    where T: AsyncWrite
{
    type SinkItem = <PacketCodec as Encoder>::Item;
    type SinkError = <PacketCodec as Encoder>::Error;
    fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
        self.0.start_send(item)
    }
    fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
        self.0.poll_complete()
    }
    fn close(&mut self) -> Poll<(), Self::SinkError> {
        self.0.close()
    }
}