either_future/
future.rs

1use crate::EitherFuture;
2use core::future::Future;
3use core::pin::Pin;
4use core::task::{Context, Poll};
5use either::Either;
6
7impl<Left, Right, LeftFuture, RightFuture> Future for EitherFuture<LeftFuture, RightFuture>
8where
9	LeftFuture: Future<Output = Left>,
10	RightFuture: Future<Output = Right>,
11{
12	type Output = Either<Left, Right>;
13
14	fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
15		// This use of unsafe is required in order to transform Pin<Either<LeftFuture,RightFuture>>
16		// to Either<Pin<LeftFuture>,Pin<RightFuture>> essentially. This is safe to do because nothing
17		// is moved or changed in this method, and LeftFuture/RightFuture are properly pinned
18		// again before being polled.
19		// Future could be implemented for EitherFuture without the use of unsafe, but then it would
20		// only work for Futures that are Unpin. Using unsafe makes this work for !Unpin as well.
21		unsafe {
22			match Pin::get_unchecked_mut(self).0.as_mut() {
23				Either::Left(left_future) => {
24					let left_future = Pin::new_unchecked(left_future);
25					match left_future.poll(context) {
26						Poll::Ready(left) => Poll::Ready(Either::Left(left)),
27						Poll::Pending => Poll::Pending,
28					}
29				}
30				Either::Right(right_future) => {
31					let right_future = Pin::new_unchecked(right_future);
32					match right_future.poll(context) {
33						Poll::Ready(right) => Poll::Ready(Either::Right(right)),
34						Poll::Pending => Poll::Pending,
35					}
36				}
37			}
38		}
39	}
40}