Skip to content

Commit e7f5c5d

Browse files
authored
Merge pull request #1732 from nozwock/document-enum-macro
Document requirement for explicit default in `Enum` properties
2 parents 275acae + 5c57e69 commit e7f5c5d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

glib-macros/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,10 @@ pub fn closure_local(item: TokenStream) -> TokenStream {
525525
/// }
526526
/// ```
527527
///
528+
/// When using the [`Properties`] macro with enums that derive [`Enum`], the default value must be
529+
/// explicitly set via the `builder` parameter of the `#[property]` attribute. See
530+
/// [here](Properties#supported-types) for details.
531+
///
528532
/// An enum can be registered as a dynamic type by setting the derive macro
529533
/// helper attribute `enum_dynamic`:
530534
///
@@ -1366,6 +1370,9 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
13661370
/// Optional types also require the `nullable` attribute: without it, the generated setter on the wrapper type
13671371
/// will take `T` instead of `Option<T>`, preventing the user from ever calling the setter with a `None` value.
13681372
///
1373+
/// Notice: For enums that derive [`Enum`] or are C-style enums, you must explicitly specify the
1374+
/// default value of the enum using the `builder` parameter in the `#[property]` attribute.
1375+
///
13691376
/// ## Adding support for custom types
13701377
/// ### Types wrapping an existing <code>T: [ToValue] + [HasParamSpec]</code>
13711378
/// If you have declared a newtype as
@@ -1390,7 +1397,7 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
13901397
///
13911398
/// # Example
13921399
/// ```
1393-
/// use std::cell::RefCell;
1400+
/// use std::cell::{Cell, RefCell};
13941401
/// use glib::prelude::*;
13951402
/// use glib::subclass::prelude::*;
13961403
/// use glib_macros::Properties;
@@ -1401,6 +1408,14 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
14011408
/// nick: String,
14021409
/// }
14031410
///
1411+
/// #[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum, Default)]
1412+
/// #[enum_type(name = "MyEnum")]
1413+
/// enum MyEnum {
1414+
/// #[default]
1415+
/// Val,
1416+
/// OtherVal
1417+
/// }
1418+
///
14041419
/// pub mod imp {
14051420
/// use std::rc::Rc;
14061421
///
@@ -1426,6 +1441,8 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
14261441
/// optional: RefCell<Option<String>>,
14271442
/// #[property(get, set)]
14281443
/// smart_pointer: Rc<RefCell<String>>,
1444+
/// #[property(get, set, builder(MyEnum::Val))]
1445+
/// my_enum: Cell<MyEnum>,
14291446
/// /// # Getter
14301447
/// ///
14311448
/// /// Get the value of the property `extra_comments`

0 commit comments

Comments
 (0)