|
| 1 | +/* |
| 2 | + * Copyright (c) godot-rust; Bromeon and contributors. |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +/// Dispatches a class to different subclasses. |
| 9 | +/// |
| 10 | +/// Similar to a `match` statement, but with downcasts. Earlier matches dominate, so keep more-derived classes first. |
| 11 | +/// The current implementation checks [`Gd::try_cast()`][crate::obj::Gd::try_cast] linearly with the number of branches. |
| 12 | +/// This may change in the future. |
| 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. |
| 17 | +/// |
| 18 | +/// # Example |
| 19 | +/// ```no_run |
| 20 | +/// # use godot::prelude::*; |
| 21 | +/// # use godot_core::classes::{InputEvent, InputEventAction}; |
| 22 | +/// # fn some_input() -> Gd<InputEvent> { unimplemented!() } |
| 23 | +/// # // Hack to keep amount of SELECTED_CLASSES limited: |
| 24 | +/// # type InputEventMouseButton = InputEventAction; |
| 25 | +/// # type InputEventMouseMotion = InputEventAction; |
| 26 | +/// // Basic syntax. |
| 27 | +/// let event: Gd<InputEvent> = some_input(); |
| 28 | +/// |
| 29 | +/// let simple_dispatch: i32 = match_class!(event, { |
| 30 | +/// InputEventMouseButton(btn) => 1, |
| 31 | +/// InputEventMouseMotion(motion) => 2, |
| 32 | +/// InputEventAction(action) => 3, |
| 33 | +/// _ => 0, // Fallback. |
| 34 | +/// }); |
| 35 | +/// |
| 36 | +/// // More diverse dispatch patterns are also supported. |
| 37 | +/// let fancy_dispatch: i32 = match_class!(some_input(), { |
| 38 | +/// InputEventMouseButton(btn) => 1, |
| 39 | +/// |
| 40 | +/// // Block syntax for multiple statements: |
| 41 | +/// InputEventMouseMotion(motion) => { |
| 42 | +/// godot_print!("motion"); |
| 43 | +/// 2 |
| 44 | +/// }, |
| 45 | +/// |
| 46 | +/// // Qualified types supported: |
| 47 | +/// godot::classes::InputEventAction(action) => 3, |
| 48 | +/// |
| 49 | +/// // Fallback with variable -- retrieves original Gd<InputEvent>. |
| 50 | +/// // Equivalent to pattern `InputEvent(original)`. |
| 51 | +/// _(original) => 0, |
| 52 | +/// }); |
| 53 | +/// |
| 54 | +/// // event_type is now 0, 1, 2, or 3 |
| 55 | +/// ``` |
| 56 | +/// |
| 57 | +/// # Limitations |
| 58 | +/// The expression block is currently wrapped by a closure, so you cannot use control-flow statements like `?`, `return`, `continue`, `break`. |
| 59 | +#[macro_export] |
| 60 | +// Note: annoyingly shows full implementation in docs. For workarounds, either move impl to a helper macro, or use something like |
| 61 | +// https://crates.io/crates/clean-macro-docs. |
| 62 | +macro_rules! match_class { |
| 63 | + ($subject:expr, { |
| 64 | + $( |
| 65 | + $($class:ident)::+($var:ident) => $body:expr |
| 66 | + ),+, |
| 67 | + _($fallback_var:ident) => $fallback:expr |
| 68 | + $(,)? |
| 69 | + }) => { |
| 70 | + (|| { |
| 71 | + let mut __evt = $subject; |
| 72 | + $( |
| 73 | + __evt = match __evt.try_cast::<$($class)::*>() { |
| 74 | + Ok($var) => return $body, |
| 75 | + Err(e) => e, |
| 76 | + }; |
| 77 | + )+ |
| 78 | + let $fallback_var = __evt; |
| 79 | + $fallback |
| 80 | + })() |
| 81 | + }; |
| 82 | + |
| 83 | + ($subject:expr, { |
| 84 | + $( |
| 85 | + $($class:ident)::+($var:ident) => $body:expr |
| 86 | + ),+, |
| 87 | + _ => $fallback:expr |
| 88 | + $(,)? |
| 89 | + }) => { |
| 90 | + (|| { |
| 91 | + let mut __evt = $subject; |
| 92 | + $( |
| 93 | + __evt = match __evt.try_cast::<$($class)::*>() { |
| 94 | + Ok($var) => return $body, |
| 95 | + Err(e) => e, |
| 96 | + }; |
| 97 | + )+ |
| 98 | + $fallback |
| 99 | + })() |
| 100 | + }; |
| 101 | +} |
0 commit comments