|
| 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 | +/// button @ InputEventMouseButton => 1, |
| 31 | +/// motion @ InputEventMouseMotion => 2, |
| 32 | +/// action @ InputEventAction => 3, |
| 33 | +/// _ => 0, // Fallback. |
| 34 | +/// }); |
| 35 | +/// |
| 36 | +/// // More diverse dispatch patterns are also supported. |
| 37 | +/// let fancy_dispatch: i32 = match_class!(some_input(), { |
| 38 | +/// button @ InputEventMouseButton => 1, |
| 39 | +/// |
| 40 | +/// // Block syntax for multiple statements: |
| 41 | +/// motion @ InputEventMouseMotion => { |
| 42 | +/// godot_print!("motion"); |
| 43 | +/// 2 |
| 44 | +/// }, |
| 45 | +/// |
| 46 | +/// // Qualified types supported: |
| 47 | +/// action @ godot::classes::InputEventAction => 3, |
| 48 | +/// |
| 49 | +/// // Fallback with variable -- retrieves original Gd<InputEvent>. |
| 50 | +/// // Equivalent to pattern `original @ InputEvent`. |
| 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 | + // TT muncher approach: consume one arm at a time and recurse with the remaining tokens. |
| 64 | + |
| 65 | + // Entry point: grab subject + ENTIRE list of arms as token-trees. |
| 66 | + ($subject:expr, { $($arms:tt)* }) => { |
| 67 | + (|| { |
| 68 | + let mut __match_subject = $subject; |
| 69 | + match_class!(@munch __match_subject; $($arms)*); |
| 70 | + // unreachable!("match_class hit end with no `_` fallback"); |
| 71 | + })() |
| 72 | + }; |
| 73 | + |
| 74 | + // ident @ Some::Path => expr, rest... |
| 75 | + (@munch $evt:ident; |
| 76 | + $var:ident @ $($class:ident)::+ => $body:expr, |
| 77 | + $($rest:tt)* |
| 78 | + ) => { |
| 79 | + // try the down‐cast |
| 80 | + $evt = match $evt.try_cast::< $($class)::* >() { |
| 81 | + Ok($var) => return $body, |
| 82 | + Err(e) => e, |
| 83 | + }; |
| 84 | + match_class!(@munch $evt; $($rest)*); |
| 85 | + }; |
| 86 | + |
| 87 | + // foo @ _ => expr |
| 88 | + (@munch $evt:ident; |
| 89 | + $fallback_var:ident @ $pat:tt => $fallback:expr |
| 90 | + $(,)? |
| 91 | + ) => { |
| 92 | + // `$pat` here will only ever be `_` if no typed‐arm matched first, because `Some::Path` would hit the more specific rule above. |
| 93 | + let $fallback_var = $evt; |
| 94 | + return $fallback; |
| 95 | + }; |
| 96 | + |
| 97 | + // _ => expr |
| 98 | + (@munch $evt:ident; |
| 99 | + _ => $fallback:expr |
| 100 | + $(,)? |
| 101 | + ) => {{ |
| 102 | + return $fallback; |
| 103 | + }}; |
| 104 | +} |
0 commit comments