diff options
| author | Christian Enderle <[email protected]> | 2024-10-19 15:36:56 +0200 |
|---|---|---|
| committer | Christian Enderle <[email protected]> | 2024-10-19 15:36:56 +0200 |
| commit | f21b19164be17df930aa557a90b3c5ab1dc39b9e (patch) | |
| tree | 46bf94d6dbd9b5e6c58d0ff75a6245f6b85ad621 /embassy-futures | |
| parent | 8dde7b625eed78271fec8f69ffa370e55c9dda9e (diff) | |
embassy-futures: add select 5 and 6
Diffstat (limited to 'embassy-futures')
| -rw-r--r-- | embassy-futures/src/select.rs | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/embassy-futures/src/select.rs b/embassy-futures/src/select.rs index 57f0cb41f..bb175253b 100644 --- a/embassy-futures/src/select.rs +++ b/embassy-futures/src/select.rs | |||
| @@ -188,6 +188,169 @@ where | |||
| 188 | 188 | ||
| 189 | // ==================================================================== | 189 | // ==================================================================== |
| 190 | 190 | ||
| 191 | /// Result for [`select5`]. | ||
| 192 | #[derive(Debug, Clone)] | ||
| 193 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 194 | pub enum Either5<A, B, C, D, E> { | ||
| 195 | /// First future finished first. | ||
| 196 | First(A), | ||
| 197 | /// Second future finished first. | ||
| 198 | Second(B), | ||
| 199 | /// Third future finished first. | ||
| 200 | Third(C), | ||
| 201 | /// Fourth future finished first. | ||
| 202 | Fourth(D), | ||
| 203 | /// Fifth future finished first. | ||
| 204 | Fifth(E), | ||
| 205 | } | ||
| 206 | |||
| 207 | /// 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> | ||
| 209 | where | ||
| 210 | A: Future, | ||
| 211 | B: Future, | ||
| 212 | C: Future, | ||
| 213 | D: Future, | ||
| 214 | E: Future, | ||
| 215 | { | ||
| 216 | Select5 { a, b, c, d, e } | ||
| 217 | } | ||
| 218 | |||
| 219 | /// Future for the [`select5`] function. | ||
| 220 | #[derive(Debug)] | ||
| 221 | #[must_use = "futures do nothing unless you `.await` or poll them"] | ||
| 222 | pub struct Select5<A, B, C, D, E> { | ||
| 223 | a: A, | ||
| 224 | b: B, | ||
| 225 | c: C, | ||
| 226 | d: D, | ||
| 227 | e: E, | ||
| 228 | } | ||
| 229 | |||
| 230 | impl<A, B, C, D, E> Future for Select5<A, B, C, D, E> | ||
| 231 | where | ||
| 232 | A: Future, | ||
| 233 | B: Future, | ||
| 234 | C: Future, | ||
| 235 | D: Future, | ||
| 236 | E: Future, | ||
| 237 | { | ||
| 238 | type Output = Either5<A::Output, B::Output, C::Output, D::Output, E::Output>; | ||
| 239 | |||
| 240 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| 241 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 242 | let a = unsafe { Pin::new_unchecked(&mut this.a) }; | ||
| 243 | let b = unsafe { Pin::new_unchecked(&mut this.b) }; | ||
| 244 | let c = unsafe { Pin::new_unchecked(&mut this.c) }; | ||
| 245 | let d = unsafe { Pin::new_unchecked(&mut this.d) }; | ||
| 246 | let e = unsafe { Pin::new_unchecked(&mut this.e) }; | ||
| 247 | if let Poll::Ready(x) = a.poll(cx) { | ||
| 248 | return Poll::Ready(Either5::First(x)); | ||
| 249 | } | ||
| 250 | if let Poll::Ready(x) = b.poll(cx) { | ||
| 251 | return Poll::Ready(Either5::Second(x)); | ||
| 252 | } | ||
| 253 | if let Poll::Ready(x) = c.poll(cx) { | ||
| 254 | return Poll::Ready(Either5::Third(x)); | ||
| 255 | } | ||
| 256 | if let Poll::Ready(x) = d.poll(cx) { | ||
| 257 | return Poll::Ready(Either5::Fourth(x)); | ||
| 258 | } | ||
| 259 | if let Poll::Ready(x) = e.poll(cx) { | ||
| 260 | return Poll::Ready(Either5::Fifth(x)); | ||
| 261 | } | ||
| 262 | Poll::Pending | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | // ==================================================================== | ||
| 267 | |||
| 268 | /// Result for [`select6`]. | ||
| 269 | #[derive(Debug, Clone)] | ||
| 270 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 271 | pub enum Either6<A, B, C, D, E, F> { | ||
| 272 | /// First future finished first. | ||
| 273 | First(A), | ||
| 274 | /// Second future finished first. | ||
| 275 | Second(B), | ||
| 276 | /// Third future finished first. | ||
| 277 | Third(C), | ||
| 278 | /// Fourth future finished first. | ||
| 279 | Fourth(D), | ||
| 280 | /// Fifth future finished first. | ||
| 281 | Fifth(E), | ||
| 282 | /// Sixth future finished first. | ||
| 283 | Sixth(F), | ||
| 284 | } | ||
| 285 | |||
| 286 | /// 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> | ||
| 288 | where | ||
| 289 | A: Future, | ||
| 290 | B: Future, | ||
| 291 | C: Future, | ||
| 292 | D: Future, | ||
| 293 | E: Future, | ||
| 294 | F: Future, | ||
| 295 | { | ||
| 296 | Select6 { a, b, c, d, e, f } | ||
| 297 | } | ||
| 298 | |||
| 299 | /// Future for the [`select6`] function. | ||
| 300 | #[derive(Debug)] | ||
| 301 | #[must_use = "futures do nothing unless you `.await` or poll them"] | ||
| 302 | pub struct Select6<A, B, C, D, E, F> { | ||
| 303 | a: A, | ||
| 304 | b: B, | ||
| 305 | c: C, | ||
| 306 | d: D, | ||
| 307 | e: E, | ||
| 308 | f: F, | ||
| 309 | } | ||
| 310 | |||
| 311 | impl<A, B, C, D, E, F> Future for Select6<A, B, C, D, E, F> | ||
| 312 | where | ||
| 313 | A: Future, | ||
| 314 | B: Future, | ||
| 315 | C: Future, | ||
| 316 | D: Future, | ||
| 317 | E: Future, | ||
| 318 | F: Future, | ||
| 319 | { | ||
| 320 | type Output = Either6<A::Output, B::Output, C::Output, D::Output, E::Output, F::Output>; | ||
| 321 | |||
| 322 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| 323 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 324 | let a = unsafe { Pin::new_unchecked(&mut this.a) }; | ||
| 325 | let b = unsafe { Pin::new_unchecked(&mut this.b) }; | ||
| 326 | let c = unsafe { Pin::new_unchecked(&mut this.c) }; | ||
| 327 | let d = unsafe { Pin::new_unchecked(&mut this.d) }; | ||
| 328 | let e = unsafe { Pin::new_unchecked(&mut this.e) }; | ||
| 329 | let f = unsafe { Pin::new_unchecked(&mut this.f) }; | ||
| 330 | if let Poll::Ready(x) = a.poll(cx) { | ||
| 331 | return Poll::Ready(Either6::First(x)); | ||
| 332 | } | ||
| 333 | if let Poll::Ready(x) = b.poll(cx) { | ||
| 334 | return Poll::Ready(Either6::Second(x)); | ||
| 335 | } | ||
| 336 | if let Poll::Ready(x) = c.poll(cx) { | ||
| 337 | return Poll::Ready(Either6::Third(x)); | ||
| 338 | } | ||
| 339 | if let Poll::Ready(x) = d.poll(cx) { | ||
| 340 | return Poll::Ready(Either6::Fourth(x)); | ||
| 341 | } | ||
| 342 | if let Poll::Ready(x) = e.poll(cx) { | ||
| 343 | return Poll::Ready(Either6::Fifth(x)); | ||
| 344 | } | ||
| 345 | if let Poll::Ready(x) = f.poll(cx) { | ||
| 346 | return Poll::Ready(Either6::Sixth(x)); | ||
| 347 | } | ||
| 348 | Poll::Pending | ||
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | // ==================================================================== | ||
| 353 | |||
| 191 | /// Future for the [`select_array`] function. | 354 | /// Future for the [`select_array`] function. |
| 192 | #[derive(Debug)] | 355 | #[derive(Debug)] |
| 193 | #[must_use = "futures do nothing unless you `.await` or poll them"] | 356 | #[must_use = "futures do nothing unless you `.await` or poll them"] |
