Skip to main content

Either

Enum Either 

Source
pub enum Either<A, B> {
    Left(A),
    Right(B),
}
Expand description

A value of one of two types.

Either is a Future when both variants are futures with the same output, and a Service when both variants are services with the same request, response, and error types.

Variants§

§

Left(A)

First branch of the type

§

Right(B)

Second branch of the type

Implementations§

Source§

impl<A, B> Either<A, B>

Source

pub fn is_left(&self) -> bool

Returns true if the value is the Left variant.

Source

pub fn is_right(&self) -> bool

Returns true if the value is the Right variant.

Source

pub fn left(self) -> Option<A>

Converts the left side of Either<A, B> to an Option<A>.

Source

pub fn right(self) -> Option<B>

Converts the right side of Either<A, B> to an Option<B>.

Source

pub fn as_ref(&self) -> Either<&A, &B> ⓘ

Converts &Either<A, B> to Either<&A, &B>.

Source

pub fn as_mut(&mut self) -> Either<&mut A, &mut B> ⓘ

Converts &mut Either<A, B> to Either<&mut A, &mut B>.

Source§

impl<T> Either<T, T>

Source

pub fn into_inner(self) -> T

Extracts the value of an Either over two equal types.

Trait Implementations§

Source§

impl<A, B> Clone for Either<A, B>
where A: Clone, B: Clone,

Source§

fn clone(&self) -> Either<A, B> ⓘ

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A, B> Copy for Either<A, B>
where A: Copy, B: Copy,

Source§

impl<A, B> Debug for Either<A, B>
where A: Debug, B: Debug,

Source§

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

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

impl<A, B> Display for Either<A, B>
where A: Display, B: Display,

Source§

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

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

impl<A, B> Eq for Either<A, B>
where A: Eq, B: Eq,

Source§

impl<A, B> Error for Either<A, B>
where A: Error, B: Error,

Either implements Error if both A and B implement it.

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Either<DecodeError, Error>> for ClientError

Source§

fn from(err: Either<DecodeError, Error>) -> Self

Converts to this type from the input type.
Source§

impl From<Either<DecodeError, Error>> for WsClientError

Source§

fn from(err: Either<DecodeError, Error>) -> Self

Converts to this type from the input type.
Source§

impl From<Either<EncodeError, Error>> for ClientError

Source§

fn from(err: Either<EncodeError, Error>) -> Self

Converts to this type from the input type.
Source§

impl From<Either<EncodeError, Error>> for WsClientError

Source§

fn from(err: Either<EncodeError, Error>) -> Self

Converts to this type from the input type.
Source§

impl From<Either<PayloadError, Error>> for PayloadError

Source§

fn from(err: Either<PayloadError, Error>) -> Self

Converts to this type from the input type.
Source§

impl<A, B> Future for Either<A, B>
where A: Future, B: Future<Output = <A as Future>::Output>,

Source§

type Output = <A as Future>::Output

The type of value produced on completion.
Source§

