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
Sinkerror type is alwaysdnet::Error<Other>, whereOtheris the transport-specific error type. - Receiving from the
StreamyieldsResult<Incoming, Other>; the stream does not usednet::Errorfor the item error. - The generic
Othertype of the result yielded by theStreammatches the genericOthererror of thednet::Error<Other>used by theSink. - The
StreamreturnsNoneto signal that the transport is closed, regardless of the underlying cause. - When the
loggingfeature is enabled, transports also implementlogging::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::Loggingextendslogging::Loggingwhen “logging” feature is enabled; it’s an empty trait otherwise.