[−][src]Enum actix_web::Either
Combines two different responder types into a single type
use actix_web::{AsyncResponder, Either, Error, HttpRequest, HttpResponse}; use futures::future::result; type RegisterResult = Either<HttpResponse, Box<Future<Item = HttpResponse, Error = Error>>>; fn index(req: HttpRequest) -> RegisterResult { if is_a_variant() { // <- choose variant A Either::A(HttpResponse::BadRequest().body("Bad data")) } else { Either::B( // <- variant B result(Ok(HttpResponse::Ok() .content_type("text/html") .body("Hello!"))) .responder(), ) } }
Variants
A(A)
First branch of the type
B(B)
Second branch of the type
Trait Implementations
impl<A, B> Responder for Either<A, B> where
A: Responder,
B: Responder,
[src]
A: Responder,
B: Responder,
type Item = AsyncResult<HttpResponse>
The associated item which can be returned.
type Error = Error
The associated error which can be returned.
fn respond_to<S: 'static>(
self,
req: &HttpRequest<S>
) -> Result<AsyncResult<HttpResponse>, Error>
[src]
self,
req: &HttpRequest<S>
) -> Result<AsyncResult<HttpResponse>, Error>
impl<A: 'static, B: 'static, S: 'static> FromRequest<S> for Either<A, B> where
A: FromRequest<S>,
B: FromRequest<S>,
[src]
A: FromRequest<S>,
B: FromRequest<S>,
Extract either one of two fields from the request.
If both or none of the fields can be extracted, the default behaviour is to prefer the first
successful, last that failed. The behaviour can be changed by setting the appropriate
EitherCollisionStrategy
.
CAVEAT: Most of the time both extractors will be run. Make sure that the extractors you specify
can be run one after another (or in parallel). This will always fail for extractors that modify
the request state (such as the Form
extractors that read in the body stream).
So Either<Form, Form> will not work correctly - it will only succeed if it matches the first
option, but will always fail to match the second (since the body stream will be at the end, and
appear to be empty).
Example
extern crate rand; #[macro_use] extern crate serde_derive; use actix_web::{http, App, Result, HttpRequest, Error, FromRequest}; use actix_web::error::ErrorBadRequest; use actix_web::Either; #[derive(Debug, Deserialize)] struct Thing { name: String } #[derive(Debug, Deserialize)] struct OtherThing { id: String } impl<S> FromRequest<S> for Thing { type Config = (); type Result = Result<Thing, Error>; #[inline] fn from_request(req: &HttpRequest<S>, _cfg: &Self::Config) -> Self::Result { if rand::random() { Ok(Thing { name: "thingy".into() }) } else { Err(ErrorBadRequest("no luck")) } } } impl<S> FromRequest<S> for OtherThing { type Config = (); type Result = Result<OtherThing, Error>; #[inline] fn from_request(req: &HttpRequest<S>, _cfg: &Self::Config) -> Self::Result { if rand::random() { Ok(OtherThing { id: "otherthingy".into() }) } else { Err(ErrorBadRequest("no luck")) } } } /// extract text data from request fn index(supplied_thing: Either<Thing, OtherThing>) -> Result<String> { match supplied_thing { Either::A(thing) => Ok(format!("Got something: {:?}", thing)), Either::B(other_thing) => Ok(format!("Got anotherthing: {:?}", other_thing)) } } fn main() { let app = App::new().resource("/users/:first", |r| { r.method(http::Method::POST).with(index) }); }
type Config = EitherConfig<A, B, S>
Configuration for conversion process
type Result = AsyncResult<Either<A, B>>
Future that resolves to a Self
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
[src]
fn extract(req: &HttpRequest<S>) -> Self::Result
[src]
Convert request to a Self Read more
impl<A: PartialEq, B: PartialEq> PartialEq<Either<A, B>> for Either<A, B>
[src]
impl<A: Debug, B: Debug> Debug for Either<A, B>
[src]
impl<A, B, I, E> Future for Either<A, B> where
A: Future<Item = I, Error = E>,
B: Future<Item = I, Error = E>,
[src]
A: Future<Item = I, Error = E>,
B: Future<Item = I, Error = E>,
type Item = I
The type of value that this future will resolved with if it is successful. Read more
type Error = E
The type of error that this future will resolve with if it fails in a normal fashion. Read more
fn poll(&mut self) -> Poll<I, E>
[src]
fn wait(self) -> Result<Self::Item, Self::Error>
[src]
Block the current thread until this future is resolved. Read more
fn map<F, U>(self, f: F) -> Map<Self, F> where
F: FnOnce(Self::Item) -> U,
[src]
F: FnOnce(Self::Item) -> U,
Map this future's result to a different type, returning a new future of the resulting type. Read more
fn map_err<F, E>(self, f: F) -> MapErr<Self, F> where
F: FnOnce(Self::Error) -> E,
[src]
F: FnOnce(Self::Error) -> E,
Map this future's error to a different error, returning a new future. Read more
fn from_err<E>(self) -> FromErr<Self, E> where
E: From<Self::Error>,
[src]
E: From<Self::Error>,
Map this future's error to any error implementing From
for this future's Error
, returning a new future. Read more
fn then<F, B>(self, f: F) -> Then<Self, B, F> where
B: IntoFuture,
F: FnOnce(Result<Self::Item, Self::Error>) -> B,
[src]
B: IntoFuture,
F: FnOnce(Result<Self::Item, Self::Error>) -> B,
Chain on a computation for when a future finished, passing the result of the future to the provided closure f
. Read more
fn and_then<F, B>(self, f: F) -> AndThen<Self, B, F> where
B: IntoFuture<Error = Self::Error>,
F: FnOnce(Self::Item) -> B,
[src]
B: IntoFuture<Error = Self::Error>,
F: FnOnce(Self::Item) -> B,
Execute another future after this one has resolved successfully. Read more
fn or_else<F, B>(self, f: F) -> OrElse<Self, B, F> where
B: IntoFuture<Item = Self::Item>,
F: FnOnce(Self::Error) -> B,
[src]
B: IntoFuture<Item = Self::Item>,
F: FnOnce(Self::Error) -> B,
Execute another future if this one resolves with an error. Read more
fn select<B>(self, other: B) -> Select<Self, <B as IntoFuture>::Future> where
B: IntoFuture<Item = Self::Item, Error = Self::Error>,
[src]
B: IntoFuture<Item = Self::Item, Error = Self::Error>,
Waits for either one of two futures to complete. Read more
fn select2<B>(self, other: B) -> Select2<Self, <B as IntoFuture>::Future> where
B: IntoFuture,
[src]
B: IntoFuture,
Waits for either one of two differently-typed futures to complete. Read more
fn join<B>(self, other: B) -> Join<Self, <B as IntoFuture>::Future> where
B: IntoFuture<Error = Self::Error>,
[src]
B: IntoFuture<Error = Self::Error>,
Joins the result of two futures, waiting for them both to complete. Read more
fn join3<B, C>(
self,
b: B,
c: C
) -> Join3<Self, <B as IntoFuture>::Future, <C as IntoFuture>::Future> where
B: IntoFuture<Error = Self::Error>,
C: IntoFuture<Error = Self::Error>,
[src]
self,
b: B,
c: C
) -> Join3<Self, <B as IntoFuture>::Future, <C as IntoFuture>::Future> where
B: IntoFuture<Error = Self::Error>,
C: IntoFuture<Error = Self::Error>,
Same as join
, but with more futures.
fn join4<B, C, D>(
self,
b: B,
c: C,
d: D
) -> Join4<Self, <B as IntoFuture>::Future, <C as IntoFuture>::Future, <D as IntoFuture>::Future> where
B: IntoFuture<Error = Self::Error>,
C: IntoFuture<Error = Self::Error>,
D: IntoFuture<Error = Self::Error>,
[src]
self,
b: B,
c: C,
d: D
) -> Join4<Self, <B as IntoFuture>::Future, <C as IntoFuture>::Future, <D as IntoFuture>::Future> where
B: IntoFuture<Error = Self::Error>,
C: IntoFuture<Error = Self::Error>,
D: IntoFuture<Error = Self::Error>,
Same as join
, but with more futures.
fn join5<B, C, D, E>(
self,
b: B,
c: C,
d: D,
e: E
) -> Join5<Self, <B as IntoFuture>::Future, <C as IntoFuture>::Future, <D as IntoFuture>::Future, <E as IntoFuture>::Future> where
B: IntoFuture<Error = Self::Error>,
C: IntoFuture<Error = Self::Error>,
D: IntoFuture<Error = Self::Error>,
E: IntoFuture<Error = Self::Error>,
[src]
self,
b: B,
c: C,
d: D,
e: E
) -> Join5<Self, <B as IntoFuture>::Future, <C as IntoFuture>::Future, <D as IntoFuture>::Future, <E as IntoFuture>::Future> where
B: IntoFuture<Error = Self::Error>,
C: IntoFuture<Error = Self::Error>,
D: IntoFuture<Error = Self::Error>,
E: IntoFuture<Error = Self::Error>,
Same as join
, but with more futures.
fn into_stream(self) -> IntoStream<Self>
[src]
Convert this future into a single element stream. Read more
fn flatten(self) -> Flatten<Self> where
Self::Item: IntoFuture,
<Self::Item as IntoFuture>::Error: From<Self::Error>,
[src]
Self::Item: IntoFuture,
<Self::Item as IntoFuture>::Error: From<Self::Error>,
Flatten the execution of this future when the successful result of this future is itself another future. Read more
fn flatten_stream(self) -> FlattenStream<Self> where
Self::Item: Stream,
<Self::Item as Stream>::Error == Self::Error,
[src]
Self::Item: Stream,
<Self::Item as Stream>::Error == Self::Error,
Flatten the execution of this future when the successful result of this future is a stream. Read more
fn fuse(self) -> Fuse<Self>
[src]
Fuse a future such that poll
will never again be called once it has completed. Read more
fn inspect<F>(self, f: F) -> Inspect<Self, F> where
F: FnOnce(&Self::Item),
[src]
F: FnOnce(&Self::Item),
Do something with the item of a future, passing it on. Read more
fn catch_unwind(self) -> CatchUnwind<Self> where
Self: UnwindSafe,
[src]
Self: UnwindSafe,
Catches unwinding panics while polling the future. Read more
fn shared(self) -> Shared<Self>
[src]
Create a cloneable handle to this future where all handles will resolve to the same result. Read more
Auto Trait Implementations
impl<A, B> Send for Either<A, B> where
A: Send,
B: Send,
A: Send,
B: Send,
impl<A, B> Sync for Either<A, B> where
A: Sync,
B: Sync,
A: Sync,
B: Sync,
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto 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.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<F> IntoFuture for F where
F: Future,
[src]
F: Future,
type Future = F
The future that this type can be converted into.
type Item = <F as Future>::Item
The item that the future may resolve with.
type Error = <F as Future>::Error
The error that the future may resolve with.
fn into_future(self) -> F
[src]
impl<T> Erased for T
impl<T> FutureExt for T where
T: Future + ?Sized,
[src]
T: Future + ?Sized,
fn timeout(self, timeout: Duration) -> Timeout<Self>
[src]
Creates a new future which allows self
until timeout
. Read more
impl<F, A> WrapFuture for F where
A: Actor,
F: Future,
[src]
A: Actor,
F: Future,
type Future = FutureWrap<F, A>
The future that this type can be converted into.
type Item = <F as Future>::Item
The item that the future may resolve with.
type Error = <F as Future>::Error
The error that the future may resolve with.