pub trait IntoResponse<B, ResB> {
// Required method
fn into_response(self, body: B) -> Response<ResB>;
// Provided method
fn as_response(&mut self, body: B) -> Response<ResB>
where Self: Default { ... }
}
Expand description
helper trait for converting a Request to Response. This is a memory optimization for re-use heap allocation and pass down the context data inside Extensions from request to response.
§Example
// arbitrary typed state inserted into request type.
#[derive(Clone)]
struct Foo;
fn into_response(mut req: Request<()>) -> Response<()> {
req.extensions_mut().insert(Foo); // insert Foo to request's extensions type map.
// convert request into response in place with the same memory allocation.
use xitca_http::http::IntoResponse;
let res = req.into_response(());
// the memory is re-used so Foo type is accessible from response's extensions type map.
assert!(res.extensions().get::<Foo>().is_some());
res
}