diff options
author | Dylan Reid <dgreid@chromium.org> | 2020-04-26 07:02:48 +0000 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-05-01 17:55:47 +0000 |
commit | f990be36d06bd2826482184f6b2b8c9b3fc4c49b (patch) | |
tree | 17006e222cc9247eade401a7e713acc77434f742 /cros_async | |
parent | 9fec3733afbdb5452b3502aa5502c82c15e5ebe3 (diff) | |
download | crosvm-f990be36d06bd2826482184f6b2b8c9b3fc4c49b.tar crosvm-f990be36d06bd2826482184f6b2b8c9b3fc4c49b.tar.gz crosvm-f990be36d06bd2826482184f6b2b8c9b3fc4c49b.tar.bz2 crosvm-f990be36d06bd2826482184f6b2b8c9b3fc4c49b.tar.lz crosvm-f990be36d06bd2826482184f6b2b8c9b3fc4c49b.tar.xz crosvm-f990be36d06bd2826482184f6b2b8c9b3fc4c49b.tar.zst crosvm-f990be36d06bd2826482184f6b2b8c9b3fc4c49b.zip |
cros_async: Add select6
Balloon needs six futures to be selected between, allow that with a new macro. TEST=added doc test Change-Id: I6c15ae5e2f6a8dcbbf0e0700e6b64fb5ca459f97 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2167693 Reviewed-by: Dylan Reid <dgreid@chromium.org> Tested-by: Dylan Reid <dgreid@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Dylan Reid <dgreid@chromium.org>
Diffstat (limited to 'cros_async')
-rw-r--r-- | cros_async/src/lib.rs | 55 | ||||
-rw-r--r-- | cros_async/src/select.rs | 3 |
2 files changed, 58 insertions, 0 deletions
diff --git a/cros_async/src/lib.rs b/cros_async/src/lib.rs index f671a0c..c3450bc 100644 --- a/cros_async/src/lib.rs +++ b/cros_async/src/lib.rs @@ -238,6 +238,61 @@ pub fn select5< FdExecutor::new(select::Select5::new(f1, f2, f3, f4, f5)).and_then(|mut f| f.run()) } +/// Creates an executor that runs the six given futures until one or more completes, returning a +/// tuple containing the result of the finished future(s) and the still pending future(s). +/// +/// # Example +/// +/// ``` +/// use cros_async::{empty_executor, Executor, select6, SelectResult}; +/// use cros_async::fd_executor::add_future; +/// use futures::future::pending; +/// use futures::pin_mut; +/// +/// let first = async {1}; +/// let second = async {let () = pending().await;}; +/// let third = async {3}; +/// let fourth = async {let () = pending().await;}; +/// let fifth = async {5}; +/// let sixth = async {6}; +/// pin_mut!(first); +/// pin_mut!(second); +/// pin_mut!(third); +/// pin_mut!(fourth); +/// pin_mut!(fifth); +/// pin_mut!(sixth); +/// match select6(first, second, third, fourth, fifth, sixth) { +/// Ok((SelectResult::Finished(1), SelectResult::Pending(_second), +/// SelectResult::Finished(3), SelectResult::Pending(_fourth), +/// SelectResult::Finished(5), SelectResult::Finished(6))) => (), +/// _ => panic!("Select didn't return the futures"), +/// }; +/// ``` +pub fn select6< + F1: Future + Unpin, + F2: Future + Unpin, + F3: Future + Unpin, + F4: Future + Unpin, + F5: Future + Unpin, + F6: Future + Unpin, +>( + f1: F1, + f2: F2, + f3: F3, + f4: F4, + f5: F5, + f6: F6, +) -> Result<( + SelectResult<F1>, + SelectResult<F2>, + SelectResult<F3>, + SelectResult<F4>, + SelectResult<F5>, + SelectResult<F6>, +)> { + FdExecutor::new(select::Select6::new(f1, f2, f3, f4, f5, f6)).and_then(|mut f| f.run()) +} + // Combination helpers to run until all futures are complete. /// Creates an executor that runs the two given futures to completion, returning a tuple of the diff --git a/cros_async/src/select.rs b/cros_async/src/select.rs index 28907c7..b10bc2c 100644 --- a/cros_async/src/select.rs +++ b/cros_async/src/select.rs @@ -106,4 +106,7 @@ generate! { /// _Future for the [`select5`] function. (Select5, <_Fut1, _Fut2, _Fut3, _Fut4, _Fut5>), + + /// _Future for the [`select6`] function. + (Select6, <_Fut1, _Fut2, _Fut3, _Fut4, _Fut5, _Fut6>), } |