Enum actix_web::Either

source ·
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§

source§

impl<T> Either<Form<T>, Json<T>>

source

pub fn into_inner(self) -> T

source§

impl<T> Either<Json<T>, Form<T>>

source

pub fn into_inner(self) -> T

Trait Implementations§

source§

impl<L: Debug, R: Debug> Debug for Either<L, R>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

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>

The associated error which can be returned.
§

type Future = EitherExtractFut<L, R>

Future that resolves to a Self. Read more
source§

fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future

Create a Self from request parts asynchronously.
source§

fn extract(req: &HttpRequest) -> Self::Future

Create a Self from request head asynchronously. Read more
source§

impl<L: PartialEq, R: PartialEq> PartialEq for Either<L, R>

source§

fn eq(&self, other: &Either<L, R>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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>

source§

fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>

Convert self to HttpResponse.
source§

fn customize(self) -> CustomizeResponder<Self>
where Self: Sized,

Wraps responder to allow alteration of its response. Read more
source§

impl<L: Eq, R: Eq> Eq for Either<L, R>

source§

impl<L, R> StructuralPartialEq for Either<L, R>

Auto Trait Implementations§

§

impl<L, R> Freeze for Either<L, R>
where L: Freeze, R: Freeze,

§

impl<L, R> RefUnwindSafe for Either<L, R>

§

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

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

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more