pub trait Protocol<Rin, Win, Ein> {
type Rout;
type Wout;
type Eout;
type Error;
// Required methods
fn handle_read(&mut self, msg: Rin) -> Result<(), Self::Error>;
fn poll_read(&mut self) -> Option<Self::Rout>;
fn handle_write(&mut self, msg: Win) -> Result<(), Self::Error>;
fn poll_write(&mut self) -> Option<Self::Wout>;
// Provided methods
fn handle_event(&mut self, _evt: Ein) -> Result<(), Self::Error> { ... }
fn poll_event(&mut self) -> Option<Self::Eout> { ... }
fn handle_timeout(&mut self, _now: Instant) -> Result<(), Self::Error> { ... }
fn poll_timeout(&mut self) -> Option<Instant> { ... }
fn close(&mut self) -> Result<(), Self::Error> { ... }
}Expand description
Protocol trait for Sans-IO protocol implementations A Sans-IO protocol abstraction.
The Protocol trait provides a simplified interface for building network protocols
that are fully decoupled from I/O operations.
§Type Parameters
Rin: Input read message typeWin: Input write message typeEin: Input event type
§Design Pattern
This trait follows a push-pull pattern:
- Push data/events into the protocol using
handle_*methods - Pull results from the protocol using
poll_*methods
This allows the protocol logic to be completely independent of I/O, making it easy to test and reuse in different contexts.
§Example
See the module-level documentation for a complete example.
Required Associated Types§
Sourcetype Rout
type Rout
Output read message type
This is the type of messages produced after processing inbound data.
Required Methods§
Sourcefn handle_read(&mut self, msg: Rin) -> Result<(), Self::Error>
fn handle_read(&mut self, msg: Rin) -> Result<(), Self::Error>
Handle an incoming read message.
Processes an inbound message. The result can be polled later via poll_read.
§Parameters
msg: The incoming message to process
§Returns
Ok(())if the message was successfully queued for processingErr(Self::Error)if processing failed
§Example
protocol.handle_read(incoming_data)?;
// Later...
if let Some(processed) = protocol.poll_read() {
// Handle processed data
}Sourcefn poll_read(&mut self) -> Option<Self::Rout>
fn poll_read(&mut self) -> Option<Self::Rout>
Poll for a processed read message.
Returns the next processed inbound message, if any.
Call this after handle_read to retrieve processed results.
§Returns
Some(Rout)if a processed message is availableNoneif no processed messages are ready
Sourcefn handle_write(&mut self, msg: Win) -> Result<(), Self::Error>
fn handle_write(&mut self, msg: Win) -> Result<(), Self::Error>
Handle an outgoing write message.
Processes an outbound message for transmission. The result can be polled
later via poll_write.
§Parameters
msg: The outgoing message to process
§Returns
Ok(())if the message was successfully queuedErr(Self::Error)if processing failed
Sourcefn poll_write(&mut self) -> Option<Self::Wout>
fn poll_write(&mut self) -> Option<Self::Wout>
Poll for a processed write message.
Returns the next processed outbound message, if any.
Call this after handle_write to retrieve messages ready for transmission.
§Returns
Some(Wout)if a message is ready to sendNoneif no messages are ready
Provided Methods§
Sourcefn handle_event(&mut self, _evt: Ein) -> Result<(), Self::Error>
fn handle_event(&mut self, _evt: Ein) -> Result<(), Self::Error>
Sourcefn poll_event(&mut self) -> Option<Self::Eout>
fn poll_event(&mut self) -> Option<Self::Eout>
Poll for a generated event.
Returns the next event generated by the protocol, if any.
§Returns
Some(Eout)if an event was generatedNoneby default (override to implement custom events)
Sourcefn poll_timeout(&mut self) -> Option<Instant>
fn poll_timeout(&mut self) -> Option<Instant>
Poll for the next timeout deadline.
Returns when the protocol next wants to be called via handle_timeout.
§Returns
Some(Instant)if the protocol has a pending timeoutNoneif no timeout is needed (default)
§Example
if let Some(deadline) = protocol.poll_timeout() {
// Wait until deadline, then call protocol.handle_timeout(now)
}Implementations on Foreign Types§
Source§impl<P, Rin, Win, Ein> Protocol<Rin, Win, Ein> for &mut P
Blanket implementation for mutable references.
impl<P, Rin, Win, Ein> Protocol<Rin, Win, Ein> for &mut P
Blanket implementation for mutable references.
This allows protocols to be used through mutable references without requiring explicit dereferencing.
type Rout = <P as Protocol<Rin, Win, Ein>>::Rout
type Wout = <P as Protocol<Rin, Win, Ein>>::Wout
type Eout = <P as Protocol<Rin, Win, Ein>>::Eout
type Error = <P as Protocol<Rin, Win, Ein>>::Error
fn handle_read(&mut self, msg: Rin) -> Result<(), P::Error>
fn poll_read(&mut self) -> Option<P::Rout>
fn handle_write(&mut self, msg: Win) -> Result<(), P::Error>
fn poll_write(&mut self) -> Option<P::Wout>
fn handle_event(&mut self, evt: Ein) -> Result<(), P::Error>
fn poll_event(&mut self) -> Option<P::Eout>
fn handle_timeout(&mut self, now: Instant) -> Result<(), P::Error>
fn poll_timeout(&mut self) -> Option<Instant>
fn close(&mut self) -> Result<(), P::Error>
Source§impl<P, Rin, Win, Ein> Protocol<Rin, Win, Ein> for Box<P>
Blanket implementation for boxed protocols.
impl<P, Rin, Win, Ein> Protocol<Rin, Win, Ein> for Box<P>
Blanket implementation for boxed protocols.
This allows protocols to be used through Box<dyn Protocol> for dynamic dispatch.