Add is_x() methods for all EitherN enum variants

This commit is contained in:
Alexander van Saase 2024-12-14 22:33:24 +01:00
parent 45d9bd5757
commit 1152148081

View File

@ -14,6 +14,18 @@ pub enum Either<A, B> {
Second(B),
}
impl<A, B> Either<A, B> {
/// Did the first future complete first?
pub fn is_first(&self) -> bool {
matches!(self, Either::First(_))
}
/// Did the second future complete first?
pub fn is_second(&self) -> bool {
matches!(self, Either::Second(_))
}
}
/// Wait for one of two futures to complete.
///
/// This function returns a new future which polls all the futures.
@ -73,6 +85,23 @@ pub enum Either3<A, B, C> {
Third(C),
}
impl<A, B, C> Either3<A, B, C> {
/// Did the first future complete first?
pub fn is_first(&self) -> bool {
matches!(self, Either3::First(_))
}
/// Did the second future complete first?
pub fn is_second(&self) -> bool {
matches!(self, Either3::Second(_))
}
/// Did the third future complete first?
pub fn is_third(&self) -> bool {
matches!(self, Either3::Third(_))
}
}
/// Same as [`select`], but with more futures.
pub fn select3<A, B, C>(a: A, b: B, c: C) -> Select3<A, B, C>
where
@ -134,6 +163,28 @@ pub enum Either4<A, B, C, D> {
Fourth(D),
}
impl<A, B, C, D> Either4<A, B, C, D> {
/// Did the first future complete first?
pub fn is_first(&self) -> bool {
matches!(self, Either4::First(_))
}
/// Did the second future complete first?
pub fn is_second(&self) -> bool {
matches!(self, Either4::Second(_))
}
/// Did the third future complete first?
pub fn is_third(&self) -> bool {
matches!(self, Either4::Third(_))
}
/// Did the fourth future complete first?
pub fn is_fourth(&self) -> bool {
matches!(self, Either4::Fourth(_))
}
}
/// Same as [`select`], but with more futures.
pub fn select4<A, B, C, D>(a: A, b: B, c: C, d: D) -> Select4<A, B, C, D>
where
@ -204,6 +255,33 @@ pub enum Either5<A, B, C, D, E> {
Fifth(E),
}
impl<A, B, C, D, E> Either5<A, B, C, D, E> {
/// Did the first future complete first?
pub fn is_first(&self) -> bool {
matches!(self, Either5::First(_))
}
/// Did the second future complete first?
pub fn is_second(&self) -> bool {
matches!(self, Either5::Second(_))
}
/// Did the third future complete first?
pub fn is_third(&self) -> bool {
matches!(self, Either5::Third(_))
}
/// Did the fourth future complete first?
pub fn is_fourth(&self) -> bool {
matches!(self, Either5::Fourth(_))
}
/// Did the fifth future complete first?
pub fn is_fifth(&self) -> bool {
matches!(self, Either5::Fifth(_))
}
}
/// Same as [`select`], but with more futures.
pub fn select5<A, B, C, D, E>(a: A, b: B, c: C, d: D, e: E) -> Select5<A, B, C, D, E>
where
@ -283,6 +361,38 @@ pub enum Either6<A, B, C, D, E, F> {
Sixth(F),
}
impl<A, B, C, D, E, F> Either6<A, B, C, D, E, F> {
/// Did the first future complete first?
pub fn is_first(&self) -> bool {
matches!(self, Either6::First(_))
}
/// Did the second future complete first?
pub fn is_second(&self) -> bool {
matches!(self, Either6::Second(_))
}
/// Did the third future complete first?
pub fn is_third(&self) -> bool {
matches!(self, Either6::Third(_))
}
/// Did the fourth future complete first?
pub fn is_fourth(&self) -> bool {
matches!(self, Either6::Fourth(_))
}
/// Did the fifth future complete first?
pub fn is_fifth(&self) -> bool {
matches!(self, Either6::Fifth(_))
}
/// Did the sixth future complete first?
pub fn is_sixth(&self) -> bool {
matches!(self, Either6::Sixth(_))
}
}
/// Same as [`select`], but with more futures.
pub fn select6<A, B, C, D, E, F>(a: A, b: B, c: C, d: D, e: E, f: F) -> Select6<A, B, C, D, E, F>
where