use bytes::Buf; use futures_core::{ready, stream::Stream}; use http_body::{Body, Frame}; use pin_project_lite::pin_project; use std::{
pin::Pin,
task::{Context, Poll},
};
pin_project! { /// A body created from a [`Stream`]. #[derive(Clone, Copy, Debug)] pubstruct StreamBody<S> { #[pin]
stream: S,
}
}
impl<S> StreamBody<S> { /// Create a new `StreamBody`. pubfn new(stream: S) -> Self { Self { stream }
}
}
impl<S, D, E> Body for StreamBody<S> where
S: Stream<Item = Result<Frame<D>, E>>,
D: Buf,
{ type Data = D; type Error = E;
#[cfg(test)] mod tests { usecrate::{BodyExt, BodyStream, StreamBody}; use bytes::Bytes; use futures_util::StreamExt; use http_body::Frame; use std::convert::Infallible;
#[tokio::test] asyncfn body_from_stream() { let chunks: Vec<Result<_, Infallible>> = vec![
Ok(Frame::data(Bytes::from(vec![1]))),
Ok(Frame::data(Bytes::from(vec![2]))),
Ok(Frame::data(Bytes::from(vec![3]))),
]; let stream = futures_util::stream::iter(chunks); letmut body = StreamBody::new(stream);
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.