pub struct Pipeline<R, W> { /* private fields */ }Expand description
Pipeline traits for inbound and outbound message processing A pipeline of handlers for processing protocol messages.
The Pipeline is the main abstraction for building protocol stacks in sansio.
It maintains an ordered chain of Handlers and routes messages bidirectionally
through them.
§Type Parameters
R: Input type at the bottom of the pipeline (from network/transport)W: Write type at the top of the pipeline (from application)
§Lifecycle
- Create:
Pipeline::new() - Build: Add handlers with
add_back()oradd_front() - Finalize: Call
finalize()to link handlers and wrap inRc - Use: Call methods from
InboundPipelineandOutboundPipelinetraits
§Example
use sansio::{Pipeline, Handler, Context, InboundPipeline};
// Create and build pipeline
let pipeline: Pipeline<String, String> = Pipeline::new();
pipeline.add_back(EchoHandler);
// Finalize before use
let pipeline = pipeline.finalize();
// Use the pipeline
pipeline.transport_active();
pipeline.handle_read("Hello".to_string());
if let Some(msg) = pipeline.poll_write() {
println!("Got: {}", msg);
}§Thread Safety
Pipelines use Rc (not Arc) and are not thread-safe. They are designed
for single-threaded I/O loops. For multi-threaded usage, create separate
pipelines per thread.
Implementations§
Source§impl<R: 'static, W: 'static> Pipeline<R, W>
impl<R: 'static, W: 'static> Pipeline<R, W>
Sourcepub fn add_back(&self, handler: impl Handler + 'static) -> &Self
pub fn add_back(&self, handler: impl Handler + 'static) -> &Self
Appends a handler at the end of the pipeline.
Handlers are processed in the order they are added:
- Inbound: first → last
- Outbound: last → first
Returns &self for method chaining.
§Parameters
handler: The handler to append
§Example
let pipeline: Pipeline<String, String> = Pipeline::new();
pipeline
.add_back(H1) // First handler
.add_back(H2); // Second handlerSourcepub fn add_front(&self, handler: impl Handler + 'static) -> &Self
pub fn add_front(&self, handler: impl Handler + 'static) -> &Self
Inserts a handler at the beginning of the pipeline.
The new handler will be the first to process inbound messages.
Returns &self for method chaining.
§Parameters
handler: The handler to prepend
§Example
let pipeline: Pipeline<String, String> = Pipeline::new();
pipeline.add_front(H1); // Will be first in chainSourcepub fn remove_back(&self) -> Result<&Self, Error>
pub fn remove_back(&self) -> Result<&Self, Error>
Sourcepub fn remove_front(&self) -> Result<&Self, Error>
pub fn remove_front(&self) -> Result<&Self, Error>
Removes the first handler from the pipeline.
§Returns
Ok(&self): Handler removed successfully (for chaining)Err(io::Error): Pipeline is empty
Sourcepub fn remove(&self, handler_name: &str) -> Result<&Self, Error>
pub fn remove(&self, handler_name: &str) -> Result<&Self, Error>
Removes a specific handler by name.
Searches for a handler with the given name and removes it.
§Parameters
handler_name: The name of the handler to remove (fromHandler::name)
§Returns
Ok(&self): Handler removed successfully (for chaining)Err(io::Error): Handler not found
§Example
let pipeline: Pipeline<String, String> = Pipeline::new();
pipeline.add_back(MyHandler);
let pipeline = pipeline.finalize();
pipeline.remove("MyHandler").unwrap();Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of handlers in the pipeline.
§Example
let pipeline: Pipeline<String, String> = Pipeline::new();
assert_eq!(pipeline.len(), 0);
pipeline.add_back(H1);
assert_eq!(pipeline.len(), 1);Sourcepub fn update(self: Rc<Self>) -> Rc<Self>
pub fn update(self: Rc<Self>) -> Rc<Self>
Updates an Rc-wrapped pipeline’s internal structure.
Called internally by finalize. You typically don’t need to call this directly.
Sourcepub fn finalize(self) -> Rc<Self>
pub fn finalize(self) -> Rc<Self>
Finalizes the pipeline, making it ready for use.
This method:
- Wraps the pipeline in
Rcfor shared ownership - Links handlers together internally
- Returns the finalized
Rc<Pipeline>
You must call this before using the pipeline.
§Example
let pipeline: Pipeline<String, String> = Pipeline::new();
pipeline.add_back(H1);
// Finalize before use
let pipeline = pipeline.finalize();
// Now ready to use
pipeline.handle_read("Hello".to_string());Trait Implementations§
Source§impl<R: 'static, W: 'static> InboundPipeline<R> for Pipeline<R, W>
impl<R: 'static, W: 'static> InboundPipeline<R> for Pipeline<R, W>
Source§fn transport_active(&self)
fn transport_active(&self)
Transport is active now, which means it is connected.
Source§fn transport_inactive(&self)
fn transport_inactive(&self)
Transport is inactive now, which means it is disconnected.
Source§fn handle_read(&self, msg: R)
fn handle_read(&self, msg: R)
Handles an incoming message.
Source§fn poll_write(&self) -> Option<R>
fn poll_write(&self) -> Option<R>
Polls an outgoing message
Source§fn handle_timeout(&self, now: Instant)
fn handle_timeout(&self, now: Instant)
Handles a timeout event.
Source§fn poll_timeout(&self, eto: &mut Instant)
fn poll_timeout(&self, eto: &mut Instant)
Polls earliest timeout (eto) in its inbound operations.
Source§fn handle_eof(&self)
fn handle_eof(&self)
Handles an EOF event.
Source§fn handle_error(&self, err: Box<dyn Error>)
fn handle_error(&self, err: Box<dyn Error>)
Handles an Error in one of its inbound operations.