expression

Unify different HTTP body types

Boxing erases the concrete body type while preserving its data and error types. This permits branches that return Full or Empty to share one return type. It does not collect the body or automatically convert errors. In-memory fixture; no external connection. Expected stdout (not executed): 0 3 Common mistake: Assuming boxed bodies must have identical concrete implementations or that boxing buffers all frames.

Formal Syntax & Structure

rust syntaxrust
body.boxed() -> BoxBody<Data, Error>

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use hyper::body::Bytes;    use http_body_util::{Full, Empty, BodyExt};    for has_data in [false, true] {        let body = if has_data { Full::new(Bytes::from_static(b"yes")).boxed() }            else { Empty::<Bytes>::new().boxed() };        println!("{}", body.collect().await.unwrap().to_bytes().len());    }}
Rust Reference