expression

Framed bidirectional in-memory transport

Use a shared codec for reading and writing records over a bidirectional transport. The example uses a small duplex buffer, not a live socket, and exchanges two short records without a live connection. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): pong Common mistake: Assuming a real TCP peer always reads before the sender’s buffer fills.

Formal Syntax & Structure

rust syntaxrust
Framed::new(io, codec)

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use futures_util::{SinkExt, StreamExt};    use tokio_util::codec::{Framed, LinesCodec};    let (a, b) = tokio::io::duplex(64);    let mut left = Framed::new(a, LinesCodec::new_with_max_length(16));    let mut right = Framed::new(b, LinesCodec::new_with_max_length(16));    left.send("ping").await.unwrap();    let msg = right.next().await.unwrap().unwrap();    right.send(if msg == "ping" { "pong" } else { "bad" }).await.unwrap();    println!("{}", left.next().await.unwrap().unwrap());}
Rust Reference