11
11
/// The current implementation checks [`Gd::try_cast()`][crate::obj::Gd::try_cast] linearly with the number of branches.
12
12
/// This may change in the future.
13
13
///
14
- /// Requires a fallback branch, even if all direct known classes are handled. The reason for this is that there may be other subclasses which
15
- /// are not statically known by godot-rust (e.g. from a script or GDExtension). The fallback branch can either be `_` (discard object), or
16
- /// `variable` to access the original object inside the fallback arm.
14
+ /// When none of the listed classes match, a _fallback branch_ acts as a catch-all and allows to retrieve the original `Gd` pointer.
15
+ /// If the type of the `match_class!` expression is `()`, you can omit the fallback branch. For all other types, it is required, even if all
16
+ /// direct subclasses are handled. The reason for this is that there may be other subclasses which are not statically known by godot-rust
17
+ /// (e.g. from a script or GDExtension).
18
+ ///
19
+ /// The fallback branch can either be `_` (discard object), or `variable` to access the original object inside the fallback arm.
17
20
///
18
21
/// # Example
19
22
/// ```no_run
51
54
/// original => 0,
52
55
/// // Can also be used with mut:
53
56
/// // mut original => 0,
57
+ /// // If the match arms have type (), we can also omit the fallback branch.
54
58
/// };
55
59
///
56
60
/// // event_type is now 0, 1, 2, or 3
@@ -75,14 +79,6 @@ macro_rules! match_class {
75
79
#[ doc( hidden) ]
76
80
#[ macro_export]
77
81
macro_rules! match_class_muncher {
78
- /*
79
- // If we want to support `variable @ _ => { ... }` syntax, use this branch first (to not match `_` as type).
80
- ($subject:ident, $var:ident @ _ => $block:expr $(,)?) => {{
81
- let $var = $subject;
82
- $block
83
- }};
84
- */
85
-
86
82
// mut variable @ Class => { ... }.
87
83
( $subject: ident, mut $var: ident @ $Ty: ty => $block: expr, $( $rest: tt) * ) => { {
88
84
match $subject. try_cast:: <$Ty>( ) {
@@ -115,8 +111,9 @@ macro_rules! match_class_muncher {
115
111
$block
116
112
} } ;
117
113
118
- // _ => { ... }.
119
- ( $subject: ident, _ => $block: expr $( , ) ?) => { {
120
- $block
114
+ // _ => { ... }
115
+ // or nothing, if fallback is absent and overall expression being ().
116
+ ( $subject: ident, $( _ => $block: expr $( , ) ?) ?) => { {
117
+ $( $block) ?
121
118
} } ;
122
119
}
0 commit comments