diff --git a/web/src/service/tower_http_compat.rs b/web/src/service/tower_http_compat.rs index 57402ace..3842f2e0 100644 --- a/web/src/service/tower_http_compat.rs +++ b/web/src/service/tower_http_compat.rs @@ -1,10 +1,12 @@ -use std::{ +use core::{ cell::RefCell, convert::Infallible, pin::Pin, task::{ready, Context, Poll}, }; +use std::borrow::Cow; + use futures_core::stream::Stream; use http_body::{Body, Frame, SizeHint}; use pin_project_lite::pin_project; @@ -15,7 +17,8 @@ use xitca_http::{ use xitca_unsafe_collection::fake::FakeSend; use crate::{ - bytes::Buf, + body::ResponseBody, + bytes::{Buf, Bytes, BytesMut}, context::WebContext, http::{Request, RequestExt, Response, WebResponse}, service::{ready::ReadyService, Service}, @@ -138,7 +141,7 @@ pin_project! { } impl CompatResBody { - pub fn new(body: B) -> Self { + pub const fn new(body: B) -> Self { Self { body } } @@ -147,6 +150,31 @@ impl CompatResBody { } } +// useful shortcuts conversion for B type. +macro_rules! impl_from { + ($ty: ty) => { + impl From<$ty> for CompatResBody + where + B: From<$ty>, + { + fn from(body: $ty) -> Self { + Self::new(B::from(body)) + } + } + }; +} + +impl_from!(Bytes); +impl_from!(BytesMut); +impl_from!(&'static [u8]); +impl_from!(&'static str); +impl_from!(Box<[u8]>); +impl_from!(Vec); +impl_from!(String); +impl_from!(Box); +impl_from!(Cow<'static, str>); +impl_from!(ResponseBody); + impl Body for CompatResBody where B: Stream>,