Skip to content

Commit

Permalink
✨ zm: add no_spawn attribute on interface
Browse files Browse the repository at this point in the history
Signed-off-by: Marc-André Lureau <[email protected]>
  • Loading branch information
elmarco committed Mar 15, 2024
1 parent 390ffde commit d70e021
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
28 changes: 18 additions & 10 deletions zbus_macros/src/iface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub mod old {

pub TraitAttributes("trait") {
interface str,
name str
name str,
no_spawn none
};

pub MethodAttributes("method") {
Expand All @@ -39,7 +40,8 @@ def_attrs! {

pub TraitAttributes("trait") {
interface str,
name str
name str,
no_spawn none
};

pub MethodAttributes("method") {
Expand Down Expand Up @@ -300,22 +302,24 @@ pub fn expand<T: AttrParse + Into<TraitAttrs>, M: AttrParse + Into<MethodAttrs>>
_ => return Err(Error::new_spanned(&input.self_ty, "Invalid type")),
};

let iface_name =
{
let (name, interface) = match T::parse_nested_metas(&args)?.into() {
TraitAttrs::New(new) => (new.name, new.interface),
TraitAttrs::Old(old) => (old.name, old.interface),
};
let (iface_name, with_spawn) = {
let (name, interface, no_spawn) = match T::parse_nested_metas(&args)?.into() {
TraitAttrs::New(new) => (new.name, new.interface, new.no_spawn),
TraitAttrs::Old(old) => (old.name, old.interface, old.no_spawn),
};

let name =
match (name, interface) {
(Some(name), None) | (None, Some(name)) => name,
(None, None) => format!("org.freedesktop.{ty}"),
(Some(_), Some(_)) => return Err(syn::Error::new(
input.span(),
"`name` and `interface` attributes should not be specified at the same time",
)),
}
};
};

(name, !no_spawn)
};

// Store parsed information about each method
let mut methods = vec![];
Expand Down Expand Up @@ -713,6 +717,10 @@ pub fn expand<T: AttrParse + Into<TraitAttrs>, M: AttrParse + Into<MethodAttrs>>
#zbus::names::InterfaceName::from_static_str_unchecked(#iface_name)
}

fn with_spawn(&self) -> bool {
#with_spawn
}

async fn get(
&self,
property_name: &str,
Expand Down
9 changes: 9 additions & 0 deletions zbus_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ pub fn dbus_proxy(attr: TokenStream, item: TokenStream) -> TokenStream {
/// properties or signal depending on the item attributes. It will implement the [`Interface`] trait
/// `for T` on your behalf, to handle the message dispatching and introspection support.
///
/// The trait accepts the `interface` attributes:
///
/// * `name` - the D-Bus interface name
///
/// * `no_spawn` - Do not spawn async tasks to call the methods. By default, zbus will spawn a task
/// for each method call. This may result in methods being handled out of order and may not be the
/// expected or desired behaviour. Note that D-Bus has currently not stated explicitely message
/// ordering guarantees (see also https://gitlab.freedesktop.org/dbus/dbus/-/issues/179).
///
/// The methods accepts the `interface` attributes:
///
/// * `name` - override the D-Bus name (pascal case form of the method by default)
Expand Down
2 changes: 1 addition & 1 deletion zbus_macros/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn test_interface() {
#[derive(Serialize, Deserialize, Type, Value)]
struct MyCustomPropertyType(u32);

#[interface(name = "org.freedesktop.zbus.Test")]
#[interface(name = "org.freedesktop.zbus.Test", no_spawn)]
impl<T: 'static> Test<T>
where
T: serde::ser::Serialize + zbus::zvariant::Type + Send + Sync,
Expand Down

0 comments on commit d70e021

Please sign in to comment.