From 930e2ab7c13f9399c7177fb22c1a7497c7fbea2f Mon Sep 17 00:00:00 2001 From: wayne Date: Fri, 29 Sep 2023 00:52:26 -0500 Subject: [PATCH] axum::body no longer pulls in axum::body::stream_body (#2245) Signed-off-by: wayne warren --- axum/src/body/stream_body.rs | 149 ----------------------------------- 1 file changed, 149 deletions(-) delete mode 100644 axum/src/body/stream_body.rs diff --git a/axum/src/body/stream_body.rs b/axum/src/body/stream_body.rs deleted file mode 100644 index 933d10511d..0000000000 --- a/axum/src/body/stream_body.rs +++ /dev/null @@ -1,149 +0,0 @@ -use crate::{ - body::{Bytes, HttpBody}, - response::{IntoResponse, Response}, - BoxError, Error, -}; -use axum_core::body::Body; -use futures_util::{ - ready, - stream::{self, TryStream}, -}; -use http::HeaderMap; -use pin_project_lite::pin_project; -use std::{ - fmt, - pin::Pin, - task::{Context, Poll}, -}; -use sync_wrapper::SyncWrapper; - -pin_project! { - /// An [`http_body::Body`] created from a [`Stream`]. - /// - /// The purpose of this type is to be used in responses. If you want to - /// extract the request body as a stream consider using - /// [`Body`](crate::body::Body). - /// - /// # Example - /// - /// ``` - /// use axum::{ - /// Router, - /// routing::get, - /// body::StreamBody, - /// response::IntoResponse, - /// }; - /// use futures_util::stream::{self, Stream}; - /// use std::io; - /// - /// async fn handler() -> StreamBody>> { - /// let chunks: Vec> = vec![ - /// Ok("Hello,"), - /// Ok(" "), - /// Ok("world!"), - /// ]; - /// let stream = stream::iter(chunks); - /// StreamBody::new(stream) - /// } - /// - /// let app = Router::new().route("/", get(handler)); - /// # let _: Router = app; - /// ``` - /// - /// [`Stream`]: futures_util::stream::Stream - #[must_use] - pub struct StreamBody { - #[pin] - stream: SyncWrapper, - } -} - -impl From for StreamBody -where - S: TryStream + Send + 'static, - S::Ok: Into, - S::Error: Into, -{ - fn from(stream: S) -> Self { - Self::new(stream) - } -} - -impl StreamBody { - /// Create a new `StreamBody` from a [`Stream`]. - /// - /// [`Stream`]: futures_util::stream::Stream - pub fn new(stream: S) -> Self - where - S: TryStream + Send + 'static, - S::Ok: Into, - S::Error: Into, - { - Self { - stream: SyncWrapper::new(stream), - } - } -} - -impl IntoResponse for StreamBody -where - S: TryStream + Send + 'static, - S::Ok: Into, - S::Error: Into, -{ - fn into_response(self) -> Response { - Response::new(Body::new(self)) - } -} - -impl Default for StreamBody>> { - fn default() -> Self { - Self::new(stream::empty()) - } -} - -impl fmt::Debug for StreamBody { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple("StreamBody").finish() - } -} - -impl HttpBody for StreamBody -where - S: TryStream, - S::Ok: Into, - S::Error: Into, -{ - type Data = Bytes; - type Error = Error; - - fn poll_data( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll>> { - let stream = self.project().stream.get_pin_mut(); - match ready!(stream.try_poll_next(cx)) { - Some(Ok(chunk)) => Poll::Ready(Some(Ok(chunk.into()))), - Some(Err(err)) => Poll::Ready(Some(Err(Error::new(err)))), - None => Poll::Ready(None), - } - } - - fn poll_trailers( - self: Pin<&mut Self>, - _cx: &mut Context<'_>, - ) -> Poll, Self::Error>> { - Poll::Ready(Ok(None)) - } -} - -#[test] -fn stream_body_traits() { - use futures_util::stream::Empty; - - type EmptyStream = StreamBody>>; - - crate::test_helpers::assert_send::(); - crate::test_helpers::assert_sync::(); - crate::test_helpers::assert_unpin::(); -}