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
Left(L)
A value of type L
.
Right(R)
A value of type R
.
Implementations
Trait Implementations
sourceimpl<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>
The associated error which can be returned.
sourcefn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
Create a Self
from request parts asynchronously.
sourcefn extract(req: &HttpRequest) -> Self::Future
fn extract(req: &HttpRequest) -> Self::Future
Create a Self
from request head asynchronously. Read more
sourceimpl<L: PartialEq, R: PartialEq> PartialEq<Either<L, R>> for Either<L, R>
impl<L: PartialEq, R: PartialEq> PartialEq<Either<L, R>> for Either<L, R>
sourceimpl<L, R> Responder for Either<L, R>where
L: Responder,
R: Responder,
impl<L, R> Responder for Either<L, R>where
L: Responder,
R: Responder,
See here for example of usage as a handler return type.
type Body = EitherBody<<L as Responder>::Body, <R as Responder>::Body>
sourcefn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>
Convert self to HttpResponse
.
sourcefn customize(self) -> CustomizeResponder<Self>where
Self: Sized,
fn customize(self) -> CustomizeResponder<Self>where
Self: Sized,
Wraps responder to allow alteration of its response. Read more
impl<L: Eq, R: Eq> Eq for Either<L, R>
impl<L, R> StructuralEq for Either<L, R>
impl<L, R> StructuralPartialEq for Either<L, R>
Auto Trait Implementations
impl<L, R> RefUnwindSafe for Either<L, R>where
L: RefUnwindSafe,
R: RefUnwindSafe,
impl<L, R> Send for Either<L, R>where
L: Send,
R: Send,
impl<L, R> Sync for Either<L, R>where
L: Sync,
R: Sync,
impl<L, R> Unpin for Either<L, R>where
L: Unpin,
R: Unpin,
impl<L, R> UnwindSafe for Either<L, R>where
L: UnwindSafe,
R: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for Twhere
V: MultiLane<T>,
impl<V, T> VZip<V> for Twhere
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more