1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-26 15:07:42 +02:00

add responder for unit type

This commit is contained in:
Nikolay Kim
2019-03-02 09:05:07 -08:00
parent bc3c29c398
commit fdf3011837
4 changed files with 15 additions and 798 deletions

View File

@ -1,13 +1,11 @@
use actix_http::dev::ResponseBuilder;
use actix_http::http::StatusCode;
use actix_http::{Error, Response};
use actix_http::{dev::ResponseBuilder, http::StatusCode, Error, Response};
use bytes::{Bytes, BytesMut};
use futures::future::{err, ok, Either as EitherFuture, FutureResult};
use futures::{Future, Poll};
use crate::request::HttpRequest;
/// Trait implemented by types that generate http responses.
/// Trait implemented by types that can be converted to a http response.
///
/// Types that implement this trait can be used as the return type of a handler.
pub trait Responder {
@ -72,6 +70,15 @@ impl Responder for ResponseBuilder {
}
}
impl Responder for () {
type Error = Error;
type Future = FutureResult<Response, Error>;
fn respond_to(self, _: &HttpRequest) -> Self::Future {
ok(Response::build(StatusCode::OK).finish())
}
}
impl Responder for &'static str {
type Error = Error;
type Future = FutureResult<Response, Error>;
@ -167,12 +174,7 @@ impl Responder for BytesMut {
/// # fn is_a_variant() -> bool { true }
/// # fn main() {}
/// ```
pub enum Either<A, B> {
/// First branch of the type
A(A),
/// Second branch of the type
B(B),
}
pub type Either<A, B> = either::Either<A, B>;
impl<A, B> Responder for Either<A, B>
where
@ -184,8 +186,8 @@ where
fn respond_to(self, req: &HttpRequest) -> Self::Future {
match self {
Either::A(a) => EitherResponder::A(a.respond_to(req)),
Either::B(b) => EitherResponder::B(b.respond_to(req)),
either::Either::Left(a) => EitherResponder::A(a.respond_to(req)),
either::Either::Right(b) => EitherResponder::B(b.respond_to(req)),
}
}
}