Skip to content

Commit 5119b61

Browse files
committed
properties: accept default = value attribute
1 parent 1bb181f commit 5119b61

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

glib-macros/src/properties.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ impl Parse for PropAttr {
114114
"set" => PropAttr::Set(Some(input.parse()?)),
115115
"type" => PropAttr::Type(input.parse()?),
116116
"member" => PropAttr::Member(input.parse()?),
117+
// Special case "default = ..." and map it to .default_value(...)
118+
"default" => PropAttr::BuilderField((
119+
syn::Ident::new("default_value", name.span()),
120+
Some(input.parse()?),
121+
)),
117122
_ => PropAttr::BuilderField((name, Some(input.parse()?))),
118123
}
119124
} else if input.peek(syn::token::Paren) {

glib-macros/tests/properties.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ mod foo {
6464
fake_field: PhantomData<String>,
6565
#[property(get, set, explicit_notify, lax_validation)]
6666
custom_flags: RefCell<String>,
67+
#[property(get, set, default = "hello")]
68+
with_default: RefCell<String>,
6769
#[property(get, set, builder())]
6870
simple_builder: RefCell<u32>,
6971
#[property(get, set, builder().minimum(0).maximum(5))]
@@ -161,6 +163,17 @@ fn props() {
161163
ParamFlags::EXPLICIT_NOTIFY | ParamFlags::READWRITE | ParamFlags::LAX_VALIDATION
162164
);
163165

166+
// default value
167+
assert_eq!(
168+
myfoo
169+
.find_property("with_default")
170+
.unwrap()
171+
.default_value()
172+
.get::<String>()
173+
.unwrap(),
174+
"hello".to_string()
175+
);
176+
164177
// numeric builder
165178
assert_eq!(
166179
myfoo

0 commit comments

Comments
 (0)