diff options
author | Zach Reizner <zachr@google.com> | 2019-07-23 15:45:42 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-07-24 06:07:19 +0000 |
commit | 229063c2bf2ee365458c8fd5cdda2ab27a23bf98 (patch) | |
tree | ac1b6bd7bbf4e7fdab7f4d7ad41deaf26c1505ac | |
parent | 2f0c0b3f5dc40307af855b394079e104857d4cc2 (diff) | |
download | crosvm-229063c2bf2ee365458c8fd5cdda2ab27a23bf98.tar crosvm-229063c2bf2ee365458c8fd5cdda2ab27a23bf98.tar.gz crosvm-229063c2bf2ee365458c8fd5cdda2ab27a23bf98.tar.bz2 crosvm-229063c2bf2ee365458c8fd5cdda2ab27a23bf98.tar.lz crosvm-229063c2bf2ee365458c8fd5cdda2ab27a23bf98.tar.xz crosvm-229063c2bf2ee365458c8fd5cdda2ab27a23bf98.tar.zst crosvm-229063c2bf2ee365458c8fd5cdda2ab27a23bf98.zip |
sys_util: poll: add build_with and add_many helper functions
These functions are wrappers around multiple `add` calls that will fail at the first error. This replaces lots of ugly `and_then`, `and`, and `ok` calls that had been sprinkled around the to initialize a `PollContext`. TEST=cargo test -p sys_util ./build_test BUG=None Change-Id: I69aa1c9ad87677cf220eda57148ff8eb2268bf67 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1715580 Tested-by: Zach Reizner <zachr@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Auto-Submit: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org> Commit-Queue: Zach Reizner <zachr@chromium.org>
-rw-r--r-- | sys_util/src/poll.rs | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/sys_util/src/poll.rs b/sys_util/src/poll.rs index 0da0831..7c09bb7 100644 --- a/sys_util/src/poll.rs +++ b/sys_util/src/poll.rs @@ -296,6 +296,29 @@ impl<T: PollToken> EpollContext<T> { }) } + /// Creates a new `EpollContext` and adds the slice of `fd` and `token` tuples to the new + /// context. + /// + /// This is equivalent to calling `new` followed by `add_many`. If there is an error, this will + /// return the error instead of the new context. + pub fn build_with(fd_tokens: &[(&dyn AsRawFd, T)]) -> Result<EpollContext<T>> { + let ctx = EpollContext::new()?; + ctx.add_many(fd_tokens)?; + Ok(ctx) + } + + /// Adds the given slice of `fd` and `token` tuples to this context. + /// + /// This is equivalent to calling `add` with each `fd` and `token`. If there are any errors, + /// this method will stop adding `fd`s and return the first error, leaving this context in a + /// undefined state. + pub fn add_many(&self, fd_tokens: &[(&dyn AsRawFd, T)]) -> Result<()> { + for (fd, token) in fd_tokens { + self.add(*fd, T::from_raw_token(token.as_raw_token()))?; + } + Ok(()) + } + /// Adds the given `fd` to this context and associates the given `token` with the `fd`'s /// readable events. /// @@ -505,6 +528,29 @@ impl<T: PollToken> PollContext<T> { }) } + /// Creates a new `PollContext` and adds the slice of `fd` and `token` tuples to the new + /// context. + /// + /// This is equivalent to calling `new` followed by `add_many`. If there is an error, this will + /// return the error instead of the new context. + pub fn build_with(fd_tokens: &[(&dyn AsRawFd, T)]) -> Result<PollContext<T>> { + let ctx = PollContext::new()?; + ctx.add_many(fd_tokens)?; + Ok(ctx) + } + + /// Adds the given slice of `fd` and `token` tuples to this context. + /// + /// This is equivalent to calling `add` with each `fd` and `token`. If there are any errors, + /// this method will stop adding `fd`s and return the first error, leaving this context in a + /// undefined state. + pub fn add_many(&self, fd_tokens: &[(&dyn AsRawFd, T)]) -> Result<()> { + for (fd, token) in fd_tokens { + self.add(*fd, T::from_raw_token(token.as_raw_token()))?; + } + Ok(()) + } + /// Adds the given `fd` to this context and associates the given `token` with the `fd`'s /// readable events. /// @@ -643,9 +689,7 @@ mod tests { let evt2 = EventFd::new().unwrap(); evt1.write(1).unwrap(); evt2.write(1).unwrap(); - let ctx: PollContext<u32> = PollContext::new().unwrap(); - ctx.add(&evt1, 1).unwrap(); - ctx.add(&evt2, 2).unwrap(); + let ctx: PollContext<u32> = PollContext::build_with(&[(&evt1, 1), (&evt2, 2)]).unwrap(); let mut evt_count = 0; while evt_count < 2 { |