Enum tokio_util::either::Either [−][src]
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
Trait Implementations
impl<L, R> AsyncBufRead for Either<L, R> where
L: AsyncBufRead,
R: AsyncBufRead,
[src]
L: AsyncBufRead,
R: AsyncBufRead,
fn poll_fill_buf(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<&[u8]>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<&[u8]>>
fn consume(self: Pin<&mut Self>, amt: usize)
[src]
impl<L, R> AsyncRead for Either<L, R> where
L: AsyncRead,
R: AsyncRead,
[src]
L: AsyncRead,
R: AsyncRead,
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>
) -> Poll<Result<()>>
[src]
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,
[src]
L: AsyncSeek,
R: AsyncSeek,
fn start_seek(self: Pin<&mut Self>, position: SeekFrom) -> Result<()>
[src]
fn poll_complete(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<u64>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<u64>>
impl<L, R> AsyncWrite for Either<L, R> where
L: AsyncWrite,
R: AsyncWrite,
[src]
L: AsyncWrite,
R: AsyncWrite,
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
[src]
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
[src]
pub fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
pub fn is_write_vectored(&self) -> bool
[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]
L: Future<Output = O>,
R: Future<Output = O>,
type Output = O
The type of value produced on completion.
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>
[src]
impl<L, R> Stream for Either<L, R> where
L: Stream,
R: Stream<Item = L::Item>,
[src]
L: Stream,
R: Stream<Item = L::Item>,
Auto Trait Implementations
impl<L, R> RefUnwindSafe for Either<L, R> where
L: RefUnwindSafe,
R: RefUnwindSafe,
L: RefUnwindSafe,
R: RefUnwindSafe,
impl<L, R> Send for Either<L, R> where
L: Send,
R: Send,
L: Send,
R: Send,
impl<L, R> Sync for Either<L, R> where
L: Sync,
R: Sync,
L: Sync,
R: Sync,
impl<L, R> Unpin for Either<L, R> where
L: Unpin,
R: Unpin,
L: Unpin,
R: Unpin,
impl<L, R> UnwindSafe for Either<L, R> where
L: UnwindSafe,
R: UnwindSafe,
L: UnwindSafe,
R: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<F> IntoFuture for F where
F: Future,
[src]
F: Future,
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?
pub fn into_future(self) -> <F as IntoFuture>::Future
[src]
impl<St> StreamExt for St where
St: Stream + ?Sized,
[src]
St: Stream + ?Sized,
pub fn next(&mut self) -> Next<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn try_next<T, E>(&mut self) -> TryNext<'_, Self> where
Self: Stream<Item = Result<T, E>> + Unpin,
[src]
Self: Stream<Item = Result<T, E>> + Unpin,
pub fn map<T, F>(self, f: F) -> Map<Self, F> where
F: FnMut(Self::Item) -> T,
[src]
F: FnMut(Self::Item) -> T,
pub fn merge<U>(self, other: U) -> Merge<Self, U> where
U: Stream<Item = Self::Item>,
[src]
U: Stream<Item = Self::Item>,
pub fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FnMut(&Self::Item) -> bool,
[src]
F: FnMut(&Self::Item) -> bool,
pub fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F> where
F: FnMut(Self::Item) -> Option<T>,
[src]
F: FnMut(Self::Item) -> Option<T>,
pub fn fuse(self) -> Fuse<Self>
[src]
pub fn take(self, n: usize) -> Take<Self>
[src]
pub fn take_while<F>(self, f: F) -> TakeWhile<Self, F> where
F: FnMut(&Self::Item) -> bool,
[src]
F: FnMut(&Self::Item) -> bool,
pub fn skip(self, n: usize) -> Skip<Self>
[src]
pub fn skip_while<F>(self, f: F) -> SkipWhile<Self, F> where
F: FnMut(&Self::Item) -> bool,
[src]
F: FnMut(&Self::Item) -> bool,
pub fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> bool,
[src]
Self: Unpin,
F: FnMut(Self::Item) -> bool,
pub fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> bool,
[src]
Self: Unpin,
F: FnMut(Self::Item) -> bool,
pub fn chain<U>(self, other: U) -> Chain<Self, U> where
U: Stream<Item = Self::Item>,
[src]
U: Stream<Item = Self::Item>,
pub fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, B, F> where
F: FnMut(B, Self::Item) -> B,
[src]
F: FnMut(B, Self::Item) -> B,
pub fn collect<T>(self) -> Collect<Self, T> where
T: FromStream<Self::Item>,
[src]
T: FromStream<Self::Item>,
pub fn timeout(self, duration: Duration) -> Timeout<Self>
[src]
pub fn throttle(self, duration: Duration) -> Throttle<Self>
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> Tokio02AsyncReadCompatExt for T where
T: AsyncRead,
[src]
T: AsyncRead,
impl<T> Tokio02AsyncWriteCompatExt for T where
T: AsyncWrite,
[src]
T: AsyncWrite,
fn compat_write(self) -> Compat<Self> where
Self: Sized,
[src]
Self: Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<F, T, E> TryFuture for F where
F: Future<Output = Result<T, E>> + ?Sized,
[src]
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
pub fn try_poll(
self: Pin<&mut F>,
cx: &mut Context<'_>
) -> Poll<<F as Future>::Output>
[src]
self: Pin<&mut F>,
cx: &mut Context<'_>
) -> Poll<<F as Future>::Output>
impl<T, U> TryInto<U> 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.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<S, T, E> TryStream for S where
S: Stream<Item = Result<T, E>> + ?Sized,
[src]
S: Stream<Item = Result<T, E>> + ?Sized,