Skip to content

Commit 8310180

Browse files
committed
take any value with impl AsRef<T> in setter
1 parent ceb9ac2 commit 8310180

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

glib-macros/src/properties.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,10 @@ fn expand_getset_properties_impl(props: &[PropDesc]) -> TokenStream2 {
498498
quote!(#fn_prototype { #body })
499499
});
500500
let setter = (p.set.is_some() && !p.flags.contains(&"construct_only")).then(|| {
501-
let body = quote!(self.set_property_from_value(#name, &value.to_value()));
501+
let body = quote!(self.set_property_from_value(#name, &value.as_ref().to_value()));
502502
let fn_prototype = {
503503
let ident = format_ident!("set_{}", ident);
504-
quote!(pub fn #ident(&self, value: &<<#ty as #crate_ident::Property>::Value as #crate_ident::HasParamSpec>::SetValue))
504+
quote!(pub fn #ident<'a>(&self, value: impl AsRef<<<#ty as #crate_ident::Property>::Value as #crate_ident::HasParamSpec>::SetValue>))
505505
};
506506
quote!(#fn_prototype { #body })
507507
});

glib-macros/tests/properties.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ fn props() {
7878
boxed: RefCell<SimpleBoxedString>,
7979
#[property(get, set, builder(SimpleEnum::static_type()))]
8080
fenum: RefCell<SimpleEnum>,
81+
#[property(get, set, builder(glib::Object::static_type()))]
82+
object: RefCell<Option<glib::Object>>,
8183
#[property(get, set)]
8284
optional: RefCell<Option<String>>,
8385
#[property(get, set)]
@@ -231,6 +233,17 @@ fn props() {
231233
"setter working".to_string()
232234
);
233235

236+
// simple with various String types
237+
myfoo.set_bar(String::from("setter working"));
238+
myfoo.set_bar(glib::GString::from("setter working"));
239+
assert_eq!(
240+
myfoo.property::<String>("bar"),
241+
"setter working".to_string()
242+
);
243+
244+
// object subclass
245+
myfoo.set_object(glib::BoxedAnyObject::new(""));
246+
234247
// custom
235248
myfoo.set_fake_field("fake setter");
236249
assert_eq!(

0 commit comments

Comments
 (0)