use std::{
convert::Infallible,
pin::Pin,
task::{Context, Poll},
};
use bytes::{Buf, Bytes}; use http::HeaderMap; use http_body::{Body, Frame};
usecrate::util::BufList;
/// A collected body produced by [`BodyExt::collect`] which collects all the DATA frames /// and trailers. /// /// [`BodyExt::collect`]: crate::BodyExt::collect #[derive(Debug)] pubstruct Collected<B> {
bufs: BufList<B>,
trailers: Option<HeaderMap>,
}
impl<B: Buf> Collected<B> { /// If there is a trailers frame buffered, returns a reference to it. /// /// Returns `None` if the body contained no trailers. pubfn trailers(&self) -> Option<&HeaderMap> { self.trailers.as_ref()
}
/// Aggregate this buffered into a [`Buf`]. pubfn aggregate(self) -> impl Buf { self.bufs
}
/// Convert this body into a [`Bytes`]. pubfn to_bytes(mutself) -> Bytes { self.bufs.copy_to_bytes(self.bufs.remaining())
}
pub(crate) fn push_frame(&mutself, frame: Frame<B>) { let frame = match frame.into_data() {
Ok(data) => { // Only push this frame if it has some data in it, to avoid crashing on // `BufList::push`. if data.has_remaining() { self.bufs.push(data);
} return;
}
Err(frame) => frame,
};
#[tokio::test] asyncfn segmented_body() { let bufs = [&b"hello"[..], &b"world"[..], &b"!"[..]]; let body = StreamBody::new(stream::iter(bufs.map(Frame::data).map(Ok::<_, Infallible>)));
#[tokio::test] asyncfn delayed_segments() { let one = stream::once(async { Ok::<_, Infallible>(Frame::data(&b"hello "[..])) }); let two = stream::once(async { // a yield just so its not ready immediately
tokio::task::yield_now().await;
Ok::<_, Infallible>(Frame::data(&b"world!"[..]))
}); let stream = futures_util::StreamExt::chain(one, two);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.