Struct axum_extra::body::AsyncReadBody
source · [−]pub struct AsyncReadBody<R> { /* private fields */ }
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
sourceimpl<R> Body for AsyncReadBody<R> where
R: AsyncRead + Send + 'static,
impl<R> Body for AsyncReadBody<R> where
R: AsyncRead + Send + 'static,
type Error = Error
type Error = Error
The error type this Body
might generate.
sourcefn poll_data(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Option<Result<Self::Data, Self::Error>>>
fn poll_data(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Option<Result<Self::Data, Self::Error>>>
Attempt to pull out the next data buffer of this stream.
sourcefn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context<'_>
) -> Poll<Result<Option<HeaderMap>, Self::Error>>
fn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context<'_>
) -> Poll<Result<Option<HeaderMap>, Self::Error>>
Poll for an optional single HeaderMap
of trailers. Read more
sourcefn is_end_stream(&self) -> bool
fn is_end_stream(&self) -> bool
Returns true
when the end of stream has been reached. Read more
sourcefn size_hint(&self) -> SizeHint
fn size_hint(&self) -> SizeHint
Returns the bounds on the remaining length of the stream. Read more
sourcefn data(&mut self) -> Data<'_, Self> where
Self: Unpin,
fn data(&mut self) -> Data<'_, Self> where
Self: Unpin,
Returns future that resolves to next data chunk, if any.
sourcefn trailers(&mut self) -> Trailers<'_, Self> where
Self: Unpin,
fn trailers(&mut self) -> Trailers<'_, Self> where
Self: Unpin,
Returns future that resolves to trailers, if any.
sourcefn map_data<F, B>(self, f: F) -> MapData<Self, F> where
F: FnMut(Self::Data) -> B,
B: Buf,
fn map_data<F, B>(self, f: F) -> MapData<Self, F> where
F: FnMut(Self::Data) -> B,
B: Buf,
Maps this body’s data value to a different value.
sourcefn map_err<F, E>(self, f: F) -> MapErr<Self, F> where
F: FnMut(Self::Error) -> E,
fn map_err<F, E>(self, f: F) -> MapErr<Self, F> where
F: FnMut(Self::Error) -> E,
Maps this body’s error value to a different value.
sourcefn boxed(self) -> BoxBody<Self::Data, Self::Error> where
Self: 'static + Send + Sync,
fn boxed(self) -> BoxBody<Self::Data, Self::Error> where
Self: 'static + Send + Sync,
Turn this body into a boxed trait object.
sourcefn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error> where
Self: 'static + Send,
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error> where
Self: 'static + Send,
Turn this body into a boxed trait object that is !Sync.
sourceimpl<R: Debug> Debug for AsyncReadBody<R>
impl<R: Debug> Debug for AsyncReadBody<R>
sourceimpl<R> IntoResponse for AsyncReadBody<R> where
R: AsyncRead + Send + 'static,
impl<R> IntoResponse for AsyncReadBody<R> where
R: AsyncRead + Send + 'static,
sourcefn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
impl<'__pin, R> Unpin for AsyncReadBody<R> where
__Origin<'__pin, R>: Unpin,
Auto Trait Implementations
impl<R> RefUnwindSafe for AsyncReadBody<R> where
R: RefUnwindSafe,
impl<R> Send for AsyncReadBody<R> where
R: Send,
impl<R> Sync for AsyncReadBody<R>
impl<R> UnwindSafe for AsyncReadBody<R> where
R: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more