mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2024-12-30 16:09:39 +00:00
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 <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
parent
7951f16b87
commit
c1b74eb8b1
1 changed files with 28 additions and 0 deletions
|
@ -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<T> {
|
||||
PollEventsOwned {
|
||||
count: self.count,
|
||||
events: RefCell::new(*self.events),
|
||||
tokens: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterates over each event.
|
||||
pub fn iter(&self) -> PollEventIter<slice::Iter<epoll_event>, 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<T> {
|
||||
count: usize,
|
||||
events: RefCell<[epoll_event; POLL_CONTEXT_MAX_EVENTS]>,
|
||||
tokens: PhantomData<T>, // Needed to satisfy usage of T
|
||||
}
|
||||
|
||||
impl<T: PollToken> PollEventsOwned<T> {
|
||||
/// Takes a reference to the events so that they can be iterated via methods in `PollEvents`.
|
||||
pub fn as_ref(&self) -> PollEvents<T> {
|
||||
PollEvents {
|
||||
count: self.count,
|
||||
events: self.events.borrow(),
|
||||
tokens: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Used to poll multiple objects that have file descriptors.
|
||||
///
|
||||
/// # Example
|
||||
|
|
Loading…
Reference in a new issue