Context

Struct Context 

Source
pub struct Context<Rin, Rout, Win, Wout> { /* private fields */ }
Expand description

Handler and context types for building protocol pipelines Enables a Handler to interact with the pipeline.

The Context object is passed to each handler method and provides the API for forwarding messages, polling data, and propagating events through the pipeline.

§Type Parameters

  • Rin: Read input message type (what the current handler receives)
  • Rout: Read output message type (what the current handler produces)
  • Win: Write input message type (what the current handler receives for writing)
  • Wout: Write output message type (what the current handler produces for writing)

§Responsibilities

The context provides two primary functions:

  1. Forward Operations: Push data/events down the pipeline

    • fire_handle_read(): Forward processed inbound messages
    • fire_transport_active(): Propagate connection established event
    • fire_handle_timeout(): Propagate timeout events
  2. Pull Operations: Pull data from the next handler

    • fire_poll_write(): Poll outbound messages from next handler
    • fire_poll_timeout(): Poll timeout requirements from next handler

§Example

use sansio::{Handler, Context};

struct MyHandler;

impl Handler for MyHandler {
    type Rin = String;
    type Rout = String;
    type Win = String;
    type Wout = String;

    fn name(&self) -> &str {
        "MyHandler"
    }

    fn handle_read(&mut self, ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>, msg: Self::Rin) {
        // Use context to forward message
        ctx.fire_handle_read(msg);
    }

    fn poll_write(&mut self, ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>) -> Option<Self::Wout> {
        // Use context to poll next handler
        ctx.fire_poll_write()
    }
}

Implementations§

Source§

impl<Rin: 'static, Rout: 'static, Win: 'static, Wout: 'static> Context<Rin, Rout, Win, Wout>

Source

pub fn new(name: &str) -> Self

Creates a new Context.

Typically called internally by the pipeline when adding a handler. You rarely need to create a Context manually.

§Parameters
  • name: The name of the handler this context belongs to
Source

pub fn fire_transport_active(&self)

Propagates transport active event to the next handler.

Call this to forward the “connection established” event through the pipeline. This should be called from your Handler::transport_active implementation unless you explicitly want to stop event propagation.

Source

pub fn fire_transport_inactive(&self)

Propagates transport inactive event to the next handler.

Call this to forward the “connection closed” event through the pipeline. This should be called from your Handler::transport_inactive implementation unless you explicitly want to stop event propagation.

Source

pub fn fire_handle_read(&self, msg: Rout)

Forwards an inbound message to the next handler.

After processing a message in Handler::handle_read, call this method to pass the transformed message (Rout) to the next handler in the pipeline.

§Parameters
  • msg: The processed message to forward
§Example
fn handle_read(&mut self, ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>, msg: Self::Rin) {
    // Decode bytes to string
    if let Ok(decoded) = String::from_utf8(msg) {
        // Forward decoded message
        ctx.fire_handle_read(decoded);
    }
}
Source

pub fn fire_poll_write(&self) -> Option<Win>

Polls the next handler for an outbound message.

Call this from Handler::poll_write to retrieve messages from the next handler in the pipeline. Returns Some(Win) if the next handler has data to send, or None otherwise.

§Returns
  • Some(Win): A message from the next handler
  • None: No message available
§Example
fn poll_write(&mut self, ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>) -> Option<Self::Wout> {
    // Poll next handler first
    if let Some(msg) = ctx.fire_poll_write() {
        // Encode string to bytes
        return Some(msg.into_bytes());
    }
    // Then return our own queued data
    self.queue.pop_front()
}
Source

pub fn fire_handle_timeout(&self, now: Instant)

Propagates timeout event to the next handler.

Call this from Handler::handle_timeout to forward the timeout event through the pipeline.

§Parameters
  • now: The current timestamp
Source

pub fn fire_poll_timeout(&self, eto: &mut Instant)

Polls the next handler for its timeout deadline.

Call this from Handler::poll_timeout to allow the next handler to update the earliest timeout deadline.

§Parameters
  • eto: Mutable reference to the earliest timeout. The next handler may update this.
Source

pub fn fire_handle_eof(&self)

Propagates EOF event to the next handler.

Call this from Handler::handle_eof to forward the end-of-file event through the pipeline.

Source

pub fn fire_handle_error(&self, err: Box<dyn Error>)

Propagates error event to the next handler.

Call this from Handler::handle_error to forward the error through the pipeline.

§Parameters
  • err: The error to propagate
Source

pub fn fire_handle_close(&self)

Propagates close event to the next handler.

Call this from Handler::handle_close to forward the close request through the pipeline.

Auto Trait Implementations§

§

impl<Rin, Rout, Win, Wout> Freeze for Context<Rin, Rout, Win, Wout>

§

impl<Rin, Rout, Win, Wout> !RefUnwindSafe for Context<Rin, Rout, Win, Wout>

§

impl<Rin, Rout, Win, Wout> !Send for Context<Rin, Rout, Win, Wout>

§

impl<Rin, Rout, Win, Wout> !Sync for Context<Rin, Rout, Win, Wout>

§

impl<Rin, Rout, Win, Wout> Unpin for Context<Rin, Rout, Win, Wout>
where Rin: Unpin, Rout: Unpin, Win: Unpin, Wout: Unpin,

§

impl<Rin, Rout, Win, Wout> !UnwindSafe for Context<Rin, Rout, Win, Wout>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.