From c1b74eb8b1d123a940cabefc7be864cf33d74d00 Mon Sep 17 00:00:00 2001 From: Zach Reizner Date: Fri, 6 Apr 2018 11:55:23 -0700 Subject: [PATCH] sys_util: add method for copying PollEvents Making a copy of PollEvents is useful to drop the PollEvents structure which borrows from a PollContext. Even though immutably borrowing from a PollContext does not prevent any operations on a PollContext, it does prevent mutable method calls on any structure that owns PollContext. TEST=None BUG=chromium:816692 Change-Id: I9527fd5c122a703933deb973ad549b792226e4c6 Reviewed-on: https://chromium-review.googlesource.com/1000101 Commit-Ready: Zach Reizner Tested-by: Zach Reizner Reviewed-by: Dylan Reid --- sys_util/src/poll.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sys_util/src/poll.rs b/sys_util/src/poll.rs index 96ab22d921..a52e2b019e 100644 --- a/sys_util/src/poll.rs +++ b/sys_util/src/poll.rs @@ -288,6 +288,16 @@ pub struct PollEvents<'a, T> { } impl<'a, T: PollToken> PollEvents<'a, T> { + /// Copies the events to an owned structure so the reference to this (and by extension + /// `PollContext`) can be dropped. + pub fn to_owned(&self) -> PollEventsOwned { + PollEventsOwned { + count: self.count, + events: RefCell::new(*self.events), + tokens: PhantomData, + } + } + /// Iterates over each event. pub fn iter(&self) -> PollEventIter, T> { PollEventIter { @@ -316,6 +326,24 @@ impl<'a, T: PollToken> PollEvents<'a, T> { } } +/// A deep copy of the event records from `PollEvents`. +pub struct PollEventsOwned { + count: usize, + events: RefCell<[epoll_event; POLL_CONTEXT_MAX_EVENTS]>, + tokens: PhantomData, // Needed to satisfy usage of T +} + +impl PollEventsOwned { + /// Takes a reference to the events so that they can be iterated via methods in `PollEvents`. + pub fn as_ref(&self) -> PollEvents { + PollEvents { + count: self.count, + events: self.events.borrow(), + tokens: PhantomData, + } + } +} + /// Used to poll multiple objects that have file descriptors. /// /// # Example