Enum actix_web::Either [−][src]
Combines two extractor or responder types into a single type.
Can be converted to and from an either::Either
.
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 desireable 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
A value of type L
.
A value of type R
.
Implementations
impl<T> Either<Form<T>, Json<T>>
[src]
pub fn into_inner(self) -> T
[src]
impl<T> Either<Json<T>, Form<T>>
[src]
pub fn into_inner(self) -> T
[src]
Trait Implementations
impl<L: Debug, R: Debug> Debug for Either<L, R>
[src]
impl<L, R> From<Either<L, R>> for Either<L, R>
[src]
impl<L, R> FromRequest for Either<L, R> where
L: FromRequest + 'static,
R: FromRequest + 'static,
[src]
L: FromRequest + 'static,
R: FromRequest + 'static,
See here for example of usage as an extractor.
type Error = EitherExtractError<L::Error, R::Error>
The associated error which can be returned.
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>
Future that resolves to a Self.
type Config = ()
Configuration for this extractor.
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
fn extract(req: &HttpRequest) -> Self::Future
[src]
fn configure<F>(f: F) -> Self::Config where
F: FnOnce(Self::Config) -> Self::Config,
[src]
F: FnOnce(Self::Config) -> Self::Config,
impl<L: PartialEq, R: PartialEq> PartialEq<Either<L, R>> for Either<L, R>
[src]
impl<L, R> Responder for Either<L, R> where
L: Responder,
R: Responder,
[src]
L: Responder,
R: Responder,
See here for example of usage as a handler return type.
fn respond_to(self, req: &HttpRequest) -> HttpResponseⓘ
[src]
fn with_status(self, status: StatusCode) -> CustomResponder<Self> where
Self: Sized,
[src]
Self: Sized,
fn with_header<H>(self, header: H) -> CustomResponder<Self> where
Self: Sized,
H: IntoHeaderPair,
[src]
Self: Sized,
H: IntoHeaderPair,
impl<L, R> StructuralPartialEq for Either<L, R>
[src]
Auto Trait Implementations
impl<L, R> RefUnwindSafe for Either<L, R> where
L: RefUnwindSafe,
R: RefUnwindSafe,
[src]
L: RefUnwindSafe,
R: RefUnwindSafe,
impl<L, R> Send for Either<L, R> where
L: Send,
R: Send,
[src]
L: Send,
R: Send,
impl<L, R> Sync for Either<L, R> where
L: Sync,
R: Sync,
[src]
L: Sync,
R: Sync,
impl<L, R> Unpin for Either<L, R> where
L: Unpin,
R: Unpin,
[src]
L: Unpin,
R: Unpin,
impl<L, R> UnwindSafe for Either<L, R> where
L: UnwindSafe,
R: UnwindSafe,
[src]
L: UnwindSafe,
R: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,