From db238ed17ffacc08ab491cdb65511648bf2dbda0 Mon Sep 17 00:00:00 2001 From: Kirill Chibisov Date: Mon, 9 Oct 2023 20:44:22 +0400 Subject: [PATCH] Implement `AsRawFd` for `EventLoop<'l, Data>` This should allow inserting one `EventLoop` into the other. --- CHANGELOG.md | 4 ++++ src/loop_logic.rs | 14 ++++++++++++++ src/sys.rs | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc6e702c..71001277 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +#### Additions + +- Implement `AsRawFd` for `EventLoop<'l, Data>` + #### Bugfixes - Fix an issue, where id-reuse could execute a PostAction on a newly registered event source diff --git a/src/loop_logic.rs b/src/loop_logic.rs index 258848c5..dca97fb6 100644 --- a/src/loop_logic.rs +++ b/src/loop_logic.rs @@ -1,6 +1,7 @@ use std::cell::{Cell, RefCell}; use std::fmt::Debug; use std::os::unix::io::AsFd; +use std::os::unix::io::{AsRawFd, RawFd}; use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -671,6 +672,19 @@ impl<'l, Data> EventLoop<'l, Data> { } } +impl<'l, Data> AsRawFd for EventLoop<'l, Data> { + /// Get the underlying raw-fd of the poller. + /// + /// This could be used to create [`Generic`] source out of the current loop + /// and inserting into some other [`EventLoop`]. It's recommended to clone `fd` + /// before doing so. + /// + /// [`Generic`]: crate::generic::Generic + fn as_raw_fd(&self) -> RawFd { + self.handle.inner.poll.borrow().poller.as_raw_fd() + } +} + #[derive(Clone, Debug)] /// The EventIterator is an `Iterator` over the events relevant to a particular source /// This type is used in the [`EventSource::before_handle_events`] methods for diff --git a/src/sys.rs b/src/sys.rs index 35e39fb3..dd2b94d7 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -180,7 +180,7 @@ pub struct Token { /// source and delegate the implementations to it. pub struct Poll { /// The handle to wepoll/epoll/kqueue/... used to poll for events. - poller: Arc, + pub(crate) poller: Arc, /// The buffer of events returned by the poller. events: RefCell,