sansio
An IO-free Rust networking framework that makes it easy to build protocols, application clients, and servers using the Sans-IO pattern.
Inspired by Netty and Wangle, sansio brings the power of pipeline-based protocol composition to Rust.
Features
- Sans-IO Design: Complete separation of protocol logic from I/O operations
- Pipeline Architecture: Chain handlers to build complex protocols from simple components
- Type-Safe Composition: Rust's type system ensures handlers connect correctly
- Runtime Agnostic: Works with tokio async runtime
- Testable: Test protocol logic without any I/O
Core Concepts
Pipeline Variants
Sansio provides three pipeline variants for different use cases:
Pipeline: Exclusive ownership (&mut self). Zero overhead, ideal for TCP where each connection owns its pipeline.RcPipeline: Shared ownership withRc. Methods take&self, perfect for UDP servers where one pipeline handles messages from multiple peers.ArcPipeline: Thread-safe shared ownership withArcandMutex. Use for multi-threaded servers.
use Pipeline;
let mut pipeline = new;
pipeline.add_back;
pipeline.add_back;
pipeline.add_back;
pipeline.finalize;
For shared pipelines:
use RcPipeline;
use Rc;
let mut pipeline = new;
// ... add handlers ...
pipeline.finalize;
let pipeline = new; // Now shareable
Handler
Each Handler processes messages flowing through the pipeline with four associated types:
Rin: Input type for inbound messagesRout: Output type for inbound messagesWin: Input type for outbound messagesWout: Output type for outbound messages
use ;
Protocol
The Protocol trait provides a simpler, fully Sans-IO alternative for protocol implementation:
use Protocol;
Quick Start
Add sansio to your Cargo.toml:
[]
= "0.0.5"
Build a simple pipeline:
use ;
// Define your handler
;
Using with a Runtime
sansio is purely Sans-IO without any runtime dependencies. For async runtime support, use the separate sansio-executor crate:
[]
= "0.0.7"
= "0.0.7" # For runtime support
use LocalExecutorBuilder;
See the sansio-executor documentation for more details on runtime support.
Documentation
Examples
See the examples crate for complete working examples:
- UDP chat server with pipeline-based protocol processing
- Handler composition and shared state management
Run an example:
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.