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

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

This is useful when conditionally choosing between two distinct future types:

use futures::future::Either;

let cond = true;

let fut = if cond {
    Either::Left(async move { 12 })
} else {
    Either::Right(async move { 44 })
};

assert_eq!(fut.await, 12);

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 as_pin_ref(self: Pin<&Self>) -> Either<Pin<&A>, Pin<&B>>

Convert Pin<&Either<A, B>> to Either<Pin<&A>, Pin<&B>>, pinned projections of the inner variants.

source

pub fn as_pin_mut(self: Pin<&mut Self>) -> Either<Pin<&mut A>, Pin<&mut B>>

Convert Pin<&mut Either<A, B>> to Either<Pin<&mut A>, Pin<&mut B>>, pinned projections of the inner variants.

source§

impl<A, B, T> Either<(T, A), (T, B)>

source

pub fn factor_first(self) -> (T, Either<A, B>)

Factor out a homogeneous type from an either of pairs.

Here, the homogeneous type is the first element of the pairs.

source§

impl<A, B, T> Either<(A, T), (B, T)>

source

pub fn factor_second(self) -> (Either<A, B>, T)

Factor out a homogeneous type from an either of pairs.

Here, the homogeneous type is the second element of the pairs.

source§

impl<T> Either<T, T>

source

pub fn into_inner(self) -> T

Extract the value of an either over two equivalent types.

Trait Implementations§

source§

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

source§

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

Attempt to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
source§

fn consume(self: Pin<&mut Self>, amt: usize)

Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to poll_read. Read more
source§

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

source§

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

Attempt to read from the AsyncRead into buf. Read more
source§

fn poll_read_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>] ) -> Poll<Result<usize>>

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
source§

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

source§

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

Attempt to seek to an offset, in bytes, in a stream. Read more
source§

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

source§

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

Attempt to write bytes from buf into the object. Read more
source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize>>

Attempt to write bytes from bufs into the object using vectored IO operations. Read more
source§

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

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
source§

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

Attempt to close the object. Read more
source§

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

source§

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

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

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

source§

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

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

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

source§

fn is_terminated(&self) -> bool

Returns true if the underlying future should no longer be polled.
source§

impl<A, B> FusedStream for Either<A, B>where A: FusedStream, B: FusedStream<Item = A::Item>,

source§

fn is_terminated(&self) -> bool

Returns true if the stream should no longer be polled.
source§

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

§

type Output = <A as Future>::Output

The type of value produced on completion.
source§

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

Attempt 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, Item> Sink<Item> for Either<A, B>where A: Sink<Item>, B: Sink<Item, Error = A::Error>,

§

type Error = <A as Sink<Item>>::Error

The type of value produced by the sink when an error occurs.
source§

fn poll_ready( self: Pin<&mut Self>, cx: &mut Context<'_> ) -> Poll<Result<(), Self::Error>>

Attempts to prepare the Sink to receive a value. Read more
source§

fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error>

Begin the process of sending a value to the sink. Each call to this function must be preceded by a successful call to poll_ready which returned Poll::Ready(Ok(())). Read more
source§

fn poll_flush( self: Pin<&mut Self>, cx: &mut Context<'_> ) -> Poll<Result<(), Self::Error>>

Flush any remaining output from this sink. Read more
source§

fn poll_close( self: Pin<&mut Self>, cx: &mut Context<'_> ) -> Poll<Result<(), Self::Error>>

Flush any remaining output and close this sink, if necessary. Read more
source§

impl<A, B> Stream for Either<A, B>where A: Stream, B: Stream<Item = A::Item>,

§

type Item = <A as Stream>::Item

Values yielded by the stream.
source§

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

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more

Auto Trait Implementations§

§

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

§

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

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<R> AsyncBufReadExt for Rwhere R: AsyncBufRead + ?Sized,

source§

fn fill_buf(&mut self) -> FillBuf<'_, Self> where Self: Unpin,

Creates a future which will wait for a non-empty buffer to be available from this I/O object or EOF to be reached. Read more
source§

fn consume_unpin(&mut self, amt: usize)where Self: Unpin,

A convenience for calling AsyncBufRead::consume on Unpin IO types. Read more
source§

fn read_until<'a>( &'a mut self, byte: u8, buf: &'a mut Vec<u8> ) -> ReadUntil<'a, Self> where Self: Unpin,

Creates a future which will read all the bytes associated with this I/O object into buf until the delimiter byte or EOF is reached. This method is the async equivalent to BufRead::read_until. Read more
source§

fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> where Self: Unpin,

Creates a future which will read all the bytes associated with this I/O object into buf until a newline (the 0xA byte) or EOF is reached, This method is the async equivalent to BufRead::read_line. Read more
source§

fn lines(self) -> Lines<Self>where Self: Sized,

Returns a stream over the lines of this reader. This method is the async equivalent to BufRead::lines. Read more
source§

impl<R> AsyncReadExt for Rwhere R: AsyncRead + ?Sized,

source§

fn chain<R>(self, next: R) -> Chain<Self, R>where Self: Sized, R: AsyncRead,

Creates an adaptor which will chain this stream with another. Read more
source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where Self: Unpin,

Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type. Read more
source§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>] ) -> ReadVectored<'a, Self> where Self: Unpin,

Creates a future which will read from the AsyncRead into bufs using vectored IO operations. Read more
source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where Self: Unpin,

Creates a future which will read exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner. Read more
source§

fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self> where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
source§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String ) -> ReadToString<'a, Self> where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
source§

fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)where Self: AsyncWrite + Sized,

Helper method for splitting this read/write object into two halves. Read more
source§

fn take(self, limit: u64) -> Take<Self>where Self: Sized,

Creates an AsyncRead adapter which will read at most limit bytes from the underlying reader. Read more
source§

impl<S> AsyncSeekExt for Swhere S: AsyncSeek + ?Sized,

source§

fn seek(&mut self, pos: SeekFrom) -> Seek<'_, Self> where Self: Unpin,

Creates a future which will seek an IO object, and then yield the new position in the object and the object itself. Read more
source§

fn stream_position(&mut self) -> Seek<'_, Self> where Self: Unpin,

Creates a future which will return the current seek position from the start of the stream. Read more
source§

impl<W> AsyncWriteExt for Wwhere W: AsyncWrite + ?Sized,

source§

fn flush(&mut self) -> Flush<'_, Self> where Self: Unpin,

Creates a future which will entirely flush this AsyncWrite. Read more
source§

fn close(&mut self) -> Close<'_, Self> where Self: Unpin,

Creates a future which will entirely close this AsyncWrite.
source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self> where Self: Unpin,

Creates a future which will write bytes from buf into the object. Read more
source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>] ) -> WriteVectored<'a, Self> where Self: Unpin,

Creates a future which will write bytes from bufs into the object using vectored IO operations. Read more
source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> where Self: Unpin,

Write data into this object. Read more
source§

fn into_sink<Item: AsRef<[u8]>>(self) -> IntoSink<Self, Item>where Self: Sized,

Allow using an AsyncWrite as a Sink<Item: AsRef<[u8]>>. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Fwhere F: Future,

§

type Output = <F as Future>::Output

The output that the future will produce on completion.
§

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<T, Item> SinkExt<Item> for Twhere T: Sink<Item> + ?Sized,

source§

fn with<U, Fut, F, E>(self, f: F) -> With<Self, Item, U, Fut, F>where F: FnMut(U) -> Fut, Fut: Future<Output = Result<Item, E>>, E: From<Self::Error>, Self: Sized,

Composes a function in front of the sink. Read more
source§

fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F>where F: FnMut(U) -> St, St: Stream<Item = Result<Item, Self::Error>>, Self: Sized,

Composes a function in front of the sink. Read more
source§

fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F>where F: FnOnce(Self::Error) -> E, Self: Sized,

Transforms the error returned by the sink.
source§

fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E>where Self: Sized, Self::Error: Into<E>,

Map this sink’s error to a different error type using the Into trait. Read more
source§

fn buffer(self, capacity: usize) -> Buffer<Self, Item>where Self: Sized,

Adds a fixed-size buffer to the current sink. Read more
source§

fn close(&mut self) -> Close<'_, Self, Item> where Self: Unpin,

Close the sink.
source§

fn fanout<Si>(self, other: Si) -> Fanout<Self, Si>where Self: Sized, Item: Clone, Si: Sink<Item, Error = Self::Error>,

Fanout items to multiple sinks. Read more
source§

fn flush(&mut self) -> Flush<'_, Self, Item> where Self: Unpin,

Flush the sink, processing all pending items. Read more
source§

fn send(&mut self, item: Item) -> Send<'_, Self, Item> where Self: Unpin,

A future that completes after the given item has been fully processed into the sink, including flushing. Read more
source§

fn feed(&mut self, item: Item) -> Feed<'_, Self, Item> where Self: Unpin,

A future that completes after the given item has been received by the sink. Read more
source§

fn send_all<'a, St>(&'a mut self, stream: &'a mut St) -> SendAll<'a, Self, St> where St: TryStream<Ok = Item, Error = Self::Error> + Stream + Unpin + ?Sized, Self: Unpin,

A future that completes after the given stream has been fully processed into the sink, including flushing. Read more
source§

fn left_sink<Si2>(self) -> Either<Self, Si2> where Si2: Sink<Item, Error = Self::Error>, Self: Sized,

Wrap this sink in an Either sink, making it the left-hand variant of that Either. Read more
source§

fn right_sink<Si1>(self) -> Either<Si1, Self> where Si1: Sink<Item, Error = Self::Error>, Self: Sized,

Wrap this stream in an Either stream, making it the right-hand variant of that Either. Read more
source§

fn poll_ready_unpin( &mut self, cx: &mut Context<'_> ) -> Poll<Result<(), Self::Error>>where Self: Unpin,

A convenience method for calling Sink::poll_ready on Unpin sink types.
source§

fn start_send_unpin(&mut self, item: Item) -> Result<(), Self::Error>where Self: Unpin,

A convenience method for calling Sink::start_send on Unpin sink types.
source§

fn poll_flush_unpin( &mut self, cx: &mut Context<'_> ) -> Poll<Result<(), Self::Error>>where Self: Unpin,

A convenience method for calling Sink::poll_flush on Unpin sink types.
source§

fn poll_close_unpin( &mut self, cx: &mut Context<'_> ) -> Poll<Result<(), Self::Error>>where Self: Unpin,

A convenience method for calling Sink::poll_close on Unpin sink types.
source§

impl<T> ToOwned for Twhere T: Clone,

§

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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<F, T, E> TryFuture for Fwhere 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
source§

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 Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<S, T, E> TryStream for Swhere S: Stream<Item = 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
source§

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

Poll this TryStream as if it were a Stream. Read more