fn poll( self: Pin<&mut Either<A, B>>, cx: &mut Context<'_>, ) -> Poll<<Either<A, B> as Future>::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Source§

impl<A, B> Hash for Either<A, B>
where A: Hash, B: Hash,

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<A, B> Ord for Either<A, B>
where A: Ord, B: Ord,

Source§

fn cmp(&self, other: &Either<A, B>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<A, B> PartialEq for Either<A, B>
where A: PartialEq, B: PartialEq,

Source§

fn eq(&self, other: &Either<A, B>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<A, B> PartialOrd for Either<A, B>
where A: PartialOrd, B: PartialOrd,

Source§

fn partial_cmp(&self, other: &Either<A, B>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<St, A, B> Responder<St> for Either<A, B>
where St: State, A: Responder<St>, B: Responder<St>,

Combines two different responder types into a single type

use ntex::{web::HttpResponse, util::Either};

fn index() -> Either<HttpResponse, &'static str> {
    if is_a_variant() {
        // <- choose left variant
        Either::Left(HttpResponse::BadRequest().body("Bad data"))
    } else {
        // <- Right variant
        Either::Right("Hello!")
    }
}
Source§

async fn respond_to(self, st: &St, req: &HttpRequest) -> Response

Builds an HTTP response from this value.
Source§

fn with_status(self, status: StatusCode) -> CustomResponder<Self, St>
where Self: Sized,

Replaces the status code produced by this responder. Read more
Source§

fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self, St>

Sets a header on the response produced by this responder. Read more
Source§

impl<A, B, St, Req> Service<St, Req> for Either<A, B>
where A: Service<St, Req>, B: Service<St, Req, Res = <A as Service<St, Req>>::Res, Error = <A as Service<St, Req>>::Error>,

Source§

type Res = <A as Service<St, Req>>::Res

Response produced by the service.
Source§

type Error = <A as Service<St, Req>>::Error

Error produced while checking readiness or processing a request.
Source§

async fn call( &self, req: Req, ctx: Ctx<'_, Either<A, B>, St>, ) -> Result<<Either<A, B> as Service<St, Req>>::Res, <Either<A, B> as Service<St, Req>>::Error>

Processes a request and asynchronously returns the response. Read more
Source§

async fn ready( &self, ctx: Ctx<'_, Either<A, B>, St>, ) -> Result<(), <Either<A, B> as Service<St, Req>>::Error>

Waits until the service is ready to process a request. Read more
Source§

async fn shutdown(&self, ctx: Ctx<'_, Either<A, B>, St>)

Shuts down the service. Read more
Source§

fn map<F, Res>(self, f: F) -> ServiceChain<Map<F, Self, Res>, St, Req>
where Self: Sized, F: Fn(Self::Res) -> Res,

Maps this service’s output to a different type, returning a new service. Read more
Source§

fn map_err<F, E>(self, f: F) -> ServiceChain<MapErr<F, Self, E>, St, Req>
where Self: Sized, F: Fn(Self::Error) -> E,

Maps this service’s error to a different type, returning a new service. Read more
Source§

fn and_then<Next, F>(self, f: F) -> ServiceChain<AndThen<Self, Next>, St, Req>
where Self: Sized, Next: Service<St, Self::Res, Error = Self::Error>, F: IntoService<Next, St, Self::Res>,

Calls another service after this service completes successfully. Read more
Source§

fn pipeline(self, st: St) -> Pipeline<Req, Self::Res, Self::Error>
where Self: Sized + 'static, St: 'static, Req: 'static,

Wraps this service and its state in a Pipeline.
Source§

impl<A, B> StructuralPartialEq for Either<A, B>
where A: PartialEq, B: PartialEq,

Source§

impl<St, Err, A, B> WebResponseError<St, Err> for Either<A, B>
where A: WebResponseError<St, Err>, B: WebResponseError<St, Err>,

Source§

fn error_response(&self, st: &St) -> HttpResponse

Generate response for error Read more
Source§

fn into(self) -> Box<dyn WebResponseError<St, Err>>
where Self: Sized,

Convert the error into a boxed trait object. Read more

Auto Trait Implementations§

§

impl<A, B> Freeze for Either<A, B>
where A: Freeze, B: Freeze,

§

impl<A, B> RefUnwindSafe for Either<A, B>

§

impl<A, B> Send for Either<A, B>
where A: Send, B: Send,

§

impl<A, B> Sync for Either<A, B>
where A: Sync, B: Sync,

§

impl<A, B> Unpin for Either<A, B>
where A: Unpin, B: Unpin,

§

impl<A, B> UnsafeUnpin for Either<A, B>
where A: UnsafeUnpin, B: UnsafeUnpin,

§

impl<A, B> UnwindSafe for Either<A, B>
where A: UnwindSafe, B: 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

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

§

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, 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<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<S, St, Req> IntoService<S, St, Req> for S
where S: Service<St, Req>,

Source§

fn into_service(self) -> S

Converts this value into a service.
Source§

impl<F> OnDropFutureExt for F
where F: Future,

Source§

fn on_drop<F>(self, on_drop: F) -> OnDropFuture<Self, F> ⓘ
where F: FnOnce(),

Wraps this future so that on_drop is called if the future is dropped before it completes. The callback is cancelled once the future completes, whatever its output.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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

impl<F, T, E> TryFuture for F
where F: Future<Output = Result<T, E>> + ?Sized,

§

type Ok = T

The type of successful values yielded by this future
§

type Error = E

The type of failures yielded by this future
§

fn try_poll( self: Pin<&mut F>, cx: &mut Context<'_>, ) -> Poll<<F as Future>::Output>

Poll this TryFuture as if it were a Future. Read more
Source§

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

Source§

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.