Enum actix_web::Either[][src]

pub enum Either<L, R> {
    Left(L),
    Right(R),
}

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

Left(L)

A value of type L.

Right(R)

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]

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.

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]

See here for example of usage as a handler return type.

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

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

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]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,