leodos_libcfs/runtime/
select_either.rs1use core::future;
4use core::future::Future;
5use core::task::Context;
6use core::task::Poll;
7
8use pin_utils::pin_mut;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum Either<A, B> {
13 Left(A),
15 Right(B),
17}
18
19pub async fn select_either<'a, F1, F2>(future1: F1, future2: F2) -> Either<F1::Output, F2::Output>
24where
25 F1: Future + 'a,
26 F2: Future + 'a,
27{
28 pin_mut!(future1);
30 pin_mut!(future2);
31
32 future::poll_fn(|cx: &mut Context<'_>| {
33 if let Poll::Ready(output) = future1.as_mut().poll(cx) {
34 return Poll::Ready(Either::Left(output));
35 }
36
37 if let Poll::Ready(output) = future2.as_mut().poll(cx) {
38 return Poll::Ready(Either::Right(output));
39 }
40
41 Poll::Pending
42 })
43 .await
44}