8000 Relax the limitations in `#[attr(key = value)]` when the values only accept literals · Issue #1331 · dbus2/zbus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Relax the limitations in #[attr(key = value)] when the values only accept literals #1331

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

Open
yuan-y-chang opened this issue Apr 12, 2025 · 3 comments
Labels
enhancement New feature or request zbus_macros

Comments

@yuan-y-chang
Copy link

Sometimes we want to define and use some constant variables in many places.

const INTERFACE: &str = "org.zbus.MyGreeter1";
const SERVICE: &str = "org.zbus.MyGreeter";
const OBJ_PATH: &str = "/org/zbus/MyGreeter";

const VERSION_NAME: &str = "eVer";

#[zbus::proxy(
    interface = INTERFACE,
    default_service = SERVICE,
    default_path = OBJ_PATH
)]
trait Greeter {
    #[zbus(property, name = VERSION_NAME)]
    fn version(&self) -> zbus::Result<u32>;
}

But shows

error: attribute `interface`'s value must be a literal
 --> src\main.rs:2:17
  |
2 |     interface = INTERFACE,
  |                 ^^^^^^^^^

error: attribute `name`'s value must be a literal
  --> src\main.rs:12:29
   |
12 |     #[zbus(property, name = VERSION_NAME)]
   |                             ^^^^^^^^^^^^

Can we relax them to expr for string consistency?

@zeenix
Copy link
Contributor
zeenix commented Apr 12, 2025

@yuan-y-chang sure thing. PR welcome!

@yuan-y-chang
Copy link
Author

I guess the type-check mechanisms in the def_attrs macro should be removed?

Change

(@attr_ty str) => {::std::option::Option<::std::string::String>};
(@attr_ty bool) => {::std::option::Option<bool>};
(@attr_ty [str]) => {::std::option::Option<::std::vec::Vec<::std::string::String>>};

to

(@attr_ty str) => {::std::option::Option<::syn::Expr>}; 
(@attr_ty bool) => {::std::option::Option<::syn::Expr>}; 
(@attr_ty [str]) => {::std::option::Option<::std::vec::Vec<::syn::Expr>>}; 

When parsing the attribute arguments,

(@match_attr_with $attr_name:ident, $meta:ident, $self:ident, $matched:expr) => {
if let ::std::option::Option::Some(value) = $matched? {
if $self.$attr_name.is_some() {
return ::std::result::Result::Err(::syn::Error::new(
$meta.span(),
::std::concat!("duplicate `", ::std::stringify!($attr_name), 8FF8 "` attribute")
));
}
$self.$attr_name = ::std::option::Option::Some(value.value());
return Ok(());
}
};

The fragment $matched can store the Exprs directly and paste later. We can check the type with the Rust compiler.

@zeenix
Copy link
Contributor
zeenix commented Apr 13, 2025

@yuan-y-chang I'm really sorry but I won't have to time to mentor you on this.

@zeenix zeenix added enhancement New feature or request zbus_macros labels May 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request zbus_macros
Projects
None yet
Development

No branches or pull requests

2 participants
0