Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add often used from trait impl for CompatResBody. #892

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions web/src/service/tower_http_compat.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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},
Expand Down Expand Up @@ -138,7 +141,7 @@ pin_project! {
}

impl<B> CompatResBody<B> {
pub fn new(body: B) -> Self {
pub const fn new(body: B) -> Self {
Self { body }
}

Expand All @@ -147,6 +150,31 @@ impl<B> CompatResBody<B> {
}
}

// useful shortcuts conversion for B type.
macro_rules! impl_from {
($ty: ty) => {
impl<B> From<$ty> for CompatResBody<B>
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<u8>);
impl_from!(String);
impl_from!(Box<str>);
impl_from!(Cow<'static, str>);
impl_from!(ResponseBody);

impl<B, T, E> Body for CompatResBody<B>
where
B: Stream<Item = Result<T, E>>,
Expand Down
Loading