Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_x() methods for all EitherN enum variants #3650

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading