Enum tokio_util::either::Either[][src]

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

Combines two different futures, streams, or sinks having the same associated types into a single type.

This type implements common asynchronous traits such as Future and those in Tokio.

Example

The following code will not work:

#[tokio::main]
async fn main() {
    let result = if some_condition() {
        some_async_function()
    } else {
        other_async_function() // <- Will print: "`if` and `else` have incompatible types"
    };

    println!("Result is {}", result.await);
}

When the output type is the same, we can wrap each future in Either to avoid the issue:

use tokio_util::either::Either;

#[tokio::main]
async fn main() {
    let result = if some_condition() {
        Either::Left(some_async_function())
    } else {
        Either::Right(other_async_function())
    };

    let value = result.await;
    println!("Result is {}", value);
}

Variants

Left(L)
Right(R)

Trait Implementations

impl<L, R> AsyncBufRead for Either<L, R> where
    L: AsyncBufRead,
    R: AsyncBufRead
[src]

impl<L, R> AsyncRead for Either<L, R> where
    L: AsyncRead,
    R: AsyncRead
[src]

impl<L, R> AsyncSeek for Either<L, R> where
    L: AsyncSeek,
    R: AsyncSeek
[src]

impl<L, R> AsyncWrite for Either<L, R> where
    L: AsyncWrite,
    R: AsyncWrite
[src]

impl<L: Clone, R: Clone> Clone for Either<L, R>[src]

impl<L: Debug, R: Debug> Debug for Either<L, R>[src]

impl<L, R, O> Future for Either<L, R> where
    L: Future<Output = O>,
    R: Future<Output = O>, 
[src]

type Output = O

The type of value produced on completion.

impl<L, R> Stream for Either<L, R> where
    L: Stream,
    R: Stream<Item = L::Item>, 
[src]

type Item = L::Item

Values yielded by the stream.

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, U> Into<U> for T where
    U: From<T>, 
[src]

impl<F> IntoFuture for F where
    F: Future
[src]

type Output = <F as Future>::Output

🔬 This is a nightly-only experimental API. (into_future)

The output that the future will produce on completion.

type Future = F

🔬 This is a nightly-only experimental API. (into_future)

Which kind of future are we turning this into?

impl<St> StreamExt for St where
    St: Stream + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> Tokio02AsyncReadCompatExt for T where
    T: AsyncRead
[src]

impl<T> Tokio02AsyncWriteCompatExt for T where
    T: AsyncWrite
[src]

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<F, T, E> TryFuture for F where
    F: Future<Output = Result<T, E>> + ?Sized
[src]

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future

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<S, T, E> TryStream for S where
    S: Stream<Item = Result<T, E>> + ?Sized
[src]

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future