Skip to content

Commit abea3fa

Browse files
committed
Add associated BuilderFn to auto set the param spec's type param
1 parent 3b783df commit abea3fa

File tree

8 files changed

+126
-12
lines changed

8 files changed

+126
-12
lines changed

glib-macros/src/boxed_derive.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
219219
impl #crate_ident::HasParamSpec for #name {
220220
type ParamSpec = #crate_ident::ParamSpecBoxed;
221221
type SetValue = Self;
222+
type BuilderFn = fn(&str) -> #crate_ident::ParamSpecBoxedBuilder;
223+
224+
fn param_spec_builder() -> Self::BuilderFn {
225+
|name| Self::ParamSpec::builder(name, <Self as #crate_ident::types::StaticType>::static_type())
226+
}
222227
}
223228
}
224229
}

glib-macros/src/enum_derive.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ pub fn impl_enum(input: &syn::DeriveInput) -> TokenStream {
178178
impl #crate_ident::HasParamSpec for #name {
179179
type ParamSpec = #crate_ident::ParamSpecEnum;
180180
type SetValue = Self;
181+
type BuilderFn = fn(&str) -> #crate_ident::ParamSpecEnumBuilder;
182+
183+
fn param_spec_builder() -> Self::BuilderFn {
184+
|name| Self::ParamSpec::builder(name, <Self as #crate_ident::types::StaticType>::static_type())
185+
}
181186
}
182187
}
183188
}

glib-macros/src/properties.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ fn expand_properties_fn(props: &[PropDesc]) -> TokenStream2 {
335335
let build_blurb = blurb.as_ref().map(|x| quote!(.blurb(#x)));
336336
let span = prop.attrs_span;
337337
quote_spanned! {span=>
338-
<<<#ty as #crate_ident::Property>::Value as #crate_ident::HasParamSpec>::ParamSpec>
339-
::builder #builder_call
338+
<<#ty as #crate_ident::Property>::Value as #crate_ident::HasParamSpec>
339+
::param_spec_builder() #builder_call
340340
#build_nick
341341
#build_blurb
342342
.flags(#flags)

glib-macros/tests/properties.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ fn props() {
7676
numeric_builder: RefCell<u32>,
7777
#[property(get, set, builder('c'))]
7878
builder_with_required_param: RefCell<char>,
79-
#[property(get, set, builder(SimpleBoxedString::static_type()))]
79+
#[property(get, set)]
8080
boxed: RefCell<SimpleBoxedString>,
81-
#[property(get, set, builder(SimpleEnum::static_type()))]
81+
#[property(get, set)]
8282
fenum: RefCell<SimpleEnum>,
83-
#[property(get, set, builder(glib::Object::static_type()))]
83+
#[property(get, set)]
8484
object: RefCell<Option<glib::Object>>,
8585
#[property(get, set)]
8686
optional: RefCell<Option<String>>,

glib/src/boxed.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,19 @@ macro_rules! glib_boxed_wrapper {
262262
}
263263
}
264264

265+
};
266+
267+
(@value_impl $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty) => {
265268
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::HasParamSpec for $name $(<$($generic),+>)? {
266269
type ParamSpec = $crate::ParamSpecBoxed;
267270
type SetValue = Self;
268-
}
269-
};
271+
type BuilderFn = fn(&str, $crate::types::Type) -> $crate::ParamSpecBoxedBuilder;
270272

271-
(@value_impl $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty) => { };
273+
fn param_spec_builder() -> Self::BuilderFn {
274+
|name, type_| Self::ParamSpec::builder(name, type_)
275+
}
276+
}
277+
};
272278

273279
(@value_impl $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty, @type_ $get_type_expr:expr) => {
274280
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::types::StaticType for $name $(<$($generic),+>)? {
@@ -342,6 +348,16 @@ macro_rules! glib_boxed_wrapper {
342348
value
343349
}
344350
}
351+
352+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::HasParamSpec for $name $(<$($generic),+>)? {
353+
type ParamSpec = $crate::ParamSpecBoxed;
354+
type SetValue = Self;
355+
type BuilderFn = fn(&str) -> $crate::ParamSpecBoxedBuilder;
356+
357+
fn param_spec_builder() -> Self::BuilderFn {
358+
|name| Self::ParamSpec::builder(name, <Self as $crate::types::StaticType>::static_type())
359+
}
360+
}
345361
};
346362

347363
(@memory_manager_impl $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty, @copy $copy_arg:ident $copy_expr:expr, @free $free_arg:ident $free_expr:expr) => {

glib/src/boxed_inline.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,6 @@ macro_rules! glib_boxed_inline_wrapper {
500500
}
501501
}
502502

503-
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::HasParamSpec for $name $(<$($generic),+>)? {
504-
type ParamSpec = $crate::ParamSpecBoxed;
505-
type SetValue = Self;
506-
}
507503
};
508504

509505
(@value_impl $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty) => { };
@@ -578,5 +574,15 @@ macro_rules! glib_boxed_inline_wrapper {
578574
value
579575
}
580576
}
577+
578+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::HasParamSpec for $name $(<$($generic),+>)? {
579+
type ParamSpec = $crate::ParamSpecBoxed;
580+
type SetValue = Self;
581+
type BuilderFn = fn(&str) -> $crate::ParamSpecBoxedBuilder;
582+
583+
fn param_spec_builder() -> Self::BuilderFn {
584+
|name| Self::ParamSpec::builder(name, <&Self as $crate::types::StaticType>::static_type())
585+
}
586+
}
581587
};
582588
}

