|
| 1 | +use core::pin::Pin; |
| 2 | +use core::task::{Context, Poll}; |
| 3 | +use futures_core::future::Future; |
| 4 | +use futures_core::stream::Stream; |
| 5 | +use futures_sink::Sink; |
| 6 | + |
| 7 | +/// Combines two different futures, streams, or sinks having the same associated types into a single |
| 8 | +/// type. |
| 9 | +#[derive(Debug, Clone)] |
| 10 | +pub enum Either<A, B> { |
| 11 | + /// First branch of the type |
| 12 | + Left(A), |
| 13 | + /// Second branch of the type |
| 14 | + Right(B), |
| 15 | +} |
| 16 | + |
| 17 | +impl<A, B, T> Either<(T, A), (T, B)> { |
| 18 | + /// Factor out a homogeneous type from an either of pairs. |
| 19 | + /// |
| 20 | + /// Here, the homogeneous type is the first element of the pairs. |
| 21 | + pub fn factor_first(self) -> (T, Either<A, B>) { |
| 22 | + match self { |
| 23 | + Either::Left((x, a)) => (x, Either::Left(a)), |
| 24 | + Either::Right((x, b)) => (x, Either::Right(b)), |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl<A, B, T> Either<(A, T), (B, T)> { |
| 30 | + /// Factor out a homogeneous type from an either of pairs. |
| 31 | + /// |
| 32 | + /// Here, the homogeneous type is the second element of the pairs. |
| 33 | + pub fn factor_second(self) -> (Either<A, B>, T) { |
| 34 | + match self { |
| 35 | + Either::Left((a, x)) => (Either::Left(a), x), |
| 36 | + Either::Right((b, x)) => (Either::Right(b), x), |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl<T> Either<T, T> { |
| 42 | + /// Extract the value of an either over two equivalent types. |
| 43 | + pub fn into_inner(self) -> T { |
| 44 | + match self { |
| 45 | + Either::Left(x) => x, |
| 46 | + Either::Right(x) => x, |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<A, B> Future for Either<A, B> |
| 52 | +where |
| 53 | + A: Future, |
| 54 | + B: Future<Output = A::Output>, |
| 55 | +{ |
| 56 | + type Output = A::Output; |
| 57 | + |
| 58 | + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<A::Output> { |
| 59 | + unsafe { |
| 60 | + match Pin::get_unchecked_mut(self) { |
| 61 | + Either::Left(a) => Pin::new_unchecked(a).poll(cx), |
| 62 | + Either::Right(b) => Pin::new_unchecked(b).poll(cx), |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<A, B> Stream for Either<A, B> |
| 69 | +where |
| 70 | + A: Stream, |
| 71 | + B: Stream<Item = A::Item>, |
| 72 | +{ |
| 73 | + type Item = A::Item; |
| 74 | + |
| 75 | + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<A::Item>> { |
| 76 | + unsafe { |
| 77 | + match Pin::get_unchecked_mut(self) { |
| 78 | + Either::Left(a) => Pin::new_unchecked(a).poll_next(cx), |
| 79 | + Either::Right(b) => Pin::new_unchecked(b).poll_next(cx), |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<A, B, Item> Sink<Item> for Either<A, B> |
| 86 | +where |
| 87 | + A: Sink<Item>, |
| 88 | + B: Sink<Item, SinkError = A::SinkError>, |
| 89 | +{ |
| 90 | + type SinkError = A::SinkError; |
| 91 | + |
| 92 | + fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> { |
| 93 | + unsafe { |
| 94 | + match Pin::get_unchecked_mut(self) { |
| 95 | + Either::Left(x) => Pin::new_unchecked(x).poll_ready(cx), |
| 96 | + Either::Right(x) => Pin::new_unchecked(x).poll_ready(cx), |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::SinkError> { |
| 102 | + unsafe { |
| 103 | + match Pin::get_unchecked_mut(self) { |
| 104 | + Either::Left(x) => Pin::new_unchecked(x).start_send(item), |
| 105 | + Either::Right(x) => Pin::new_unchecked(x).start_send(item), |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> { |
| 111 | + unsafe { |
| 112 | + match Pin::get_unchecked_mut(self) { |
| 113 | + Either::Left(x) => Pin::new_unchecked(x).poll_flush(cx), |
| 114 | + Either::Right(x) => Pin::new_unchecked(x).poll_flush(cx), |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> { |
| 120 | + unsafe { |
| 121 | + match Pin::get_unchecked_mut(self) { |
| 122 | + Either::Left(x) => Pin::new_unchecked(x).poll_close(cx), |
| 123 | + Either::Right(x) => Pin::new_unchecked(x).poll_close(cx), |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments