diff options
| author | Alexander van Saase <[email protected]> | 2024-12-14 22:33:24 +0100 |
|---|---|---|
| committer | Alexander van Saase <[email protected]> | 2024-12-14 22:33:24 +0100 |
| commit | 1152148081c7c80e42d67d9517ae75b628ecf38b (patch) | |
| tree | ae1abd52b6ef02972c3f065c0b84a41a2d8146d8 | |
| parent | 45d9bd57575d9391a68334a2f3966b92032d5dc6 (diff) | |
Add is_x() methods for all EitherN enum variants
| -rw-r--r-- | embassy-futures/src/select.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/embassy-futures/src/select.rs b/embassy-futures/src/select.rs index bb175253b..014fee5d2 100644 --- a/embassy-futures/src/select.rs +++ b/embassy-futures/src/select.rs | |||
| @@ -14,6 +14,18 @@ pub enum Either<A, B> { | |||
| 14 | Second(B), | 14 | Second(B), |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | impl<A, B> Either<A, B> { | ||
| 18 | /// Did the first future complete first? | ||
| 19 | pub fn is_first(&self) -> bool { | ||
| 20 | matches!(self, Either::First(_)) | ||
| 21 | } | ||
| 22 | |||
| 23 | /// Did the second future complete first? | ||
| 24 | pub fn is_second(&self) -> bool { | ||
| 25 | matches!(self, Either::Second(_)) | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 17 | /// Wait for one of two futures to complete. | 29 | /// Wait for one of two futures to complete. |
| 18 | /// | 30 | /// |
| 19 | /// This function returns a new future which polls all the futures. | 31 | /// This function returns a new future which polls all the futures. |
| @@ -73,6 +85,23 @@ pub enum Either3<A, B, C> { | |||
| 73 | Third(C), | 85 | Third(C), |
| 74 | } | 86 | } |
| 75 | 87 | ||
| 88 | impl<A, B, C> Either3<A, B, C> { | ||
| 89 | /// Did the first future complete first? | ||
| 90 | pub fn is_first(&self) -> bool { | ||
| 91 | matches!(self, Either3::First(_)) | ||
| 92 | } | ||
| 93 | |||
| 94 | /// Did the second future complete first? | ||
| 95 | pub fn is_second(&self) -> bool { | ||
| 96 | matches!(self, Either3::Second(_)) | ||
| 97 | } | ||
| 98 | |||
| 99 | /// Did the third future complete first? | ||
| 100 | pub fn is_third(&self) -> bool { | ||
| 101 | matches!(self, Either3::Third(_)) | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 76 | /// Same as [`select`], but with more futures. | 105 | /// Same as [`select`], but with more futures. |
| 77 | pub fn select3<A, B, C>(a: A, b: B, c: C) -> Select3<A, B, C> | 106 | pub fn select3<A, B, C>(a: A, b: B, c: C) -> Select3<A, B, C> |
| 78 | where | 107 | where |
| @@ -134,6 +163,28 @@ pub enum Either4<A, B, C, D> { | |||
| 134 | Fourth(D), | 163 | Fourth(D), |
| 135 | } | 164 | } |
| 136 | 165 | ||
| 166 | impl<A, B, C, D> Either4<A, B, C, D> { | ||
| 167 | /// Did the first future complete first? | ||
| 168 | pub fn is_first(&self) -> bool { | ||
| 169 | matches!(self, Either4::First(_)) | ||
| 170 | } | ||
| 171 | |||
| 172 | /// Did the second future complete first? | ||
| 173 | pub fn is_second(&self) -> bool { | ||
| 174 | matches!(self, Either4::Second(_)) | ||
| 175 | } | ||
| 176 | |||
| 177 | /// Did the third future complete first? | ||
| 178 | pub fn is_third(&self) -> bool { | ||
| 179 | matches!(self, Either4::Third(_)) | ||
| 180 | } | ||
| 181 | |||
| 182 | /// Did the fourth future complete first? | ||
| 183 | pub fn is_fourth(&self) -> bool { | ||
| 184 | matches!(self, Either4::Fourth(_)) | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 137 | /// Same as [`select`], but with more futures. | 188 | /// Same as [`select`], but with more futures. |
| 138 | pub fn select4<A, B, C, D>(a: A, b: B, c: C, d: D) -> Select4<A, B, C, D> | 189 | pub fn select4<A, B, C, D>(a: A, b: B, c: C, d: D) -> Select4<A, B, C, D> |
| 139 | where | 190 | where |
| @@ -204,6 +255,33 @@ pub enum Either5<A, B, C, D, E> { | |||
| 204 | Fifth(E), | 255 | Fifth(E), |
| 205 | } | 256 | } |
| 206 | 257 | ||
| 258 | impl<A, B, C, D, E> Either5<A, B, C, D, E> { | ||
| 259 | /// Did the first future complete first? | ||
| 260 | pub fn is_first(&self) -> bool { | ||
| 261 | matches!(self, Either5::First(_)) | ||
| 262 | } | ||
| 263 | |||
| 264 | /// Did the second future complete first? | ||
| 265 | pub fn is_second(&self) -> bool { | ||
| 266 | matches!(self, Either5::Second(_)) | ||
| 267 | } | ||
| 268 | |||
| 269 | /// Did the third future complete first? | ||
| 270 | pub fn is_third(&self) -> bool { | ||
| 271 | matches!(self, Either5::Third(_)) | ||
| 272 | } | ||
| 273 | |||
| 274 | /// Did the fourth future complete first? | ||
| 275 | pub fn is_fourth(&self) -> bool { | ||
| 276 | matches!(self, Either5::Fourth(_)) | ||
| 277 | } | ||
| 278 | |||
| 279 | /// Did the fifth future complete first? | ||
| 280 | pub fn is_fifth(&self) -> bool { | ||
| 281 | matches!(self, Either5::Fifth(_)) | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 207 | /// Same as [`select`], but with more futures. | 285 | /// Same as [`select`], but with more futures. |
| 208 | pub fn select5<A, B, C, D, E>(a: A, b: B, c: C, d: D, e: E) -> Select5<A, B, C, D, E> | 286 | pub fn select5<A, B, C, D, E>(a: A, b: B, c: C, d: D, e: E) -> Select5<A, B, C, D, E> |
| 209 | where | 287 | where |
| @@ -283,6 +361,38 @@ pub enum Either6<A, B, C, D, E, F> { | |||
| 283 | Sixth(F), | 361 | Sixth(F), |
| 284 | } | 362 | } |
| 285 | 363 | ||
| 364 | impl<A, B, C, D, E, F> Either6<A, B, C, D, E, F> { | ||
| 365 | /// Did the first future complete first? | ||
| 366 | pub fn is_first(&self) -> bool { | ||
| 367 | matches!(self, Either6::First(_)) | ||
| 368 | } | ||
| 369 | |||
| 370 | /// Did the second future complete first? | ||
| 371 | pub fn is_second(&self) -> bool { | ||
| 372 | matches!(self, Either6::Second(_)) | ||
| 373 | } | ||
| 374 | |||
| 375 | /// Did the third future complete first? | ||
| 376 | pub fn is_third(&self) -> bool { | ||
| 377 | matches!(self, Either6::Third(_)) | ||
| 378 | } | ||
| 379 | |||
| 380 | /// Did the fourth future complete first? | ||
| 381 | pub fn is_fourth(&self) -> bool { | ||
| 382 | matches!(self, Either6::Fourth(_)) | ||
| 383 | } | ||
| 384 | |||
| 385 | /// Did the fifth future complete first? | ||
| 386 | pub fn is_fifth(&self) -> bool { | ||
| 387 | matches!(self, Either6::Fifth(_)) | ||
| 388 | } | ||
| 389 | |||
| 390 | /// Did the sixth future complete first? | ||
| 391 | pub fn is_sixth(&self) -> bool { | ||
| 392 | matches!(self, Either6::Sixth(_)) | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 286 | /// Same as [`select`], but with more futures. | 396 | /// Same as [`select`], but with more futures. |
| 287 | 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> | 397 | 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> |
| 288 | where | 398 | where |
