@@ -17,14 +17,44 @@ mod utils;
17
17
mod varargs;
18
18
mod variant;
19
19
20
- /// Collects method signatures of all functions in a `NativeClass` that have the `#[method]` attribute and registers them with Godot.
20
+ /// Collects method signatures of all functions in a `NativeClass` that have the `#[method]`
21
+ /// attribute and registers them with Godot.
21
22
///
22
- /// **Important**: Only one `impl` block per struct may be attributed with `#[methods]`.
23
+ /// The `#[methods]` attribute can be used with both `impl Type` and `impl Trait for Type`
24
+ /// blocks. The semantics change depending on whether a mix-in name is provided for the block.
23
25
///
24
- /// For more context, please refer to [gdnative::derive::NativeClass](NativeClass).
26
+ /// ## Universal `impl` blocks: `#[methods]`
27
+ ///
28
+ /// An `impl` block that doesn't have a `mixin` parameter is universal. Universal `#[methods]`
29
+ /// blocks **must not overlap**. Usually, this means that **only one** `impl` block per struct
30
+ /// may be universal.
31
+ ///
32
+ /// When applied to a generic `impl`, the `impl` block must apply to **all** monomorphizations
33
+ /// of the type, i.e. be *universal*.
34
+ ///
35
+ /// One applicable universal block must be present for each type one wishes to use as a
36
+ /// `NativeClass`. Universal blocks are always registered automatically.
37
+ ///
38
+ /// ## Mix-ins: `#[methods(mixin = "Name")]`
39
+ ///
40
+ /// When given a name with the `mixin` argument, a block behaves instead as a mix-in block.
41
+ /// `#[method(mixin = "Name")]` creates an opaque type called `Name` under the current scope,
42
+ /// that can be manually registered to any type the `impl` block covers. This can be done in
43
+ /// a `register_with` callback with `builder.mixin::<MyMixin>()`.
44
+ ///
45
+ /// Unlike universal blocks, mix-in blocks have a **many-to-many** relationship with the types
46
+ /// they are registered to. Any number of mix-ins can be applied to any number of compatible
47
+ /// types. This can be useful for reusing generics `impl`s, or organizing code for big interfaces.
48
+ ///
49
+ /// Additionally, the attribute accepts the following arguments:
50
+ ///
51
+ /// - `#[methods(pub)]`<br>
52
+ /// Mix-in types are private by default. The `pub` argument makes them public instead.
25
53
///
26
54
/// ## Example
27
55
///
56
+ /// ### Universal
57
+ ///
28
58
/// ```
29
59
/// use gdnative::prelude::*;
30
60
///
@@ -42,6 +72,30 @@ mod variant;
42
72
/// }
43
73
///
44
74
/// ```
75
+ ///
76
+ /// ### Mix-in
77
+ ///
78
+ /// ```
79
+ /// use gdnative::prelude::*;
80
+ ///
81
+ /// #[derive(NativeClass)]
82
+ /// #[inherit(Reference)]
83
+ /// #[register_with(register_foo)]
84
+ /// #[no_constructor]
85
+ /// struct Foo {}
86
+ ///
87
+ /// fn register_foo(builder: &ClassBuilder<Foo>) {
88
+ /// builder.mixin::<FooMixin>();
89
+ /// }
90
+ ///
91
+ /// #[methods(mixin = "FooMixin")]
92
+ /// impl Foo {
93
+ /// #[method]
94
+ /// fn foo(&self, #[base] _base: &Reference, bar: i64) -> i64 {
95
+ /// bar
96
+ /// }
97
+ /// }
98
+ /// ```
45
99
#[ proc_macro_attribute]
46
100
pub fn methods ( meta : TokenStream , input : TokenStream ) -> TokenStream {
47
101
let args =
@@ -217,7 +271,9 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
217
271
/// ### `#[methods]`
218
272
/// Adds the necessary information to a an `impl` block to register the properties and methods with Godot.
219
273
///
220
- /// **Important**: This needs to be added to one and only one `impl` block for a given `NativeClass`.
274
+ /// One and only one universal `impl` block must be available for each `NativeClass`
275
+ /// monomorphization, along with any number of additional mix-ins. See [`methods`](attr.methods.html)
276
+ /// for more information.
221
277
///
222
278
/// ### `#[method]`
223
279
/// Registers the attributed function signature to be used by Godot.
@@ -468,22 +524,16 @@ pub fn derive_native_class(input: TokenStream) -> TokenStream {
468
524
///
469
525
/// For more context, please refer to [gdnative::derive::NativeClass](NativeClass).
470
526
///
471
- /// # Examples
527
+ /// ## Type attributes
472
528
///
473
- /// ```ignore
474
- /// #[derive(NativeClass)]
475
- /// struct Pair<T> {
476
- /// a: T,
477
- /// b: T,
478
- /// }
529
+ /// The behavior of the attribute can be customized using additional attributes on the type
530
+ /// alias. All type attributes are optional.
479
531
///
480
- /// #[gdnative::derive::monomorphize]
481
- /// type IntPair = Pair<i32>;
532
+ /// ### `#[register_with(path::to::function)]`
482
533
///
483
- /// fn init(handle: InitHandle) {
484
- /// handle.add_class::<IntPair>();
485
- /// }
486
- /// ```
534
+ /// Use a custom function to register signals, properties or methods, in addition
535
+ /// to the one generated by a universal `#[methods]` block on the generic type.
536
+ /// This can be used to register extra mix-ins that apply to the specific monomorphization.
487
537
#[ proc_macro_attribute]
488
538
pub fn monomorphize ( meta : TokenStream , input : TokenStream ) -> TokenStream {
489
539
let args = parse_macro_input ! ( meta as AttributeArgs ) ;
0 commit comments