From 229063c2bf2ee365458c8fd5cdda2ab27a23bf98 Mon Sep 17 00:00:00 2001 From: Zach Reizner Date: Tue, 23 Jul 2019 15:45:42 -0700 Subject: 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 Tested-by: kokoro Auto-Submit: Zach Reizner Reviewed-by: Dylan Reid Commit-Queue: Zach Reizner --- sys_util/src/poll.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'sys_util') 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 EpollContext { }) } + /// 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> { + 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 PollContext { }) } + /// 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> { + 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 = PollContext::new().unwrap(); - ctx.add(&evt1, 1).unwrap(); - ctx.add(&evt2, 2).unwrap(); + let ctx: PollContext = PollContext::build_with(&[(&evt1, 1), (&evt2, 2)]).unwrap(); let mut evt_count = 0; while evt_count < 2 { -- cgit 1.4.1