Implement Responder for () converting it into OK/200.

This commit is contained in:
boxdot 2019-02-16 09:06:24 +01:00
parent 0059a55dfb
commit 6a23d82dce
1 changed files with 24 additions and 6 deletions

View File

@ -32,7 +32,8 @@ pub trait Responder {
/// Convert itself to `AsyncResult` or `Error`. /// Convert itself to `AsyncResult` or `Error`.
fn respond_to<S: 'static>( fn respond_to<S: 'static>(
self, req: &HttpRequest<S>, self,
req: &HttpRequest<S>,
) -> Result<Self::Item, Self::Error>; ) -> Result<Self::Item, Self::Error>;
} }
@ -103,7 +104,8 @@ where
type Error = Error; type Error = Error;
fn respond_to<S: 'static>( fn respond_to<S: 'static>(
self, req: &HttpRequest<S>, self,
req: &HttpRequest<S>,
) -> Result<AsyncResult<HttpResponse>, Error> { ) -> Result<AsyncResult<HttpResponse>, Error> {
match self { match self {
Either::A(a) => match a.respond_to(req) { Either::A(a) => match a.respond_to(req) {
@ -142,7 +144,8 @@ where
type Error = Error; type Error = Error;
fn respond_to<S: 'static>( fn respond_to<S: 'static>(
self, req: &HttpRequest<S>, self,
req: &HttpRequest<S>,
) -> Result<AsyncResult<HttpResponse>, Error> { ) -> Result<AsyncResult<HttpResponse>, Error> {
match self { match self {
Some(t) => match t.respond_to(req) { Some(t) => match t.respond_to(req) {
@ -154,6 +157,18 @@ where
} }
} }
impl Responder for () {
type Item = HttpResponse;
type Error = Error;
fn respond_to<S: 'static>(
self,
req: &HttpRequest<S>,
) -> Result<Self::Item, Self::Error> {
Ok(req.build_response(StatusCode::OK).finish())
}
}
/// Convenience trait that converts `Future` object to a `Boxed` future /// Convenience trait that converts `Future` object to a `Boxed` future
/// ///
/// For example loading json from request's body is async operation. /// For example loading json from request's body is async operation.
@ -293,7 +308,8 @@ impl Responder for AsyncResult<HttpResponse> {
type Error = Error; type Error = Error;
fn respond_to<S>( fn respond_to<S>(
self, _: &HttpRequest<S>, self,
_: &HttpRequest<S>,
) -> Result<AsyncResult<HttpResponse>, Error> { ) -> Result<AsyncResult<HttpResponse>, Error> {
Ok(self) Ok(self)
} }
@ -305,7 +321,8 @@ impl Responder for HttpResponse {
#[inline] #[inline]
fn respond_to<S>( fn respond_to<S>(
self, _: &HttpRequest<S>, self,
_: &HttpRequest<S>,
) -> Result<AsyncResult<HttpResponse>, Error> { ) -> Result<AsyncResult<HttpResponse>, Error> {
Ok(AsyncResult(Some(AsyncResultItem::Ok(self)))) Ok(AsyncResult(Some(AsyncResultItem::Ok(self))))
} }
@ -389,7 +406,8 @@ where
#[inline] #[inline]
fn respond_to<S: 'static>( fn respond_to<S: 'static>(
self, req: &HttpRequest<S>, self,
req: &HttpRequest<S>,
) -> Result<AsyncResult<HttpResponse>, Error> { ) -> Result<AsyncResult<HttpResponse>, Error> {
let req = req.clone(); let req = req.clone();
let fut = self let fut = self