Skip to content

Commit

Permalink
Merge pull request #525 from zeenix/deref-misuse
Browse files Browse the repository at this point in the history
Drop Deref misuses
  • Loading branch information
zeenix authored Dec 19, 2023
2 parents 768d230 + 5716e19 commit 2b1f8b1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 59 deletions.
2 changes: 1 addition & 1 deletion book/src/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ async fn main() -> Result<()> {
let props = zbus::fdo::PropertiesProxy::builder(&conn)
.destination("org.freedesktop.GeoClue2")?
.path(client.path())?
.path(client.inner().path())?
.build()
.await?;
let mut props_changed = props.receive_properties_changed().await?;
Expand Down
10 changes: 0 additions & 10 deletions zbus/src/blocking/object_server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! The object server API.
use std::ops::Deref;

use static_assertions::assert_impl_all;
use zvariant::ObjectPath;

Expand Down Expand Up @@ -235,14 +233,6 @@ impl ObjectServer {
}
}

impl Deref for ObjectServer {
type Target = crate::ObjectServer;

fn deref(&self) -> &Self::Target {
self.inner()
}
}

impl From<crate::ObjectServer> for ObjectServer {
fn from(azync: crate::ObjectServer) -> Self {
Self { azync }
Expand Down
4 changes: 3 additions & 1 deletion zbus/src/connection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ impl<'a> Builder<'a> {
let object_server = conn.sync_object_server(false, None);
for (path, interfaces) in self.interfaces {
for (name, iface) in interfaces {
let future = object_server.at_ready(path.to_owned(), name, || iface);
let future = object_server
.inner()
.at_ready(path.to_owned(), name, || iface);
let added = future.await?;
// Duplicates shouldn't happen.
assert!(added);
Expand Down
24 changes: 13 additions & 11 deletions zbus/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,12 @@ impl PropertiesCache {
let prop_changes = proxy.receive_properties_changed().await?.map(Either::Left);

let get_all = proxy
.inner()
.connection()
.call_method_raw(
Some(proxy.destination()),
proxy.path(),
Some(proxy.interface()),
Some(proxy.inner().destination()),
proxy.inner().path(),
Some(proxy.inner().interface()),
"GetAll",
BitFlags::empty(),
&interface,
Expand Down Expand Up @@ -1358,17 +1359,18 @@ mod tests {

let proxy = fdo::DBusProxy::new(&dest_conn).await?;
let mut name_acquired_stream = proxy
.inner()
.receive_signal_with_args("NameAcquired", &[(0, well_known)])
.await?;

let prop_stream =
proxy
.receive_property_changed("SomeProp")
.await
.filter_map(|changed| async move {
let v: Option<u32> = changed.get().await.ok();
dbg!(v)
});
let prop_stream = proxy
.inner()
.receive_property_changed("SomeProp")
.await
.filter_map(|changed| async move {
let v: Option<u32> = changed.get().await.ok();
dbg!(v)
});
drop(proxy);
drop(prop_stream);

Expand Down
9 changes: 5 additions & 4 deletions zbus/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ async fn my_iface_test(conn: Connection, event: Event) -> zbus::Result<u32> {

assert_eq!(proxy.optional_property().await?, Some(42).into());

let xml = proxy.introspect().await?;
let xml = proxy.inner().introspect().await?;
debug!("Introspection: {}", xml);
let node =
zbus_xml::Node::from_reader(xml.as_bytes()).map_err(|e| Error::Failure(e.to_string()))?;
Expand Down Expand Up @@ -761,7 +761,7 @@ async fn my_iface_test(conn: Connection, event: Event) -> zbus::Result<u32> {
// TODO: Check if the properties are correct.

// issue#207: interface panics on incorrect number of args.
assert!(proxy.call_method("CreateObj", &()).await.is_err());
assert!(proxy.inner().call_method("CreateObj", &()).await.is_err());

let my_obj_proxy = MyIfaceProxy::builder(&conn)
.destination("org.freedesktop.MyService")?
Expand All @@ -776,7 +776,7 @@ async fn my_iface_test(conn: Connection, event: Event) -> zbus::Result<u32> {
assert_eq!(my_obj_proxy.count().await?, 0);
assert_eq!(my_obj_proxy.cached_count()?, Some(0));
assert_eq!(
my_obj_proxy.cached_property_raw("Count").as_deref(),
my_obj_proxy.inner().cached_property_raw("Count").as_deref(),
Some(&Value::from(0u32))
);
my_obj_proxy.ping().await?;
Expand All @@ -801,12 +801,13 @@ async fn my_iface_test(conn: Connection, event: Event) -> zbus::Result<u32> {
assert_eq!(args.object_path(), "/zbus/test/MyObj");
assert_eq!(args.interfaces(), &["org.freedesktop.MyIface"]);

assert!(my_obj_proxy.introspect().await.is_err());
assert!(my_obj_proxy.inner().introspect().await.is_err());
assert!(my_obj_proxy.ping().await.is_err());

// Make sure methods modifying the ObjectServer can be called without
// deadlocks.
proxy
.inner()
.call_method("CreateObjInside", &("CreatedInside"))
.await?;
let created_inside_proxy = MyIfaceProxy::builder(&conn)
Expand Down
41 changes: 9 additions & 32 deletions zbus_macros/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ pub fn create_proxy(
&self.0
}

/// The mutable reference to the underlying `zbus::Proxy`.
pub fn inner_mut(&mut self) -> &mut #proxy_struct<'p> {
&mut self.0
}

#methods
}

Expand All @@ -397,29 +402,15 @@ pub fn create_proxy(
}
}

impl<'p> ::std::ops::Deref for #proxy_name<'p> {
type Target = #proxy_struct<'p>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<'p> ::std::ops::DerefMut for #proxy_name<'p> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<'p> ::std::convert::AsRef<#proxy_struct<'p>> for #proxy_name<'p> {
fn as_ref(&self) -> &#proxy_struct<'p> {
&*self
self.inner()
}
}

impl<'p> ::std::convert::AsMut<#proxy_struct<'p>> for #proxy_name<'p> {
fn as_mut(&mut self) -> &mut #proxy_struct<'p> {
&mut *self
self.inner_mut()
}
}

Expand Down Expand Up @@ -876,7 +867,7 @@ fn gen_proxy_signal(
#(#other_attrs)*
pub #usage fn #receiver_with_args_name(&self, args: &[(u8, &str)]) -> #zbus::Result<#stream_name<'static>>
{
self.receive_signal_with_args(#signal_name, args)#wait.map(#stream_name)
self.0.receive_signal_with_args(#signal_name, args)#wait.map(#stream_name)
}
}
};
Expand All @@ -885,7 +876,7 @@ fn gen_proxy_signal(
#(#other_attrs)*
pub #usage fn #receiver_name(&self) -> #zbus::Result<#stream_name<'static>>
{
self.receive_signal(#signal_name)#wait.map(#stream_name)
self.0.receive_signal(#signal_name)#wait.map(#stream_name)
}

#receive_signal_with_args
Expand Down Expand Up @@ -1088,20 +1079,6 @@ fn gen_proxy_signal(
}
}

impl<'a> std::ops::Deref for #stream_name<'a> {
type Target = #zbus::#signal_type<'a>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl ::std::ops::DerefMut for #stream_name<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

#stream_impl

#args_struct_decl
Expand Down

0 comments on commit 2b1f8b1

Please sign in to comment.