Skip to content

Commit

Permalink
Merge pull request #574 from zeenix/event-listener-5
Browse files Browse the repository at this point in the history
Port to event-listener 5.0
  • Loading branch information
zeenix authored Feb 7, 2024
2 parents fc8ea5b + a43967b commit 9ca27bd
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 82 deletions.
6 changes: 3 additions & 3 deletions book/src/blocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ the beginning of this chapter for details on why and a possible workaround.
# use std::error::Error;
# use zbus::{blocking::connection, interface, fdo, SignalContext};
#
use event_listener::Event;
use event_listener::{Event, Listener};
struct Greeter {
name: String,
Expand Down Expand Up @@ -185,13 +185,13 @@ fn main() -> Result<(), Box<dyn Error>> {
name: "GreeterName".to_string(),
done: event_listener::Event::new(),
};
let mut done_listener = greeter.done.listen();
let done_listener = greeter.done.listen();
let _handle = connection::Builder::session()?
.name("org.zbus.MyGreeter")?
.serve_at("/org/zbus/MyGreeter", greeter)?
.build()?;
done_listener.as_mut().wait();
done_listener.wait();
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions book/src/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ synchronize with the interface handlers from outside, thanks to the `event_liste
```rust,no_run
# use zbus::{object_server::SignalContext, connection::Builder, interface, fdo, Result};
#
use event_listener::Event;
use event_listener::{Event, Listener};
struct Greeter {
name: String,
Expand Down Expand Up @@ -262,14 +262,14 @@ async fn main() -> Result<()> {
name: "GreeterName".to_string(),
done: event_listener::Event::new(),
};
let mut done_listener = greeter.done.listen();
let done_listener = greeter.done.listen();
let _connection = Builder::session()?
.name("org.zbus.MyGreeter")?
.serve_at("/org/zbus/MyGreeter", greeter)?
.build()
.await?;
done_listener.as_mut().wait();
done_listener.wait();
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion zbus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ hex = "0.4.3"
ordered-stream = "0.2"
rand = "0.8.5"
sha1 = { version = "0.10.5", features = ["std"] }
event-listener = "4.0.1"
event-listener = "5.0.0"
static_assertions = "1.1.0"
async-trait = "0.1.58"
async-fs = { version = "2.0.0", optional = true }
Expand Down
56 changes: 10 additions & 46 deletions zbus/src/blocking/connection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
//! Blocking connection API.
use enumflags2::BitFlags;
use event_listener::EventListener;
use static_assertions::assert_impl_all;
use std::{
io,
ops::Deref,
time::{Duration, Instant},
};
use std::{io, ops::Deref};
use zbus_names::{BusName, ErrorName, InterfaceName, MemberName, OwnedUniqueName, WellKnownName};
use zvariant::ObjectPath;

Expand Down Expand Up @@ -241,13 +238,11 @@ impl Connection {
self.inner
}

/// Returns an [`Activity`] instance to wait for various connection activity.
/// Returns a listener, notified on various connection activity.
///
/// This function is meant for the caller to implement idle or timeout on inactivity.
pub fn monitor_activity(&self) -> Activity {
Activity {
inner: self.inner.monitor_activity(),
}
pub fn monitor_activity(&self) -> EventListener {
self.inner.monitor_activity()
}

/// Returns the peer credentials.
Expand Down Expand Up @@ -276,43 +271,9 @@ impl From<crate::Connection> for Connection {
}
}

/// Allows you to wait for activity on the connection.
///
/// Use [`Connection::monitor_activity`] to get an instance of this type.
#[derive(Debug)]
pub struct Activity {
inner: crate::connection::Activity,
}

assert_impl_all!(Activity: Send, Sync, Unpin);

impl Activity {
/// Wait indefinitely for the activity.
pub fn wait(mut self) {
self.inner.listener.as_mut().wait()
}

/// Wait for the activity for the given amount of time.
///
/// Returns `true` if an activity occurred, `false` if it timedout.
pub fn wait_timeout(mut self, timeout: Duration) -> bool {
self.inner.listener.as_mut().wait_timeout(timeout).is_some()
}

/// Wait for the activity until the given time.
///
/// Returns `true` if an activity occurred, `false` if the deadline was reached.
pub fn wait_deadline(mut self, deadline: Instant) -> bool {
self.inner
.listener
.as_mut()
.wait_deadline(deadline)
.is_some()
}
}

#[cfg(all(test, unix))]
mod tests {
use event_listener::Listener;
use ntest::timeout;
#[cfg(all(unix, not(feature = "tokio")))]
use std::os::unix::net::UnixStream;
Expand Down Expand Up @@ -373,7 +334,10 @@ mod tests {
// eventually, nothing happens and it will timeout
loop {
let listener = c.monitor_activity();
if !listener.wait_timeout(std::time::Duration::from_millis(10)) {
if listener
.wait_timeout(std::time::Duration::from_millis(10))
.is_none()
{
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions zbus/src/blocking/object_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ where
/// ```no_run
/// # use std::error::Error;
/// use zbus::{blocking::Connection, interface};
/// use event_listener::Event;
/// use event_listener::{Event, Listener};
///
/// struct Example {
/// // Interfaces are owned by the ObjectServer. They can have
Expand Down Expand Up @@ -113,13 +113,13 @@ where
/// let connection = Connection::session()?;
///
/// let quit_event = Event::new();
/// let mut quit_listener = quit_event.listen();
/// let quit_listener = quit_event.listen();
/// let interface = Example::new(quit_event);
/// connection
/// .object_server()
/// .at("/org/zbus/path", interface)?;
///
/// quit_listener.as_mut().wait();
/// quit_listener.wait();
/// # Ok::<_, Box<dyn Error + Send + Sync>>(())
/// ```
#[derive(Debug)]
Expand Down
26 changes: 3 additions & 23 deletions zbus/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,13 +1211,11 @@ impl Connection {
Builder::system()?.build().await
}

/// Returns an [`Activity`] instance to wait for various connection activity.
/// Returns a listener, notified on various connection activity.
///
/// This function is meant for the caller to implement idle or timeout on inactivity.
pub fn monitor_activity(&self) -> Activity {
Activity {
listener: self.inner.activity_event.listen(),
}
pub fn monitor_activity(&self) -> EventListener {
self.inner.activity_event.listen()
}

/// Returns the peer credentials.
Expand Down Expand Up @@ -1307,24 +1305,6 @@ enum NameStatus {
Queued(#[allow(unused)] Task<()>),
}

/// A future that resolves when there is activity on the connection.
///
/// Use [`Connection::monitor_activity`] to get an instance of this type.
#[derive(Debug)]
pub struct Activity {
pub(crate) listener: Pin<Box<EventListener>>,
}

assert_impl_all!(Activity: Send, Sync, Unpin);

impl Future for Activity {
type Output = ();

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Future::poll(self.listener.as_mut(), cx)
}
}

#[cfg(test)]
mod tests {
use futures_util::stream::TryStreamExt;
Expand Down
3 changes: 1 addition & 2 deletions zbus/src/object_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{
fmt::Write,
marker::PhantomData,
ops::{Deref, DerefMut},
pin::Pin,
sync::Arc,
};
use tracing::{debug, instrument, trace};
Expand Down Expand Up @@ -839,7 +838,7 @@ pub struct ResponseDispatchNotifier<R> {

impl<R> ResponseDispatchNotifier<R> {
/// Create a new `NotifyResponse`.
pub fn new(response: R) -> (Self, Pin<Box<EventListener>>) {
pub fn new(response: R) -> (Self, EventListener) {
let event = Event::new();
let listener = event.listen();
(
Expand Down
2 changes: 1 addition & 1 deletion zbus/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ where
pub struct PropertyStream<'a, T> {
name: &'a str,
proxy: Proxy<'a>,
changed_listener: Pin<Box<EventListener>>,
changed_listener: EventListener,
phantom: std::marker::PhantomData<T>,
}

Expand Down

0 comments on commit 9ca27bd

Please sign in to comment.