Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Transport contract

A dnet transport implements both futures::Sink and futures::Stream to provide a unified asynchronous transport interface.

A dnet transport must satisfy these requirements:

  • Implement Sink<Outgoing, Error = dnet::Error<Other>> for outgoing messages.
  • Implement Stream<Item = Result<Incoming, Other>> for incoming messages.
  • The Sink error type is always dnet::Error<Other>, where Other is the transport-specific error type.
  • Receiving from the Stream yields Result<Incoming, Other>; the stream does not use dnet::Error for the item error.
  • The generic Other type of the result yielded by the Stream matches the generic Other error of the dnet::Error<Other> used by the Sink.
  • The Stream returns None to signal that the transport is closed, regardless of the underlying cause.
  • When the logging feature is enabled, transports also implement logging::Logging.
/// Transport error.
#[derive(Debug, PartialEq, Eq)]
pub enum Error<Other> {
    /// Occurs when transport is closed.
    Closed,

    /// Other non-predefined transport-specific error.
    Other(Other),
}

/// Trait for transports implementing `dnet` interface.
pub trait Transport<Incoming, Outgoing, Error>:
    Sink<Outgoing, Error = crate::Error<Error>>
    + Stream<Item = Result<Incoming, Error>>
    + conditional::Logging
{
}

impl<T, Incoming, Outgoing, Error> Transport<Incoming, Outgoing, Error> for T where
    T: Sink<Outgoing, Error = crate::Error<Error>>
        + Stream<Item = Result<Incoming, Error>>
        + conditional::Logging
{
}

Note

conditional::Logging extends logging::Logging when “logging” feature is enabled; it’s an empty trait otherwise.