Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require an owned value for property setters. #1189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions zbus/src/fdo/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Properties {
&self,
interface_name: InterfaceName<'_>,
property_name: &str,
value: Value<'_>,
value: &Value<'_>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this will be a breaking change since this is a public API. :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, ok, lmk if / how you want to deal with it and I can update the patch. Alternatively feel free to just close this (or tag it as deferred or whatever you want :))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just tag it I guess. I hope you're around to rebase when 6.0 is being released. :)

#[zbus(object_server)] server: &ObjectServer,
#[zbus(connection)] connection: &Connection,
#[zbus(header)] header: Header<'_>,
Expand All @@ -79,7 +79,7 @@ impl Properties {

match iface.instance.read().await.set(
property_name,
&value,
value,
server,
connection,
Some(&header),
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 @@ -810,7 +810,7 @@ impl<'a> Proxy<'a> {
T: 't + Into<Value<'t>>,
{
self.properties_proxy()
.set(self.inner.interface.as_ref(), property_name, value.into())
.set(self.inner.interface.as_ref(), property_name, &value.into())
.await
}

Expand Down
53 changes: 43 additions & 10 deletions zbus_macros/src/iface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use syn::{
ImplItem, ImplItemFn, ItemImpl,
Lit::Str,
Meta, MetaNameValue, PatType, PathArguments, ReturnType, Signature, Token, Type, TypePath,
Visibility,
TypeReference, Visibility,
};
use zvariant_utils::{case, def_attrs};

Expand Down Expand Up @@ -983,6 +983,18 @@ pub fn expand(args: Punctuated<Meta, Token![,]>, mut input: ItemImpl) -> syn::Re
})
}

fn maybe_unref_ty(ty: &Type) -> Option<&Type> {
fn should_unref(v: &TypeReference) -> bool {
// &str is special and we can deserialize to it, unlike other reference types.
!matches!(*v.elem, Type::Path(ref p) if p.path.is_ident("str"))
}

match *ty {
Type::Reference(ref v) if should_unref(v) => Some(&v.elem),
_ => None,
}
}

fn get_args_from_inputs(
inputs: &[PatType],
method_type: MethodType,
Expand Down Expand Up @@ -1071,7 +1083,7 @@ fn get_args_from_inputs(
};
} else {
args_names.push(pat_ident(input).unwrap());
tys.push(&input.ty);
tys.push(&*input.ty);
}
}

Expand All @@ -1085,15 +1097,36 @@ fn get_args_from_inputs(
_ => (
quote! { let hdr = message.header(); },
quote! { let msg_body = message.body(); },
quote! {
let (#(#args_names),*): (#(#tys),*) =
match msg_body.deserialize() {
::std::result::Result::Ok(r) => r,
::std::result::Result::Err(e) => {
let err = <#zbus::fdo::Error as ::std::convert::From<_>>::from(e);
return connection.reply_dbus_error(&hdr, err).await;
{
let mut unrefed_indices = vec![];
let owned_tys = tys
.iter()
.enumerate()
.map(|(i, ty)| {
let owned = maybe_unref_ty(ty);
if owned.is_some() {
unrefed_indices.push(i);
}
};
owned.unwrap_or(ty)
})
.collect::<Vec<_>>();
let mut q = quote! {
let (#(#args_names),*): (#(#owned_tys),*) =
match msg_body.deserialize() {
::std::result::Result::Ok(r) => r,
::std::result::Result::Err(e) => {
let err = <#zbus::fdo::Error as ::std::convert::From<_>>::from(e);
return connection.reply_dbus_error(&hdr, err).await;
}
};
};
for index in unrefed_indices {
let arg_name = &args_names[index];
q.extend(quote! {
let #arg_name = &#arg_name;
});
}
q
},
),
};
Expand Down
Loading