Enum Either

pub 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(flavor = "current_thread")]
# 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)

Trait Implementations

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

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> where L: AsyncRead, R: AsyncRead,

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

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

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> where L: AsyncWrite, R: AsyncWrite,

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) -> bool

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

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

impl<L, R, Item, Error> Sink<Item> for Either<L, R> where L: Sink<Item, Error = Error>, R: Sink<Item, Error = Error>,

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

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

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

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

fn clone(&self) -> Self

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

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

Auto Trait Implementations

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

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> UnsafeUnpin for Either<L, R> where L: UnsafeUnpin, R: UnsafeUnpin,

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

Blanket Implementations

impl<F> IntoFuture for Either<L, R> where F: Future,

type Output = <F as Future>::Output;
type IntoFuture = F;
fn into_future(self) -> <F as IntoFuture>::IntoFuture

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

type Ok = T;
type Error = E;
fn try_poll(self: Pin<&mut F>, cx: &mut Context<'_>) -> Poll<<F as Future>::Output>

impl<R> AsyncBufReadExt for Either<L, R> where R: AsyncBufRead + ?Sized,

impl<R> AsyncReadExt for Either<L, R> where R: AsyncRead + ?Sized,

impl<S> AsyncSeekExt for Either<L, R> where S: AsyncSeek + ?Sized,

impl<S, T, E> TryStream for Either<L, R> where S: Stream<Item = Result<T, E>> + ?Sized,

type Ok = T;
type Error = E;
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> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Either<L, R> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Either<L, R> where T: ?Sized,

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

impl<T> CloneToUninit for Either<L, R> where T: Clone,

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

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

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FutureExt for Either<L, R> where T: Future + ?Sized,

impl<T> ToOwned for Either<L, R> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Either<L, R> where U: From<T>,

fn into(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<U> for Either<L, R> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Either<L, R> where U: TryFrom<T>,

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

impl<W> AsyncWriteExt for Either<L, R> where W: AsyncWrite + ?Sized,