Skip to content

Commit

Permalink
add shortcut for respond with body type.
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeshadow committed Feb 20, 2024
1 parent 2d1178a commit cd88ff5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 4 additions & 2 deletions web/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ mod nightly {

}

impl<B> From<B> for AsyncBody<B>
impl<B, T, E> From<B> for AsyncBody<B>
where
B: AsyncIterator,
B: AsyncIterator<Item = Result<T, E>> + 'static,
E: Into<BodyError>,
Bytes: From<T>,
{
fn from(inner: B) -> Self {
Self { inner }
Expand Down
17 changes: 16 additions & 1 deletion web/src/handler/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
body::ResponseBody,
context::WebContext,
error::{Error, ErrorStatus},
http::{Method, RequestExt, StatusCode, WebRequest, WebResponse},
http::{IntoResponse, Method, RequestExt, StatusCode, WebRequest, WebResponse},
};

use super::{FromRequest, Responder};
Expand Down Expand Up @@ -163,6 +163,21 @@ impl<'r, C, B, ResB> Responder<WebContext<'r, C, B>> for WebResponse<ResB> {
}
}

impl<'r, C, B, ResB> Responder<WebContext<'r, C, B>> for ResponseBody<ResB> {
type Response = WebResponse<ResponseBody<ResB>>;
type Error = Error<C>;

#[inline]
async fn respond(self, ctx: WebContext<'r, C, B>) -> Result<Self::Response, Self::Error> {
Ok(ctx.req.as_response(self))
}

#[inline]
fn map(self, res: Self::Response) -> Result<Self::Response, Self::Error> {
Ok(res.map(|_| self))
}
}

impl<'r, C, B> Responder<WebContext<'r, C, B>> for Error<C> {
type Response = WebResponse;
type Error = Error<C>;
Expand Down

0 comments on commit cd88ff5

Please sign in to comment.