glib/src/object.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,11 @@ macro_rules! glib_object_wrapper {
11511151
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::HasParamSpec for $name $(<$($generic),+>)? {
11521152
type ParamSpec = $crate::ParamSpecObject;
11531153
type SetValue = Self;
1154+
type BuilderFn = fn(&str) -> $crate::ParamSpecObjectBuilder;
1155+
1156+
fn param_spec_builder() -> Self::BuilderFn {
1157+
|name| Self::ParamSpec::builder(name, <Self as $crate::types::StaticType>::static_type())
1158+
}
11541159
}
11551160
};
11561161

glib/src/param_spec.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,67 +1223,144 @@ pub trait HasParamSpec {
12231223
// rustdoc-stripper-ignore-next
12241224
/// Preferred value to be used as setter for the associated ParamSpec.
12251225
type SetValue: ?Sized;
1226+
type BuilderFn;
1227+
fn param_spec_builder() -> Self::BuilderFn;
12261228
}
12271229

12281230
impl<T: crate::value::ToValueOptional + HasParamSpec> HasParamSpec for Option<T> {
12291231
type ParamSpec = T::ParamSpec;
12301232
type SetValue = T::SetValue;
1233+
type BuilderFn = T::BuilderFn;
1234+
1235+
fn param_spec_builder() -> Self::BuilderFn {
1236+
T::param_spec_builder()
1237+
}
12311238
}
12321239
impl<T: HasParamSpec + ?Sized> HasParamSpec for &T {
12331240
type ParamSpec = T::ParamSpec;
12341241
type SetValue = T::SetValue;
1242+
type BuilderFn = T::BuilderFn;
1243+
1244+
fn param_spec_builder() -> Self::BuilderFn {
1245+
T::param_spec_builder()
1246+
}
12351247
}
12361248
impl HasParamSpec for crate::GString {
12371249
type ParamSpec = ParamSpecString;
12381250
type SetValue = str;
1251+
type BuilderFn = fn(&str) -> ParamSpecStringBuilder;
1252+
1253+
fn param_spec_builder() -> Self::BuilderFn {
1254+
|name| Self::ParamSpec::builder(name)
1255+
}
12391256
}
12401257
impl HasParamSpec for str {
12411258
type ParamSpec = ParamSpecString;
12421259
type SetValue = str;
1260+
type BuilderFn = fn(&str) -> ParamSpecStringBuilder;
1261+
1262+
fn param_spec_builder() -> Self::BuilderFn {
1263+
|name| Self::ParamSpec::builder(name)
1264+
}
12431265
}
12441266
impl HasParamSpec for String {
12451267
type ParamSpec = ParamSpecString;
12461268
type SetValue = str;
1269+
type BuilderFn = fn(&str) -> ParamSpecStringBuilder;
1270+
1271+
fn param_spec_builder() -> Self::BuilderFn {
1272+
|name| Self::ParamSpec::builder(name)
1273+
}
12471274
}
12481275
impl HasParamSpec for char {
12491276
type ParamSpec = ParamSpecUnichar;
12501277
type SetValue = Self;
1278+
type BuilderFn = fn(&str, char) -> ParamSpecUnicharBuilder;
1279+
1280+
fn param_spec_builder() -> Self::BuilderFn {
1281+
|name, char| Self::ParamSpec::builder(name, char)
1282+
}
12511283
}
12521284
impl HasParamSpec for f64 {
12531285
type ParamSpec = ParamSpecDouble;
12541286
type SetValue = Self;
1287+
type BuilderFn = fn(&str) -> ParamSpecDoubleBuilder;
1288+
1289+
fn param_spec_builder() -> Self::BuilderFn {
1290+
|name| Self::ParamSpec::builder(name)
1291+
}
12551292
}
12561293
impl HasParamSpec for f32 {
12571294
type ParamSpec = ParamSpecFloat;
12581295
type SetValue = Self;
1296+
type BuilderFn = fn(&str) -> ParamSpecFloatBuilder;
1297+
1298+
fn param_spec_builder() -> Self::BuilderFn {
1299+
|name| Self::ParamSpec::builder(name)
1300+
}
12591301
}
12601302
impl HasParamSpec for i64 {
12611303
type ParamSpec = ParamSpecInt64;
12621304
type SetValue = Self;
1305+
type BuilderFn = fn(&str) -> ParamSpecInt64Builder;
1306+
1307+
fn param_spec_builder() -> Self::BuilderFn {
1308+
|name| Self::ParamSpec::builder(name)
1309+
}
12631310
}
12641311
impl HasParamSpec for i32 {
12651312
type ParamSpec = ParamSpecInt;
12661313
type SetValue = Self;
1314+
type BuilderFn = fn(&str) -> ParamSpecIntBuilder;
1315+
1316+
fn param_spec_builder() -> Self::BuilderFn {
1317+
|name| Self::ParamSpec::builder(name)
1318+
}
12671319
}
12681320
impl HasParamSpec for i8 {
12691321
type ParamSpec = ParamSpecChar;
12701322
type SetValue = Self;
1323+
type BuilderFn = fn(&str) -> ParamSpecCharBuilder;
1324+
1325+
fn param_spec_builder() -> Self::BuilderFn {
1326+
|name| Self::ParamSpec::builder(name)
1327+
}
12711328
}
12721329
impl HasParamSpec for u64 {
12731330
type ParamSpec = ParamSpecUInt64;
12741331
type SetValue = Self;
1332+
type BuilderFn = fn(&str) -> ParamSpecUInt64Builder;
1333+
1334+
fn param_spec_builder() -> Self::BuilderFn {
1335+
|name| Self::ParamSpec::builder(name)
1336+
}
12751337
}
12761338
impl HasParamSpec for u32 {
12771339
type ParamSpec = ParamSpecUInt;
12781340
type SetValue = Self;
1341+
type BuilderFn = fn(&str) -> ParamSpecUIntBuilder;
1342+
1343+
fn param_spec_builder() -> Self::BuilderFn {
1344+
|name| Self::ParamSpec::builder(name)
1345+
}
12791346
}
12801347
impl HasParamSpec for u8 {
12811348
type ParamSpec = ParamSpecUChar;
12821349
type SetValue = Self;
1350+
type BuilderFn = fn(&str) -> ParamSpecUCharBuilder;
1351+
1352+
fn param_spec_builder() -> Self::BuilderFn {
1353+
|name| Self::ParamSpec::builder(name)
1354+
}
12831355
}
12841356
impl HasParamSpec for bool {
12851357
type ParamSpec = ParamSpecBoolean;
12861358
type SetValue = Self;
1359+
type BuilderFn = fn(&str) -> ParamSpecBooleanBuilder;
1360+
1361+
fn param_spec_builder() -> Self::BuilderFn {
1362+
|name| Self::ParamSpec::builder(name)
1363+
}
12871364
}
12881365

12891366
#[cfg(test)]

0 commit comments

Comments
 (0)