diff --git a/book/src/blocking.md b/book/src/blocking.md index 3812bd48f..328b27991 100644 --- a/book/src/blocking.md +++ b/book/src/blocking.md @@ -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, @@ -185,13 +185,13 @@ fn main() -> Result<(), Box> { 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(()) } diff --git a/book/src/server.md b/book/src/server.md index ea3082382..51dc4e1fb 100644 --- a/book/src/server.md +++ b/book/src/server.md @@ -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, @@ -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(()) } diff --git a/zbus/Cargo.toml b/zbus/Cargo.toml index e1b473eec..7f039864d 100644 --- a/zbus/Cargo.toml +++ b/zbus/Cargo.toml @@ -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 } diff --git a/zbus/src/blocking/connection/mod.rs b/zbus/src/blocking/connection/mod.rs index 334807056..026015f8a 100644 --- a/zbus/src/blocking/connection/mod.rs +++ b/zbus/src/blocking/connection/mod.rs @@ -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; @@ -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. @@ -276,43 +271,9 @@ impl From 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; @@ -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; } } diff --git a/zbus/src/blocking/object_server.rs b/zbus/src/blocking/object_server.rs index 796661ef8..9627ef0b1 100644 --- a/zbus/src/blocking/object_server.rs +++ b/zbus/src/blocking/object_server.rs @@ -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 @@ -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>(()) /// ``` #[derive(Debug)] diff --git a/zbus/src/connection/mod.rs b/zbus/src/connection/mod.rs index f0c6fe49f..edd262808 100644 --- a/zbus/src/connection/mod.rs +++ b/zbus/src/connection/mod.rs @@ -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. @@ -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>, -} - -assert_impl_all!(Activity: Send, Sync, Unpin); - -impl Future for Activity { - type Output = (); - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - Future::poll(self.listener.as_mut(), cx) - } -} - #[cfg(test)] mod tests { use futures_util::stream::TryStreamExt; diff --git a/zbus/src/object_server/mod.rs b/zbus/src/object_server/mod.rs index bb32b211f..bca8dc8bb 100644 --- a/zbus/src/object_server/mod.rs +++ b/zbus/src/object_server/mod.rs @@ -7,7 +7,6 @@ use std::{ fmt::Write, marker::PhantomData, ops::{Deref, DerefMut}, - pin::Pin, sync::Arc, }; use tracing::{debug, instrument, trace}; @@ -839,7 +838,7 @@ pub struct ResponseDispatchNotifier { impl ResponseDispatchNotifier { /// Create a new `NotifyResponse`. - pub fn new(response: R) -> (Self, Pin>) { + pub fn new(response: R) -> (Self, EventListener) { let event = Event::new(); let listener = event.listen(); ( diff --git a/zbus/src/proxy/mod.rs b/zbus/src/proxy/mod.rs index 67277401d..1ae149cd2 100644 --- a/zbus/src/proxy/mod.rs +++ b/zbus/src/proxy/mod.rs @@ -208,7 +208,7 @@ where pub struct PropertyStream<'a, T> { name: &'a str, proxy: Proxy<'a>, - changed_listener: Pin>, + changed_listener: EventListener, phantom: std::marker::PhantomData, }