Skip to content

Commit

Permalink
Add opt-in handlers for before sleeping and before handling events (#144
Browse files Browse the repository at this point in the history
)

* Add opt-in handlers for before sleeping and before handling events

* Remove pre_run and fix some missed impls

* Appease the great 📎

* Use an associated constant instead

* Do the borrow outside the loop

* Fix cfged out mistake

* Update documentation to reflect changes

* Actually use the synthetic events

* Add some tests for the new functionality

* Pin `nix` to a lower version

* Rename `before_will_sleep` to `before_sleep`

* Add some more coverage

* Solve review comments

* Restore inconsistencies in formatting

* Remove AdditionalLifetimeEventsRegister

* Remove unused lifetime

* Unencapsulate the refcell

* Address review comments

* Exclude synthetic events from the iterator

* Follow clippy's improvement (?)
  • Loading branch information
DJMcNab authored Sep 9, 2023
1 parent d468cc9 commit 6319d81
Show file tree
Hide file tree
Showing 5 changed files with 589 additions and 131 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

- Bump MSRV to 1.63
- Make signals an optional feature under the `signals` features.
- Replace the `nix` crate with standard library I/O errors and the `rustix`
crate.
- Replace the `nix` crate with standard library I/O errors and the `rustix` crate.
- `pre_run` and `post_run` on `EventSource` have been replaced with `before_sleep` and `before_handle_events`, respectively.
These are now opt-in through the `NEEDS_EXTRA_LIFECYCLE_EVENTS` associated constant, and occur at slightly different times to
the methods they are replacing. This allows greater compatibility with Wayland based event sources.

## 0.11.0 -- 2023-06-05

Expand Down
31 changes: 23 additions & 8 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ use rustix::fs::{fcntl_getfl, fcntl_setfl, OFlags};
#[cfg(feature = "futures-io")]
use futures_io::{AsyncRead, AsyncWrite, IoSlice, IoSliceMut};

use crate::loop_logic::EventIterator;
use crate::{
loop_logic::{LoopInner, MAX_SOURCES_MASK},
sources::EventDispatcher,
Interest, Mode, Poll, PostAction, Readiness, Token, TokenFactory,
};
use crate::{AdditionalLifecycleEventsSet, RegistrationToken};

/// Adapter for async IO manipulations
///
Expand Down Expand Up @@ -237,30 +239,43 @@ impl<Data> EventDispatcher<Data> for RefCell<IoDispatcher> {
Ok(PostAction::Continue)
}

fn register(&self, _: &mut Poll, _: &mut TokenFactory) -> crate::Result<()> {
fn register(
&self,
_: &mut Poll,
_: &mut AdditionalLifecycleEventsSet,
_: &mut TokenFactory,
) -> crate::Result<()> {
// registration is handled by IoLoopInner
unreachable!()
}

fn reregister(&self, _: &mut Poll, _: &mut TokenFactory) -> crate::Result<bool> {
fn reregister(
&self,
_: &mut Poll,
_: &mut AdditionalLifecycleEventsSet,
_: &mut TokenFactory,
) -> crate::Result<bool> {
// registration is handled by IoLoopInner
unreachable!()
}

fn unregister(&self, poll: &mut Poll) -> crate::Result<bool> {
fn unregister(
&self,
poll: &mut Poll,
_: &mut AdditionalLifecycleEventsSet,
_: RegistrationToken,
) -> crate::Result<bool> {
let disp = self.borrow();
if disp.is_registered {
poll.unregister(unsafe { BorrowedFd::borrow_raw(disp.fd) })?;
}
Ok(true)
}

fn pre_run(&self, _data: &mut Data) -> crate::Result<()> {
Ok(())
}
fn post_run(&self, _data: &mut Data) -> crate::Result<()> {
Ok(())
fn before_sleep(&self) -> crate::Result<Option<(Readiness, Token)>> {
Ok(None)
}
fn before_handle_events(&self, _: EventIterator<'_>) {}
}

/*
Expand Down
Loading

0 comments on commit 6319d81

Please sign in to comment.