pub struct AsyncReadBody { /* private fields */ }
Available on crate feature
async-read-body
only.Expand description
An HttpBody
created from an AsyncRead
.
§Example
AsyncReadBody
can be used to stream the contents of a file:
use axum::{
Router,
routing::get,
http::{StatusCode, header::CONTENT_TYPE},
response::{Response, IntoResponse},
};
use axum_extra::body::AsyncReadBody;
use tokio::fs::File;
async fn cargo_toml() -> Result<Response, (StatusCode, String)> {
let file = File::open("Cargo.toml")
.await
.map_err(|err| {
(StatusCode::NOT_FOUND, format!("File not found: {err}"))
})?;
let headers = [(CONTENT_TYPE, "text/x-toml")];
let body = AsyncReadBody::new(file);
Ok((headers, body).into_response())
}
let app = Router::new().route("/Cargo.toml", get(cargo_toml));
Implementations§
Trait Implementations§
Source§impl Body for AsyncReadBody
impl Body for AsyncReadBody
Source§impl Debug for AsyncReadBody
impl Debug for AsyncReadBody
Source§impl IntoResponse for AsyncReadBody
impl IntoResponse for AsyncReadBody
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
impl<'__pin> Unpin for AsyncReadBodywhere
PinnedFieldsOf<__Origin<'__pin>>: Unpin,
Auto Trait Implementations§
impl Freeze for AsyncReadBody
impl !RefUnwindSafe for AsyncReadBody
impl Send for AsyncReadBody
impl !Sync for AsyncReadBody
impl !UnwindSafe for AsyncReadBody
Blanket Implementations§
Source§impl<T> BodyExt for T
impl<T> BodyExt for T
Source§fn frame(&mut self) -> Frame<'_, Self>where
Self: Unpin,
fn frame(&mut self) -> Frame<'_, Self>where
Self: Unpin,
Returns a future that resolves to the next
Frame
, if any.Source§fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
Maps this body’s frame to a different kind.
Source§fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
Maps this body’s error value to a different value.
Source§fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
Turn this body into a boxed trait object that is !Sync.
Source§fn collect(self) -> Collect<Self>where
Self: Sized,
fn collect(self) -> Collect<Self>where
Self: Sized,
Turn this body into
Collected
body which will collect all the DATA frames
and trailers.Source§fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
Add trailers to the body. Read more
Source§fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
Turn this body into
BodyDataStream
.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more