expression

Decode complete text frames

A codec separates meaningful records from a byte buffer. The decoder returns a line only when it has enough input. The limit applies to decoding, not to all memory used by a connection. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): red true blue Common mistake: Assuming every read contains exactly one complete line.

Formal Syntax & Structure

rust syntaxrust
LinesCodec::new_with_max_length(limit); Decoder::decode

Working Example

Example Snippetrust
fn main() {    use bytes::BytesMut;    use tokio_util::codec::{Decoder, LinesCodec};    let mut c = LinesCodec::new_with_max_length(16);    let mut b = BytesMut::from(&b"red\nblu"[..]);    println!("{}", c.decode(&mut b).unwrap().unwrap());    println!("{}", c.decode(&mut b).unwrap().is_none());    b.extend_from_slice(b"e\n");    println!("{}", c.decode(&mut b).unwrap().unwrap());}
Rust Reference