pub enum Either<L, R> {
Left(L),
Right(R),
}
Expand description
Combines two extractor or responder types into a single type.
§Extractor
Provides a mechanism for trying two extractors, a primary and a fallback. Useful for “polymorphic payloads” where, for example, a form might be JSON or URL encoded.
It is important to note that this extractor, by necessity, buffers the entire request payload
as part of its implementation. Though, it does respect any PayloadConfig
maximum size limits.
use actix_web::{post, web, Either};
use serde::Deserialize;
#[derive(Deserialize)]
struct Info {
name: String,
}
// handler that accepts form as JSON or form-urlencoded.
#[post("/")]
async fn index(form: Either<web::Json<Info>, web::Form<Info>>) -> String {
let name: String = match form {
Either::Left(json) => json.name.to_owned(),
Either::Right(form) => form.name.to_owned(),
};
format!("Welcome {}!", name)
}
§Responder
It may be desirable to use a concrete type for a response with multiple branches. As long as
both types implement Responder
, so will the Either
type, enabling it to be used as a
handler’s return type.
All properties of a response are determined by the Responder branch returned.
use actix_web::{get, Either, Error, HttpResponse};
#[get("/")]
async fn index() -> Either<&'static str, Result<HttpResponse, Error>> {
if 1 == 2 {
// respond with Left variant
Either::Left("Bad data")
} else {
// respond with Right variant
Either::Right(
Ok(HttpResponse::Ok()
.content_type(mime::TEXT_HTML)
.body("<p>Hello!</p>"))
)
}
}
Variants§
Implementations§
Trait Implementations§
source§impl<L, R> FromRequest for Either<L, R>where
L: FromRequest + 'static,
R: FromRequest + 'static,
impl<L, R> FromRequest for Either<L, R>where
L: FromRequest + 'static,
R: FromRequest + 'static,
See here for example of usage as an extractor.
§type Error = EitherExtractError<<L as FromRequest>::Error, <R as FromRequest>::Error>
type Error = EitherExtractError<<L as FromRequest>::Error, <R as FromRequest>::Error>
source§fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
Self
from request parts asynchronously.source§impl<L: PartialEq, R: PartialEq> PartialEq for Either<L, R>
impl<L: PartialEq, R: PartialEq> PartialEq for Either<L, R>
source§impl<L, R> Responder for Either<L, R>
impl<L, R> Responder for Either<L, R>
See here for example of usage as a handler return type.
type Body = EitherBody<<L as Responder>::Body, <R as Responder>::Body>
source§fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>
HttpResponse
.impl<L: Eq, R: Eq> Eq for Either<L, R>
impl<L, R> StructuralPartialEq for Either<L, R>
Auto Trait Implementations§
impl<L, R> Freeze for Either<L, R>
impl<L, R> RefUnwindSafe for Either<L, R>where
L: RefUnwindSafe,
R: RefUnwindSafe,
impl<L, R> Send for Either<L, R>
impl<L, R> Sync for Either<L, R>
impl<L, R> Unpin for Either<L, R>
impl<L, R> UnwindSafe for Either<L, R>where
L: UnwindSafe,
R: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.