Skip to content

Commit

Permalink
Implement AsRawFd/AsFd for EventLoop<'l, Data>
Browse files Browse the repository at this point in the history
This should allow inserting one `EventLoop` into another.
  • Loading branch information
elinorbgr committed Oct 10, 2023
1 parent b55175c commit 04463bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Additions

- `Token` and `RegistrationToken` are now invalidated when the event source they represent is removed from the event loop.
- Implement `AsRawFd` and `AsFd` for `EventLoop<'l, Data>`

#### Bugfixes

Expand Down
31 changes: 30 additions & 1 deletion src/loop_logic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cell::{Cell, RefCell};
use std::fmt::Debug;
use std::os::unix::io::AsFd;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand All @@ -11,6 +11,7 @@ use std::{io, slice};
use std::future::Future;

use log::trace;
use polling::Poller;

use crate::list::{SourceEntry, SourceList};
use crate::sources::{Dispatcher, EventSource, Idle, IdleDispatcher};
Expand Down Expand Up @@ -286,6 +287,7 @@ impl<'l, Data> LoopHandle<'l, Data> {
///
/// This loop can host several event sources, that can be dynamically added or removed.
pub struct EventLoop<'l, Data> {
poller: Arc<Poller>,
handle: LoopHandle<'l, Data>,
signals: Arc<Signals>,
// A caching vector for synthetic poll events
Expand Down Expand Up @@ -315,6 +317,7 @@ impl<'l, Data> EventLoop<'l, Data> {
/// Fails if the initialization of the polling system failed.
pub fn try_new() -> crate::Result<Self> {
let poll = Poll::new()?;
let poller = poll.poller.clone();
let handle = LoopHandle {
inner: Rc::new(LoopInner {
poll: RefCell::new(poll),
Expand All @@ -332,6 +335,7 @@ impl<'l, Data> EventLoop<'l, Data> {
#[cfg(feature = "block_on")]
future_ready: AtomicBool::new(false),
}),
poller,
synthetic_events: vec![],
})
}
Expand Down Expand Up @@ -660,6 +664,31 @@ 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.poller.as_raw_fd()
}
}

impl<'l, Data> AsFd for EventLoop<'l, Data> {
/// Get the underlying fd of the poller.
///
/// This could be used to create [`Generic`] source out of the current loop
/// and inserting into some other [`EventLoop`].
///
/// [`Generic`]: crate::generic::Generic
fn as_fd(&self) -> BorrowedFd<'_> {
self.poller.as_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
Expand Down
2 changes: 1 addition & 1 deletion src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,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<Poller>,
pub(crate) poller: Arc<Poller>,

/// The buffer of events returned by the poller.
events: RefCell<Events>,
Expand Down

0 comments on commit 04463bf

Please sign in to comment.