Skip to content

Commit

Permalink
Merge pull request #3650 from avsaase/either-is
Browse files Browse the repository at this point in the history
Add is_x() methods for all EitherN enum variants
  • Loading branch information
lulf authored Dec 20, 2024
2 parents ce4aaba + 1152148 commit c775604
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions embassy-futures/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c775604

Please sign in to comment.