Enum Either

enum Either<L, 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:

# fn some_condition() -> bool { true }
# async fn some_async_function() -> u32 { 10 }
# async fn other_async_function() -> u32 { 20 }
#[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;
# fn some_condition() -> bool { true }
# async fn some_async_function() -> u32 { 10 }
# async fn other_async_function() -> u32 { 20 }

#[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);
    # assert_eq!(value, 10);
}

Variants

Left(L)
Right(R)

Implementations

impl<F> IntoFuture for Either<L, R>

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

impl<F, T, E> TryFuture for Either<L, R>

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

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

fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>>
fn consume(self: Pin<&mut Self>, amt: usize)

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

fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<Result<()>>

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

fn start_seek(self: Pin<&mut Self>, position: SeekFrom) -> Result<()>
fn poll_complete(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<u64>>

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

fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize, Error>>
fn is_write_vectored(self: &Self) -> bool

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

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

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

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

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<<Self as >::Item>>

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

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

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

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

impl<L, R, Item, Error> Sink for Either<L, R>

fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), <Self as >::Error>>
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), <Self as >::Error>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), <Self as >::Error>>
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), <Self as >::Error>>

impl<L, R, O> Future for Either<L, R>

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

impl<L: $crate::clone::Clone, R: $crate::clone::Clone> Clone for Either<L, R>

fn clone(self: &Self) -> Either<L, R>

impl<L: $crate::fmt::Debug, R: $crate::fmt::Debug> Debug for Either<L, R>

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

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

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

impl<S> AsyncSeekExt for Either<L, R>

impl<S, T, E> TryStream for Either<L, R>

fn try_poll_next(self: Pin<&mut S>, cx: &mut Context<'_>) -> Poll<Option<Result<<S as TryStream>::Ok, <S as TryStream>::Error>>>

impl<T> Any for Either<L, R>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Either<L, R>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Either<L, R>

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

impl<T> CloneToUninit for Either<L, R>

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

impl<T> From for Either<L, R>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Either<L, R>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for Either<L, R>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Either<L, R>

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

impl<T, U> TryInto for Either<L, R>

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

impl<W> AsyncWriteExt for Either<L, R>