Skip to main content

Codec

Trait Codec 

pub trait Codec {
    type Protocol: AsRef<str> + Send + Clone;
    type Request: Send;
    type Response: Send;

    // Required methods
    fn read_request<T>(
        &mut self,
        protocol: &Self::Protocol,
        io: &mut T,
    ) -> impl Future<Output = Result<Self::Request, Error>> + Send
       where T: AsyncRead + Unpin + Send;
    fn read_response<T>(
        &mut self,
        protocol: &Self::Protocol,
        io: &mut T,
    ) -> impl Future<Output = Result<Self::Response, Error>> + Send
       where T: AsyncRead + Unpin + Send;
    fn write_request<T>(
        &mut self,
        protocol: &Self::Protocol,
        io: &mut T,
        req: Self::Request,
    ) -> impl Future<Output = Result<(), Error>> + Send
       where T: AsyncWrite + Unpin + Send;
    fn write_response<T>(
        &mut self,
        protocol: &Self::Protocol,
        io: &mut T,
        res: Self::Response,
    ) -> impl Future<Output = Result<(), Error>> + Send
       where T: AsyncWrite + Unpin + Send;
}
Available on crate feature request-response only.
Expand description

A Codec defines the request and response types for a request-response Behaviour protocol or protocol family and how they are encoded / decoded on an I/O stream.

Required Associated Types§

type Protocol: AsRef<str> + Send + Clone

The type of protocol(s) or protocol versions being negotiated.

type Request: Send

The type of inbound and outbound requests.

type Response: Send

The type of inbound and outbound responses.

Required Methods§

fn read_request<T>( &mut self, protocol: &Self::Protocol, io: &mut T, ) -> impl Future<Output = Result<Self::Request, Error>> + Send
where T: AsyncRead + Unpin + Send,

Reads a request from the given I/O stream according to the negotiated protocol.

fn read_response<T>( &mut self, protocol: &Self::Protocol, io: &mut T, ) -> impl Future<Output = Result<Self::Response, Error>> + Send
where T: AsyncRead + Unpin + Send,

Reads a response from the given I/O stream according to the negotiated protocol.

fn write_request<T>( &mut self, protocol: &Self::Protocol, io: &mut T, req: Self::Request, ) -> impl Future<Output = Result<(), Error>> + Send
where T: AsyncWrite + Unpin + Send,

Writes a request to the given I/O stream according to the negotiated protocol.

fn write_response<T>( &mut self, protocol: &Self::Protocol, io: &mut T, res: Self::Response, ) -> impl Future<Output = Result<(), Error>> + Send
where T: AsyncWrite + Unpin + Send,

Writes a response to the given I/O stream according to the negotiated protocol.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

§

impl<Req, Resp> Codec for libp2p::request_response::cbor::codec::Codec<Req, Resp>

§

type Protocol = StreamProtocol

§

type Request = Req

§

type Response = Resp

§

impl<Req, Resp> Codec for libp2p::request_response::json::codec::Codec<Req, Resp>

§

type Protocol = StreamProtocol

§

type Request = Req

§

type Response = Resp