Skip to content

Commit

Permalink
♻️ Removes unneeded borrows in iface macro
Browse files Browse the repository at this point in the history
Removes unneeded borrows in the `new` function of the `MethodInfo` struct,
and changes the signature of `cfg_attrs` to a slice instead of a Vec.
  • Loading branch information
mmasque committed Nov 28, 2023
1 parent 4779718 commit 3064336
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions zbus_macros/src/iface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl MethodInfo {
zbus: &TokenStream,
method: &ImplItemMethod,
attrs: &MethodAttributes,
cfg_attrs: &Vec<&Attribute>,
cfg_attrs: &[&Attribute],
) -> syn::Result<MethodInfo> {
let is_async = method.sig.asyncness.is_some();
let Signature {
Expand Down Expand Up @@ -139,16 +139,16 @@ impl MethodInfo {

let is_mut = if let FnArg::Receiver(r) = inputs
.first()
.ok_or_else(|| Error::new_spanned(&ident, "not &self method"))?
.ok_or_else(|| Error::new_spanned(ident, "not &self method"))?
{
r.mutability.is_some()
} else if is_signal {
false
} else {
return Err(Error::new_spanned(&method, "missing receiver"));
return Err(Error::new_spanned(method, "missing receiver"));
};
if is_signal && !is_async {
return Err(Error::new_spanned(&method, "signals must be async"));
return Err(Error::new_spanned(method, "signals must be async"));
}
let method_await = if is_async {
quote! { .await }
Expand All @@ -164,7 +164,7 @@ impl MethodInfo {
let signal_context_arg: Option<PatType> = if is_signal {
if typed_inputs.is_empty() {
return Err(Error::new_spanned(
&inputs,
inputs,
"Expected a `&zbus::object_server::SignalContext<'_> argument",
));
}
Expand All @@ -173,11 +173,11 @@ impl MethodInfo {
None
};
let mut intro_args = quote!();
intro_args.extend(introspect_input_args(&typed_inputs, is_signal, &cfg_attrs));
intro_args.extend(introspect_input_args(&typed_inputs, is_signal, cfg_attrs));
let is_result_output =
introspect_add_output_args(&mut intro_args, output, out_args, &cfg_attrs)?;
introspect_add_output_args(&mut intro_args, output, out_args, cfg_attrs)?;

let (args_from_msg, args_names) = get_args_from_inputs(&typed_inputs, &zbus)?;
let (args_from_msg, args_names) = get_args_from_inputs(&typed_inputs, zbus)?;

let reply = if is_result_output {
let ret = quote!(r);
Expand Down Expand Up @@ -290,7 +290,7 @@ pub fn expand(args: AttributeArgs, mut input: ItemImpl) -> syn::Result<TokenStre
.filter(|a| a.path.is_ident("cfg"))
.collect();

let method_info = MethodInfo::new(&zbus, &method, &attrs, &cfg_attrs)?;
let method_info = MethodInfo::new(&zbus, method, &attrs, &cfg_attrs)?;
if let Some(prop_attrs) = &attrs.property {
if method_info.method_type == MethodType::Property(PropertyType::NoInputs) {
let emits_changed_signal = if let Some(s) = &prop_attrs.emits_changed_signal {
Expand All @@ -301,13 +301,11 @@ pub fn expand(args: AttributeArgs, mut input: ItemImpl) -> syn::Result<TokenStre
let mut property = Property::new();
property.emits_changed_signal = emits_changed_signal;
properties.insert(method_info.member_name.to_string(), property);
} else {
if let Some(_) = &prop_attrs.emits_changed_signal {
return Err(syn::Error::new(
method.span(),
"`emits_changed_signal` cannot be specified on setters",
));
}
} else if prop_attrs.emits_changed_signal.is_some() {
return Err(syn::Error::new(
method.span(),
"`emits_changed_signal` cannot be specified on setters",
));
}
};
methods.push((method, method_info));
Expand Down Expand Up @@ -397,8 +395,8 @@ pub fn expand(args: AttributeArgs, mut input: ItemImpl) -> syn::Result<TokenStre
// * For argument type with lifetimes, we convert from `Value` (so
// `TryFrom<Value<'_>>` is required).
//
// * For all other arg types, we convert the passed value to `OwnedValue` first and
// then pass it as `Value` (so `TryFrom<OwnedValue>` is required).
// * For all other arg types, we convert the passed value to `OwnedValue` first
// and then pass it as `Value` (so `TryFrom<OwnedValue>` is required).
let value_to_owned = quote! {
match ::zbus::zvariant::Value::try_to_owned(value) {
::std::result::Result::Ok(val) => ::zbus::zvariant::Value::from(val),
Expand Down

0 comments on commit 3064336

Please sign in to comment.