Skip to main content

BodyExtractExt

Trait BodyExtractExt 

pub trait BodyExtractExt: Sealed {
    // Required methods
    fn try_into_json<T>(
        self,
    ) -> impl Future<Output = Result<T, Box<dyn Error + Sync + Send>>> + Send
       where T: DeserializeOwned + Send + 'static;
    fn try_into_json_streaming<T>(
        self,
    ) -> impl Future<Output = Result<T, Box<dyn Error + Sync + Send>>> + Send
       where T: DeserializeOwned + Send + 'static;
    fn try_into_string(
        self,
    ) -> impl Future<Output = Result<String, Box<dyn Error + Sync + Send>>> + Send;
}
Available on crate feature http only.
Expand description

An extension trait for StreamingBody that provides methods to extract data from it.

Required Methods§

fn try_into_json<T>( self, ) -> impl Future<Output = Result<T, Box<dyn Error + Sync + Send>>> + Send
where T: DeserializeOwned + Send + 'static,

Try to deserialize the (contained) body as a JSON object.

Buffers the entire body in memory before deserializing. For large bodies prefer BodyExtractExt::try_into_json_streaming.

fn try_into_json_streaming<T>( self, ) -> impl Future<Output = Result<T, Box<dyn Error + Sync + Send>>> + Send
where T: DeserializeOwned + Send + 'static,

Try to deserialize the (contained) body as a JSON object, streaming bytes from the body directly into the JSON parser instead of buffering the whole body first.

Preferable to BodyExtractExt::try_into_json for large bodies where peak memory matters.

§Note

Internally this runs serde_json::from_reader inside a [tokio::task::spawn_blocking] task using SyncIoBridge to bridge the async body to serde’s synchronous io::Read interface. The thread hop is unavoidable today because serde::Deserialize is a pull-based, synchronous trait and no production-ready async-first JSON crate currently ships a drop-in serde::Deserialize integration. Contributions that remove this hop — e.g. by building a serde::Deserializer on top of an async event-based JSON parser — are welcome.

fn try_into_string( self, ) -> impl Future<Output = Result<String, Box<dyn Error + Sync + Send>>> + Send

Try to turn the (contained) body in an utf-8 string.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

§

impl<B> BodyExtractExt for B
where B: Into<Body> + Send + 'static,

§

impl<Body> BodyExtractExt for Request<Body>
where Body: Body + Send + 'static, <Body as Body>::Data: Send + 'static, <Body as Body>::Error: Into<Box<dyn Error + Sync + Send>>,

§

impl<Body> BodyExtractExt for Response<Body>
where Body: Body + Send + 'static, <Body as Body>::Data: Send + 'static, <Body as Body>::Error: Into<Box<dyn Error + Sync + Send>>,