Skip to content

Commit 4a8a870

Browse files
committed
associate preferred SetValue to HasParamSpec, restrict accepted set param
1 parent 95dcb89 commit 4a8a870

File tree

10 files changed

+41
-31
lines changed

10 files changed

+41
-31
lines changed

glib-macros/src/boxed_derive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
218218

219219
impl #crate_ident::HasParamSpec for #name {
220220
type ParamSpec = #crate_ident::ParamSpecBoxed;
221+
type SetValue = Self;
221222
}
222223
}
223224
}

glib-macros/src/enum_derive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ pub fn impl_enum(input: &syn::DeriveInput) -> TokenStream {
177177

178178
impl #crate_ident::HasParamSpec for #name {
179179
type ParamSpec = #crate_ident::ParamSpecEnum;
180+
type SetValue = Self;
180181
}
181182
}
182183
}

glib-macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
901901
/// fn main() {
902902
/// let myfoo: Foo = glib::object::Object::new(&[]).unwrap();
903903
///
904-
/// myfoo.set_fizz("test value".to_string());
904+
/// myfoo.set_fizz("test value");
905905
/// assert_eq!(myfoo.fizz(), "custom set: test value".to_string());
906906
/// }
907907
/// ```

glib-macros/src/properties.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ fn expand_properties_fn(props: &[PropDesc]) -> TokenStream2 {
311311
let build_blurb = blurb.as_ref().map(|x| quote!(.blurb(#x)));
312312
let span = prop.attrs_span;
313313
quote_spanned! {span=>
314-
<<#ty as #crate_ident::Property>::ParamSpec>
314+
<<<#ty as #crate_ident::Property>::Value as #crate_ident::HasParamSpec>::ParamSpec>
315315
::builder #builder_call
316316
#build_nick
317317
#build_blurb
@@ -461,7 +461,7 @@ fn getter_prototype(ident: &syn::Ident, ty: &syn::Type) -> TokenStream2 {
461461
fn setter_prototype(ident: &syn::Ident, ty: &syn::Type) -> TokenStream2 {
462462
let crate_ident = crate_ident_new();
463463
let ident = format_ident!("set_{}", ident);
464-
quote!(fn #ident<P: #crate_ident::HasParamSpec<ParamSpec = <#ty as #crate_ident::Property>::ParamSpec> + #crate_ident::ToValue>(&self, value: P))
464+
quote!(fn #ident(&self, value: &<<#ty as #crate_ident::Property>::Value as #crate_ident::HasParamSpec>::SetValue))
465465
}
466466
fn expand_getset_properties_def(props: &[PropDesc]) -> TokenStream2 {
467467
let defs = props
@@ -489,7 +489,7 @@ fn expand_getset_properties_impl(props: &[PropDesc]) -> TokenStream2 {
489489
quote!(#fn_prototype { #body })
490490
});
491491
let setter = (p.set.is_some() && !p.flags.contains(&"construct_only")).then(|| {
492-
let body = quote!(self.set_property(#name, value));
492+
let body = quote!(self.set_property_from_value(#name, &value.to_value()));
493493
let fn_prototype = setter_prototype(&ident, ty);
494494
quote!(#fn_prototype { #body })
495495
});

glib/src/boxed.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ macro_rules! glib_boxed_wrapper {
264264

265265
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::HasParamSpec for $name $(<$($generic),+>)? {
266266
type ParamSpec = $crate::ParamSpecBoxed;
267+
type SetValue = Self;
267268
}
268269
};
269270

glib/src/boxed_inline.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ macro_rules! glib_boxed_inline_wrapper {
502502

503503
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::HasParamSpec for $name $(<$($generic),+>)? {
504504
type ParamSpec = $crate::ParamSpecBoxed;
505+
type SetValue = Self;
505506
}
506507
};
507508

glib/src/construct_cell.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use crate::{Property, PropertyGet, PropertySet, PropertySetNested};
3+
use crate::{HasParamSpec, Property, PropertyGet, PropertySet, PropertySetNested};
44

55
use once_cell::sync::OnceCell as SyncOnceCell;
66
use once_cell::unsync::OnceCell;
@@ -17,9 +17,8 @@ macro_rules! define_construct {
1717
Self::new_empty()
1818
}
1919
}
20-
impl<T: Property> Property for $ident<T> {
20+
impl<T: Property + HasParamSpec> Property for $ident<T> {
2121
type Value = T;
22-
type ParamSpec = T::ParamSpec;
2322
}
2423

2524
impl<T> ::std::ops::Deref for $ident<T> {

glib/src/object.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,7 @@ macro_rules! glib_object_wrapper {
11491149

11501150
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::HasParamSpec for $name $(<$($generic),+>)? {
11511151
type ParamSpec = $crate::ParamSpecObject;
1152+
type SetValue = Self;
11521153
}
11531154
};
11541155

glib/src/param_spec.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,49 +1219,71 @@ define_builder!(
12191219

12201220
pub trait HasParamSpec {
12211221
type ParamSpec;
1222+
1223+
// rustdoc-stripper-ignore-next
1224+
/// Preferred value to be used as setter for the associated ParamSpec.
1225+
type SetValue: ?Sized;
12221226
}
12231227

1228+
impl<T: crate::value::ToValueOptional + HasParamSpec> HasParamSpec for Option<T> {
1229+
type ParamSpec = T::ParamSpec;
1230+
type SetValue = T::SetValue;
1231+
}
12241232
impl<T: HasParamSpec + ?Sized> HasParamSpec for &T {
12251233
type ParamSpec = T::ParamSpec;
1234+
type SetValue = T::SetValue;
12261235
}
12271236
impl HasParamSpec for crate::GString {
12281237
type ParamSpec = ParamSpecString;
1238+
type SetValue = str;
12291239
}
12301240
impl HasParamSpec for str {
12311241
type ParamSpec = ParamSpecString;
1242+
type SetValue = str;
12321243
}
12331244
impl HasParamSpec for String {
12341245
type ParamSpec = ParamSpecString;
1246+
type SetValue = str;
12351247
}
12361248
impl HasParamSpec for char {
12371249
type ParamSpec = ParamSpecUnichar;
1250+
type SetValue = Self;
12381251
}
12391252
impl HasParamSpec for f64 {
12401253
type ParamSpec = ParamSpecDouble;
1254+
type SetValue = Self;
12411255
}
12421256
impl HasParamSpec for f32 {
12431257
type ParamSpec = ParamSpecFloat;
1258+
type SetValue = Self;
12441259
}
12451260
impl HasParamSpec for i64 {
12461261
type ParamSpec = ParamSpecInt64;
1262+
type SetValue = Self;
12471263
}
12481264
impl HasParamSpec for i32 {
12491265
type ParamSpec = ParamSpecInt;
1266+
type SetValue = Self;
12501267
}
12511268
impl HasParamSpec for i8 {
12521269
type ParamSpec = ParamSpecChar;
1270+
type SetValue = Self;
12531271
}
12541272
impl HasParamSpec for u64 {
12551273
type ParamSpec = ParamSpecUInt64;
1274+
type SetValue = Self;
12561275
}
12571276
impl HasParamSpec for u32 {
12581277
type ParamSpec = ParamSpecUInt;
1278+
type SetValue = Self;
12591279
}
12601280
impl HasParamSpec for u8 {
12611281
type ParamSpec = ParamSpecUChar;
1282+
type SetValue = Self;
12621283
}
12631284
impl HasParamSpec for bool {
12641285
type ParamSpec = ParamSpecBoolean;
1286+
type SetValue = Self;
12651287
}
12661288

12671289
#[cfg(test)]

glib/src/property.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,38 @@ use crate::HasParamSpec;
1717
/// The definition is recursive, so you can nest many `Property`s together. The final `ParamSpec` will
1818
/// be the one of the innermost type
1919
pub trait Property {
20-
type Value;
21-
type ParamSpec;
20+
type Value: HasParamSpec;
2221
}
2322
impl<T: HasParamSpec> Property for T {
2423
type Value = T;
25-
type ParamSpec = T::ParamSpec;
26-
}
27-
impl<T: Property> Property for Option<T> {
28-
type Value = Option<T>;
29-
type ParamSpec = T::ParamSpec;
3024
}
3125
impl<T: Property> Property for PhantomData<T> {
32-
type Value = T;
33-
type ParamSpec = T::ParamSpec;
26+
type Value = T::Value;
3427
}
3528
impl<T: Property> Property for RefCell<T> {
36-
type Value = T;
37-
type ParamSpec = T::ParamSpec;
29+
type Value = T::Value;
3830
}
3931
impl<T: Property> Property for Cell<T> {
40-
type Value = T;
41-
type ParamSpec = T::ParamSpec;
32+
type Value = T::Value;
4233
}
4334
impl<T: Property> Property for Mutex<T> {
44-
type Value = T;
45-
type ParamSpec = T::ParamSpec;
35+
type Value = T::Value;
4636
}
4737
impl<T: Property> Property for RwLock<T> {
48-
type Value = T;
49-
type ParamSpec = T::ParamSpec;
38+
type Value = T::Value;
5039
}
5140
impl<T: Property> Property for once_cell::sync::OnceCell<T> {
52-
type Value = T;
53-
type ParamSpec = T::ParamSpec;
41+
type Value = T::Value;
5442
}
5543
impl<T: Property> Property for once_cell::unsync::OnceCell<T> {
56-
type Value = T;
57-
type ParamSpec = T::ParamSpec;
44+
type Value = T::Value;
5845
}
5946
// Handle smart pointers trasparently
6047
impl<T: Property> Property for Rc<T> {
6148
type Value = T::Value;
62-
type ParamSpec = T::ParamSpec;
6349
}
6450
impl<T: Property> Property for Arc<T> {
6551
type Value = T::Value;
66-
type ParamSpec = T::ParamSpec;
6752
}
6853

6954
// rustdoc-stripper-ignore-next
@@ -208,7 +193,6 @@ macro_rules! impl_atomic {
208193
($atomic:ty, $valuety:ty) => {
209194
impl Property for $atomic {
210195
type Value = $valuety;
211-
type ParamSpec = <$valuety as HasParamSpec>::ParamSpec;
212196
}
213197
impl PropertyGet for $atomic {
214198
type Value = $valuety;

0 commit comments

Comments
 (0)