gtk-rs / gtk-rs-core

Rust bindings for GNOME libraries
https://gtk-rs.org/gtk-rs-core
MIT License
293 stars 115 forks source link

[BUG] not able to use the Property macro with a nested GEnum #1338

Closed gdesmott closed 1 month ago

gdesmott commented 8 months ago

See this code trying to define a GEnum property inside a nested object. It fails, I think, because type = needs to be defined when using member = but I didn't find any way to make it work.

error[E0277]: the trait bound `std::sync::Mutex<foo::Inner>: Property` is not satisfied
   --> glib-macros/tests/properties.rs:159:13
    |
159 |             #[property(get, set, member = senum)]
    |             ^ the trait `Property` is not implemented for `std::sync::Mutex<foo::Inner>`
    |
    = help: the trait `Property` is implemented for `std::sync::Mutex<T>`

error[E0277]: the trait bound `std::sync::Mutex<foo::Inner>: Property` is not satisfied
  --> glib-macros/tests/properties.rs:90:18
   |
90 |         #[derive(Properties, Default)]
   |                  ^^^^^^^^^^ the trait `Property` is not implemented for `std::sync::Mutex<foo::Inner>`
   |
   = help: the trait `Property` is implemented for `std::sync::Mutex<T>`
   = note: this error originates in the derive macro `Properties` (in Nightly builds, run with -Z macro-backtrace for more info)
gdesmott commented 8 months ago

It seems to work when using a custom get implementation:

#[property(get = |t: &Self| t.inner_enum.lock().unwrap().senum.to_owned(), type = SimpleEnum, builder(SimpleEnum::One))]
inner_enum: Mutex<Inner>,
zecakeh commented 7 months ago

I tried with this, and it seems to work:

#[property(get, set, type = SimpleEnum, member = senum, builder(SimpleEnum::default()))]
inner_enum: Mutex<Inner>,
bilelmoussaoui commented 7 months ago

@gdesmott could you add a full reproducer of the issue please?

gdesmott commented 7 months ago

@gdesmott could you add a full reproducer of the issue please?

There is in this commit: https://github.com/gdesmott/gtk-rs-core/commit/f7d6a6049b44c298c56cbc1e4b6fb9ff218f46a9

sdroege commented 1 month ago

Indeed something going wrong with the traits there. Annoying that rust-analyzer doesn't seem to be able to expand the derive macro, that would simplify debugging.

sdroege commented 1 month ago

There's actually no bug here, sorry.

--- a/glib-macros/tests/properties.rs
+++ b/glib-macros/tests/properties.rs
@@ -163,7 +163,7 @@ mod foo {
             construct_only_cell: OnceCell<u32>,
             #[property(get, set = Self::set_construct_only_custom, construct_only)]
             construct_only_custom_setter: OnceCell<Option<String>>,
-            #[property(get, set, member = senum)]
+            #[property(get, set, member = senum, type = SimpleEnum, builder(SimpleEnum::One))]
             inner_enum: Mutex<StructWithEnum>,
         }

When going via member = ..., you always need to specify the property type. And for enum properties you need to provide the builder.