godot_core/obj/
traits.rs

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
8use godot_ffi as sys;
9
10use crate::builder::ClassBuilder;
11use crate::builtin::GString;
12use crate::init::InitLevel;
13use crate::meta::inspect::EnumConstant;
14use crate::meta::ClassId;
15use crate::obj::{bounds, Base, BaseMut, BaseRef, Bounds, Gd};
16use crate::registry::signal::SignalObject;
17use crate::storage::Storage;
18
19/// Makes `T` eligible to be managed by Godot and stored in [`Gd<T>`][crate::obj::Gd] pointers.
20///
21/// The behavior of types implementing this trait is influenced by the associated types; check their documentation for information.
22///
23/// Normally, you don't need to implement this trait yourself; use [`#[derive(GodotClass)]`](../register/derive.GodotClass.html) instead.
24// Above intra-doc link to the derive-macro only works as HTML, not as symbol link.
25#[diagnostic::on_unimplemented(
26    message = "only classes registered with Godot are allowed in this context",
27    note = "you can use `#[derive(GodotClass)]` to register your own structs with Godot",
28    note = "see also: https://godot-rust.github.io/book/register/classes.html"
29)]
30pub trait GodotClass: Bounds + 'static
31where
32    Self: Sized,
33{
34    /// The immediate superclass of `T`. This is always a Godot engine class.
35    type Base: GodotClass; // not EngineClass because it can be ()
36
37    /// Globally unique class ID, linked to the name under which the class is registered in Godot.
38    ///
39    /// The name may deviate from the Rust struct name: `HttpRequest::class_id().as_str() == "HTTPRequest"`.
40    fn class_id() -> ClassId;
41
42    #[deprecated = "Renamed to `class_id()`"]
43    fn class_name() -> ClassId {
44        Self::class_id()
45    }
46
47    /// Initialization level, during which this class should be initialized with Godot.
48    ///
49    /// The default is a good choice in most cases; override only if you have very specific initialization requirements.
50    /// It must not be less than `Base::INIT_LEVEL`.
51    const INIT_LEVEL: InitLevel = <Self::Base as GodotClass>::INIT_LEVEL;
52
53    /// Returns whether `Self` inherits from `Base`.
54    ///
55    /// This is reflexive, i.e `Self` inherits from itself.
56    ///
57    /// See also [`Inherits`] for a trait bound.
58    fn inherits<Base: GodotClass>() -> bool {
59        if Self::class_id() == Base::class_id() {
60            true
61        } else if Self::Base::class_id() == <NoBase>::class_id() {
62            false
63        } else {
64            Self::Base::inherits::<Base>()
65        }
66    }
67}
68
69/// Type representing the absence of a base class, at the root of the hierarchy.
70///
71/// `NoBase` is used as the base class for exactly one class: [`Object`][crate::classes::Object].
72///
73/// This is an enum without any variants, as we should never construct an instance of this class.
74pub enum NoBase {}
75
76impl GodotClass for NoBase {
77    type Base = NoBase;
78
79    fn class_id() -> ClassId {
80        ClassId::none()
81    }
82
83    const INIT_LEVEL: InitLevel = InitLevel::Core; // arbitrary; never read.
84}
85
86unsafe impl Bounds for NoBase {
87    type Memory = bounds::MemManual;
88    type DynMemory = bounds::MemManual;
89    type Declarer = bounds::DeclEngine;
90    type Exportable = bounds::No;
91}
92
93/// Non-strict inheritance relationship in the Godot class hierarchy.
94///
95/// `Derived: Inherits<Base>` means that either `Derived` is a subclass of `Base`, or the class `Base` itself (hence "non-strict").
96///
97/// This trait is automatically implemented for all Godot engine classes and user-defined classes that derive [`GodotClass`].
98/// It has `GodotClass` as a supertrait, allowing your code to have bounds solely on `Derived: Inherits<Base>` rather than
99/// `Derived: Inherits<Base> + GodotClass`.
100///
101/// Inheritance is transitive across indirect base classes: `Node3D` implements `Inherits<Node>` and `Inherits<Object>`.
102///
103/// The trait is also reflexive: `T` always implements `Inherits<T>`.
104///
105/// # Usage
106///
107/// The primary use case for this trait is polymorphism: you write a function that accepts anything that derives from a certain class
108/// (including the class itself):
109/// ```no_run
110/// # use godot::prelude::*;
111/// fn print_node<T>(node: Gd<T>)
112/// where
113///     T: Inherits<Node>,
114/// {
115///     let up = node.upcast(); // type Gd<Node> inferred
116///     println!("Node #{} with name {}", up.instance_id(), up.get_name());
117///     up.free();
118/// }
119///
120/// // Call with different types
121/// print_node(Node::new_alloc());   // works on T=Node as well
122/// print_node(Node2D::new_alloc()); // or derived classes
123/// print_node(Node3D::new_alloc());
124/// ```
125///
126/// A variation of the above pattern works without `Inherits` or generics, if you move the `upcast()` into the call site:
127/// ```no_run
128/// # use godot::prelude::*;
129/// fn print_node(node: Gd<Node>) { /* ... */ }
130///
131/// // Call with different types
132/// print_node(Node::new_alloc());            // no upcast needed
133/// print_node(Node2D::new_alloc().upcast());
134/// print_node(Node3D::new_alloc().upcast());
135/// ```
136///
137/// # Safety
138///
139/// This trait must only be implemented for subclasses of `Base`.
140///
141/// Importantly, this means it is always safe to upcast a value of type `Gd<Self>` to `Gd<Base>`.
142pub unsafe trait Inherits<Base: GodotClass>: GodotClass {
143    /// True iff `Self == Base`.
144    ///
145    /// Exists because something like C++'s [`std::is_same`](https://en.cppreference.com/w/cpp/types/is_same.html) is notoriously difficult
146    /// in stable Rust, due to lack of specialization.
147    const IS_SAME_CLASS: bool = false;
148}
149
150// SAFETY: Every class is a subclass of itself.
151unsafe impl<T: GodotClass> Inherits<T> for T {
152    const IS_SAME_CLASS: bool = true;
153}
154
155/// Trait that defines a `T` -> `dyn Trait` relation for use in [`DynGd`][crate::obj::DynGd].
156///
157/// You should typically not implement this manually, but use the [`#[godot_dyn]`](../register/attr.godot_dyn.html) macro.
158#[diagnostic::on_unimplemented(
159    message = "`{Trait}` needs to be a trait object linked with class `{Self}` in the library",
160    note = "you can use `#[godot_dyn]` on `impl Trait for Class` to auto-generate `impl Implements<dyn Trait> for Class`"
161)]
162// Note: technically, `Trait` doesn't _have to_ implement `Self`. The Rust type system provides no way to verify that a) D is a trait object,
163// and b) that the trait behind it is implemented for the class. Thus, users could any another reference type, such as `&str` pointing to a field.
164// This should be safe, since lifetimes are checked throughout and the class instance remains in place (pinned) inside a DynGd.
165pub trait AsDyn<Trait>: GodotClass
166where
167    Trait: ?Sized + 'static,
168{
169    fn dyn_upcast(&self) -> &Trait;
170    fn dyn_upcast_mut(&mut self) -> &mut Trait;
171}
172
173/// Implemented for all user-defined classes, providing extensions on the raw object to interact with `Gd`.
174#[doc(hidden)]
175pub trait UserClass: Bounds<Declarer = bounds::DeclUser> {
176    #[doc(hidden)]
177    fn __config() -> crate::private::ClassConfig;
178
179    #[doc(hidden)]
180    fn __before_ready(&mut self);
181
182    #[doc(hidden)]
183    fn __default_virtual_call(
184        _method_name: &str,
185        #[cfg(since_api = "4.4")] _hash: u32,
186    ) -> sys::GDExtensionClassCallVirtual {
187        None
188    }
189}
190
191/// Auto-implemented for all engine-provided enums.
192pub trait EngineEnum: Copy {
193    fn try_from_ord(ord: i32) -> Option<Self>;
194
195    /// Ordinal value of the enumerator, as specified in Godot.
196    /// This is not necessarily unique.
197    fn ord(self) -> i32;
198
199    fn from_ord(ord: i32) -> Self {
200        Self::try_from_ord(ord)
201            .unwrap_or_else(|| panic!("ordinal {ord} does not map to any enumerator"))
202    }
203
204    /// The name of the enumerator, as it appears in Rust.
205    ///
206    /// Note that **this may not match the Rust constant name.** In case of multiple constants with the same ordinal value, this method returns
207    /// the first one in the order of definition. For example, [`LayoutDirection::LOCALE.as_str()`][crate::classes::window::LayoutDirection::LOCALE]
208    /// (ord 1) returns `"APPLICATION_LOCALE"`, because that happens to be the first constant with ordinal `1`.
209    /// See [`all_constants()`][Self::all_constants] for a more robust and general approach to introspection of enum constants.
210    ///
211    /// If the value does not match one of the known enumerators, the empty string is returned.
212    fn as_str(&self) -> &'static str;
213
214    /// Returns a slice of distinct enum values.
215    ///
216    /// This excludes `MAX` constants at the end (existing only to express the number of enumerators) and deduplicates aliases,
217    /// providing only meaningful enum values. See [`all_constants()`][Self::all_constants] for a complete list of all constants.
218    ///
219    /// Enables iteration over distinct enum variants:
220    /// ```no_run
221    /// use godot::classes::window;
222    /// use godot::obj::EngineEnum;
223    ///
224    /// for mode in window::Mode::values() {
225    ///     println!("* {}: {}", mode.as_str(), mode.ord());
226    /// }
227    /// ```
228    fn values() -> &'static [Self];
229
230    /// Returns metadata for all enum constants.
231    ///
232    /// This includes all constants as they appear in the enum definition, including duplicates and `MAX` constants.
233    /// For a list of useful, distinct values, use [`values()`][Self::values].
234    ///
235    /// Enables introspection of available constants:
236    /// ```no_run
237    /// use godot::classes::window;
238    /// use godot::obj::EngineEnum;
239    ///
240    /// for constant in window::Mode::all_constants() {
241    ///     println!("* window::Mode.{} (original {}) has ordinal value {}.",
242    ///         constant.rust_name(),
243    ///         constant.godot_name(),
244    ///         constant.value().ord()
245    ///     );
246    /// }
247    /// ```
248    fn all_constants() -> &'static [EnumConstant<Self>];
249}
250
251/// Auto-implemented for all engine-provided bitfields.
252pub trait EngineBitfield: Copy {
253    fn try_from_ord(ord: u64) -> Option<Self>;
254
255    /// Ordinal value of the bit flag, as specified in Godot.
256    fn ord(self) -> u64;
257
258    fn from_ord(ord: u64) -> Self {
259        Self::try_from_ord(ord)
260            .unwrap_or_else(|| panic!("ordinal {ord} does not map to any valid bit flag"))
261    }
262
263    // TODO consolidate API: named methods vs. | & ! etc.
264    fn is_set(self, flag: Self) -> bool {
265        self.ord() & flag.ord() != 0
266    }
267
268    /// Returns metadata for all bitfield constants.
269    ///
270    /// This includes all constants as they appear in the bitfield definition.
271    ///
272    /// Enables introspection of available constants:
273    /// ```no_run
274    /// use godot::global::KeyModifierMask;
275    /// use godot::obj::EngineBitfield;
276    ///
277    /// for constant in KeyModifierMask::all_constants() {
278    ///     println!("* KeyModifierMask.{} (original {}) has ordinal value {}.",
279    ///         constant.rust_name(),
280    ///         constant.godot_name(),
281    ///         constant.value().ord()
282    ///     );
283    /// }
284    /// ```
285    fn all_constants() -> &'static [EnumConstant<Self>];
286}
287
288/// Trait for enums that can be used as indices in arrays.
289///
290/// The conditions for a Godot enum to be "index-like" are:
291/// - Contains an enumerator ending in `_MAX`, which has the highest ordinal (denotes the size).
292/// - All other enumerators are consecutive integers inside `0..max` (no negative ordinals, no gaps).
293///
294/// Duplicates are explicitly allowed, to allow for renamings/deprecations. The order in which Godot exposes
295/// the enumerators in the JSON is irrelevant.
296pub trait IndexEnum: EngineEnum {
297    /// Number of **distinct** enumerators in the enum.
298    ///
299    /// All enumerators are guaranteed to be in the range `0..ENUMERATOR_COUNT`, so you can use them
300    /// as indices in an array of size `ENUMERATOR_COUNT`.
301    ///
302    /// Keep in mind that two enumerators with the same ordinal are only counted once.
303    const ENUMERATOR_COUNT: usize;
304
305    /// Converts the enumerator to `usize`, which can be used as an array index.
306    ///
307    /// Note that two enumerators may have the same index, if they have the same ordinal.
308    fn to_index(self) -> usize {
309        self.ord() as usize
310    }
311}
312
313/// Trait that is automatically implemented for user classes containing a `Base<T>` field.
314///
315/// Gives direct access to the containing `Gd<Self>` from `self`.
316///
317/// # Usage as a bound
318///
319/// In order to call `base()` or `base_mut()` within a function or on a type you define, you need a `WithBaseField<Base = T>` bound,
320/// where `T` is the base class of your type.
321///
322/// ```no_run
323/// # use godot::prelude::*;
324/// # use godot::obj::WithBaseField;
325/// fn some_fn<T>(value: &T)
326/// where
327///     T: WithBaseField<Base = Node3D>,
328/// {
329///     let base = value.base();
330///     let pos = base.get_position();
331/// }
332/// ```
333///
334// Possible alternative for builder APIs, although even less ergonomic: Base<T> could be Base<T, Self> and return Gd<Self>.
335#[diagnostic::on_unimplemented(
336    message = "Class `{Self}` requires a `Base<T>` field",
337    label = "missing field `_base: Base<...>` in struct declaration",
338    note = "a base field is required to access the base from within `self`, as well as for #[signal], #[rpc] and #[func(virtual)]",
339    note = "see also: https://godot-rust.github.io/book/register/classes.html#the-base-field"
340)]
341pub trait WithBaseField: GodotClass + Bounds<Declarer = bounds::DeclUser> {
342    /// Returns the `Gd` pointer containing this object.
343    ///
344    /// This is intended to be stored or passed to engine methods. You cannot call `bind()` or `bind_mut()` on it, while the method
345    /// calling `to_gd()` is still running; that would lead to a double borrow panic.
346    ///
347    /// # Panics
348    /// If called during initialization (the `init()` function or `Gd::from_init_fn()`). Use [`Base::to_init_gd()`] instead.
349    fn to_gd(&self) -> Gd<Self>;
350
351    /// Returns a reference to the `Base` stored by this object.
352    #[doc(hidden)]
353    fn base_field(&self) -> &Base<Self::Base>;
354
355    /// Returns a shared reference guard, suitable for calling `&self` engine methods on this object.
356    ///
357    /// Holding a shared guard prevents other code paths from obtaining a _mutable_ reference to `self`, as such it is recommended to drop the
358    /// guard as soon as you no longer need it.
359    ///
360    /// # Examples
361    ///
362    /// ```no_run
363    /// use godot::prelude::*;
364    ///
365    /// #[derive(GodotClass)]
366    /// #[class(init, base=Node)]
367    /// struct MyClass {
368    ///     base: Base<Node>,
369    /// }
370    ///
371    /// #[godot_api]
372    /// impl INode for MyClass {
373    ///     fn process(&mut self, _delta: f32) {
374    ///         let name = self.base().get_name();
375    ///         godot_print!("name is {name}");
376    ///     }
377    /// }
378    /// ```
379    ///
380    /// However, we cannot call methods that require `&mut Base`, such as
381    /// [`Node::add_child()`](crate::classes::Node::add_child).
382    ///
383    /// ```compile_fail
384    /// use godot::prelude::*;
385    ///
386    /// #[derive(GodotClass)]
387    /// #[class(init, base = Node)]
388    /// struct MyClass {
389    ///     ///     base: Base<Node>,
390    /// }
391    ///
392    /// #[godot_api]
393    /// impl INode for MyClass {
394    ///     fn process(&mut self, _delta: f32) {
395    ///         let node = Node::new_alloc();
396    ///         // fails because `add_child` requires a mutable reference.
397    ///         self.base().add_child(&node);
398    ///     }
399    /// }
400    ///
401    /// # pub struct Test;
402    ///
403    /// # #[gdextension]
404    /// # unsafe impl ExtensionLibrary for Test {}
405    /// ```
406    ///
407    /// For this, use [`base_mut()`](WithBaseField::base_mut()) instead.
408    fn base(&self) -> BaseRef<'_, Self> {
409        // SAFETY: lifetime is bound to self through BaseRef, ensuring the object remains valid.
410        let passive_gd = unsafe { self.base_field().constructed_passive() };
411        BaseRef::new(passive_gd, self)
412    }
413
414    /// Returns an exclusive reference guard, suitable for calling `&self`/`&mut self` engine methods on this object.
415    ///
416    /// This method will allow you to call back into the same object from Godot -- something that [`to_gd()`][Self::to_gd] does not allow.
417    /// You have to keep the `BaseMut` guard bound for the entire duration the engine might re-enter a function of your class. The guard
418    /// temporarily absorbs the `&mut self` reference, which allows for an additional exclusive (mutable) reference to be acquired.
419    ///
420    /// Holding an exclusive guard prevents other code paths from obtaining _any_ reference to `self`, as such it is recommended to drop the
421    /// guard as soon as you no longer need it.
422    ///
423    /// # Examples
424    ///
425    /// ```no_run
426    /// # use godot::prelude::*;
427    /// #[derive(GodotClass)]
428    /// #[class(init, base = Node)]
429    /// struct MyClass {
430    ///     base: Base<Node>,
431    /// }
432    ///
433    /// #[godot_api]
434    /// impl INode for MyClass {
435    ///     fn process(&mut self, _delta: f32) {
436    ///         let node = Node::new_alloc();
437    ///         self.base_mut().add_child(&node);
438    ///     }
439    /// }
440    ///
441    /// # pub struct Test;
442    ///
443    /// # #[gdextension]
444    /// # unsafe impl ExtensionLibrary for Test {}
445    /// ```
446    ///
447    /// We can call back into `self` through Godot:
448    ///
449    /// ```no_run
450    /// # use godot::prelude::*;
451    /// #[derive(GodotClass)]
452    /// #[class(init, base=Node)]
453    /// struct MyClass {
454    ///     base: Base<Node>,
455    /// }
456    ///
457    /// #[godot_api]
458    /// impl INode for MyClass {
459    ///     fn process(&mut self, _delta: f32) {
460    ///         self.base_mut().call("other_method", &[]);
461    ///     }
462    /// }
463    ///
464    /// #[godot_api]
465    /// impl MyClass {
466    ///     #[func]
467    ///     fn other_method(&mut self) {}
468    /// }
469    /// ```
470    ///
471    /// Rust's borrow checking rules are enforced if you try to overlap `base_mut()` calls:
472    /// ```compile_fail
473    /// # use godot::prelude::*;
474    /// # #[derive(GodotClass)]
475    /// # #[class(init)]
476    /// # struct MyStruct {
477    /// #     base: Base<RefCounted>,
478    /// # }
479    /// # impl MyStruct {
480    /// // error[E0499]: cannot borrow `*self` as mutable more than once at a time
481    ///
482    /// fn method(&mut self) {
483    ///     let mut a = self.base_mut();
484    ///     //          ---- first mutable borrow occurs here
485    ///     let mut b = self.base_mut();
486    ///     //          ^^^^ second mutable borrow occurs here
487    /// }
488    /// # }
489    /// ```
490    #[allow(clippy::let_unit_value)]
491    fn base_mut(&mut self) -> BaseMut<'_, Self> {
492        // We need to construct this first, as the mut-borrow below will block all other access.
493        // SAFETY: lifetime is re-established at the bottom BaseMut construction, since return type of this fn has lifetime bound to instance.
494        let passive_gd = unsafe { self.base_field().constructed_passive() };
495
496        let gd = self.to_gd();
497
498        // SAFETY:
499        // - We have a `Gd<Self>` so, provided that `storage_unbounded` succeeds, the associated instance
500        //   storage has been created.
501        //
502        // - Since we can get a `&'a Base<Self::Base>` from `&'a self`, that must mean we have a Rust object
503        //   somewhere that has this base object. The only way to have such a base object is by being the
504        //   Rust object referenced by that base object. I.e. this storage's user-instance is that Rust
505        //   object. That means this storage cannot be destroyed for the lifetime of that Rust object. And
506        //   since we have a reference to the base object derived from that Rust object, then that Rust
507        //   object must outlive `'a`. And so the storage cannot be destroyed during the lifetime `'a`.
508        let storage = unsafe {
509            gd.raw
510                .storage_unbounded()
511                .expect("we have Gd<Self>; its RawGd should not be null")
512        };
513
514        let guard = storage.get_inaccessible(self);
515
516        // Narrows lifetime again from 'static to 'self.
517        BaseMut::new(passive_gd, guard)
518    }
519
520    /// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
521    ///
522    /// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
523    /// `&mut Self` allowing direct access to Rust fields and methods.
524    ///
525    /// See also [`Gd::run_deferred()`] to defer logic outside of `self`.
526    ///
527    /// # Panics
528    /// If called outside the main thread.
529    fn run_deferred<F>(&mut self, mut_self_method: F)
530    where
531        F: FnOnce(&mut Self) + 'static,
532    {
533        // We need to copy the Gd, because the lifetime of `&mut self` does not extend throughout the closure, which will only be called
534        // deferred. It might even be freed in-between, causing panic on bind_mut().
535        self.to_gd().run_deferred(mut_self_method)
536    }
537
538    /// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
539    ///
540    /// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
541    /// `Gd<Self>`, which can be used to call engine methods or [`bind()`][Gd::bind]/[`bind_mut()`][Gd::bind_mut] to access the Rust object.
542    ///
543    /// See also [`Gd::run_deferred_gd()`] to defer logic outside of `self`.
544    ///
545    /// # Panics
546    /// If called outside the main thread.
547    fn run_deferred_gd<F>(&mut self, gd_function: F)
548    where
549        F: FnOnce(Gd<Self>) + 'static,
550    {
551        self.to_gd().run_deferred_gd(gd_function)
552    }
553
554    #[deprecated = "Split into `run_deferred()` + `run_deferred_gd()`."]
555    fn apply_deferred<F>(&mut self, rust_function: F)
556    where
557        F: FnOnce(&mut Self) + 'static,
558    {
559        self.run_deferred(rust_function)
560    }
561}
562
563/// Implemented for all classes with registered signals, both engine- and user-declared.
564///
565/// This trait enables the [`Gd::signals()`] method.
566///
567/// User-defined classes with `#[signal]` additionally implement [`WithUserSignals`].
568// Inherits bound makes some up/downcasting in signals impl easier.
569pub trait WithSignals: GodotClass + Inherits<crate::classes::Object> {
570    /// The associated struct listing all signals of this class.
571    ///
572    /// Parameters:
573    /// - `'c` denotes the lifetime during which the class instance is borrowed and its signals can be modified.
574    /// - `C` is the concrete class on which the signals are provided. This can be different than `Self` in case of derived classes
575    ///   (e.g. a user-defined node) connecting/emitting signals of a base class (e.g. `Node`).
576    type SignalCollection<'c, C>
577    where
578        C: WithSignals;
579
580    /// Whether the representation needs to be able to hold just `Gd` (for engine classes) or `UserSignalObject` (for user classes).
581    // Note: this cannot be in Declarer (Engine/UserDecl) as associated type `type SignalObjectType<'c, T: WithSignals>`,
582    // because the user impl has the additional requirement T: WithUserSignals.
583    #[doc(hidden)]
584    type __SignalObj<'c>: SignalObject<'c>;
585    // type __SignalObj<'c, C>: SignalObject<'c>
586    // where
587    //     C: WithSignals + 'c;
588
589    /// Create from existing `Gd`, to enable `Gd::signals()`.
590    ///
591    /// Only used for constructing from a concrete class, so `C = Self` in the return type.
592    ///
593    /// Takes by reference and not value, to retain lifetime chain.
594    #[doc(hidden)]
595    fn __signals_from_external(external: &Gd<Self>) -> Self::SignalCollection<'_, Self>;
596}
597
598/// Implemented for user-defined classes with at least one `#[signal]` declaration.
599///
600/// Allows to access signals from within the class, as `self.signals()`. This requires a `Base<T>` field.
601pub trait WithUserSignals: WithSignals + WithBaseField {
602    /// Access user-defined signals of the current object `self`.
603    ///
604    /// For classes that have at least one `#[signal]` defined, returns a collection of signal names. Each returned signal has a specialized
605    /// API for connecting and emitting signals in a type-safe way. If you need to access signals from outside (given a `Gd` pointer), use
606    /// [`Gd::signals()`] instead.
607    ///
608    /// If you haven't already, read the [book chapter about signals](https://godot-rust.github.io/book/register/signals.html) for a
609    /// walkthrough.
610    ///
611    /// # Provided API
612    ///
613    /// The returned collection provides a method for each signal, with the same name as the corresponding `#[signal]`.  \
614    /// For example, if you have...
615    /// ```ignore
616    /// #[signal]
617    /// fn damage_taken(&mut self, amount: i32);
618    /// ```
619    /// ...then you can access the signal as `self.signals().damage_taken()`, which returns an object with the following API:
620    /// ```ignore
621    /// // Connects global or associated function, or a closure.
622    /// fn connect(f: impl FnMut(i32));
623    ///
624    /// // Connects a &mut self method or closure on the emitter object.
625    /// fn connect_self(f: impl FnMut(&mut Self, i32));
626    ///
627    /// // Connects a &mut self method or closure on another object.
628    /// fn connect_other<C>(f: impl FnMut(&mut C, i32));
629    ///
630    /// // Emits the signal with the given arguments.
631    /// fn emit(amount: i32);
632    /// ```
633    ///
634    /// See [`TypedSignal`](crate::registry::signal::TypedSignal) for more information.
635    fn signals(&mut self) -> Self::SignalCollection<'_, Self>;
636}
637
638/// Extension trait for all reference-counted classes.
639pub trait NewGd: GodotClass {
640    /// Return a new, ref-counted `Gd` containing a default-constructed instance.
641    ///
642    /// `MyClass::new_gd()` is equivalent to `Gd::<MyClass>::default()`.
643    ///
644    /// # Panics
645    /// If `Self` is user-defined and its default constructor `init()` panics, that panic is propagated.
646    fn new_gd() -> Gd<Self>;
647}
648
649impl<T> NewGd for T
650where
651    T: cap::GodotDefault + Bounds<Memory = bounds::MemRefCounted>,
652{
653    fn new_gd() -> Gd<Self> {
654        Gd::default()
655    }
656}
657
658/// Extension trait for all manually managed classes.
659pub trait NewAlloc: GodotClass {
660    /// Return a new, manually-managed `Gd` containing a default-constructed instance.
661    ///
662    /// The result must be manually managed, e.g. by attaching it to the scene tree or calling `free()` after usage.
663    /// Failure to do so will result in memory leaks.
664    ///
665    /// # Panics
666    /// If `Self` is user-defined and its default constructor `init()` panics, that panic is propagated to the caller.
667    #[must_use]
668    fn new_alloc() -> Gd<Self>;
669}
670
671/// Trait for singleton classes in Godot.
672///
673/// There is only one instance of each singleton class in the engine, accessible through [`singleton()`][Self::singleton].
674pub trait Singleton: GodotClass {
675    // Note: we cannot return &'static mut Self, as this would be very easy to mutably alias. Returning &'static Self is possible,  but we'd
676    // lose the whole mutability information (even if that is best-effort and not strict Rust mutability, it makes the API much more usable).
677    // As long as the user has multiple Gd smart pointers to the same singletons, only the internal raw pointers are aliased.
678    // See also Deref/DerefMut impl for Gd.
679
680    /// Returns the singleton instance.
681    ///
682    /// # Panics
683    /// If called during global init/deinit of godot-rust. Most singletons are only available after the first frame has run.
684    /// See also [`ExtensionLibrary`](../init/trait.ExtensionLibrary.html#availability-of-godot-apis-during-init-and-deinit).
685    fn singleton() -> Gd<Self>;
686}
687
688impl<T> NewAlloc for T
689where
690    T: cap::GodotDefault + Bounds<Memory = bounds::MemManual>,
691{
692    fn new_alloc() -> Gd<Self> {
693        use crate::obj::bounds::Declarer as _;
694
695        <Self as Bounds>::Declarer::create_gd()
696    }
697}
698
699// ----------------------------------------------------------------------------------------------------------------------------------------------
700
701/// Capability traits, providing dedicated functionalities for Godot classes
702pub mod cap {
703    use std::any::Any;
704
705    use super::*;
706    use crate::builtin::{StringName, Variant};
707    use crate::meta::PropertyInfo;
708    use crate::obj::{Base, Bounds, Gd};
709    use crate::storage::{IntoVirtualMethodReceiver, VirtualMethodReceiver};
710
711    /// Trait for all classes that are default-constructible from the Godot engine.
712    ///
713    /// Enables the `MyClass.new()` syntax in GDScript, and allows the type to be used by the editor, which often default-constructs objects.
714    ///
715    /// This trait is automatically implemented for the following classes:
716    /// - User defined classes if either:
717    ///   - they override an `init()` method
718    ///   - they have `#[class(init)]` attribute
719    /// - Engine classes if:
720    ///   - they are reference-counted and constructible (i.e. provide a `new()` method).
721    ///
722    /// This trait is not manually implemented, and you cannot call any methods. You can use it as a bound, but typically you'd use
723    /// it indirectly through [`Gd::default()`][crate::obj::Gd::default()]. Note that `Gd::default()` has an additional requirement on
724    /// being reference-counted, meaning not every `GodotDefault` class can automatically be used with `Gd::default()`.
725    #[diagnostic::on_unimplemented(
726        message = "Class `{Self}` requires either an `init` constructor, or explicit opt-out",
727        label = "needs `init`",
728        note = "to provide a default constructor, use `#[class(init)]` or implement an `init` method",
729        note = "to opt out, use `#[class(no_init)]`",
730        note = "see also: https://godot-rust.github.io/book/register/constructors.html"
731    )]
732    pub trait GodotDefault: GodotClass {
733        /// Provides a default smart pointer instance.
734        ///
735        /// Semantics:
736        /// - For user-defined classes, this calls `T::init()` or the generated init-constructor.
737        /// - For engine classes, this calls `T::new()`.
738        #[doc(hidden)]
739        fn __godot_default() -> Gd<Self> {
740            // This is a bit hackish, but the alternatives are:
741            // 1. Separate trait `GodotUserDefault` for user classes, which then proliferates through all APIs and makes abstraction harder.
742            // 2. Repeatedly implementing __godot_default() that forwards to something like Gd::default_user_instance(). Possible, but this
743            //    will make the step toward builder APIs more difficult, as users would need to re-implement this as well.
744            debug_assert_eq!(
745                std::any::TypeId::of::<<Self as Bounds>::Declarer>(),
746                std::any::TypeId::of::<bounds::DeclUser>(),
747                "__godot_default() called on engine class; must be overridden for engine classes"
748            );
749
750            Gd::default_instance()
751        }
752
753        /// Only provided for user classes.
754        #[doc(hidden)]
755        fn __godot_user_init(_base: Base<Self::Base>) -> Self {
756            unreachable!(
757                "__godot_user_init() called on engine class; must be overridden for user classes"
758            )
759        }
760    }
761
762    // TODO Evaluate whether we want this public or not
763    #[doc(hidden)]
764    pub trait GodotToString: GodotClass {
765        #[doc(hidden)]
766        type Recv: IntoVirtualMethodReceiver<Self>;
767
768        #[doc(hidden)]
769        fn __godot_to_string(this: VirtualMethodReceiver<Self>) -> GString;
770    }
771
772    // TODO Evaluate whether we want this public or not
773    #[doc(hidden)]
774    pub trait GodotNotification: GodotClass {
775        #[doc(hidden)]
776        fn __godot_notification(&mut self, what: i32);
777    }
778
779    // TODO Evaluate whether we want this public or not
780    #[doc(hidden)]
781    pub trait GodotRegisterClass: GodotClass {
782        #[doc(hidden)]
783        fn __godot_register_class(builder: &mut ClassBuilder<Self>);
784    }
785
786    #[doc(hidden)]
787    pub trait GodotGet: GodotClass {
788        #[doc(hidden)]
789        type Recv: IntoVirtualMethodReceiver<Self>;
790
791        #[doc(hidden)]
792        fn __godot_get_property(
793            this: VirtualMethodReceiver<Self>,
794            property: StringName,
795        ) -> Option<Variant>;
796    }
797
798    #[doc(hidden)]
799    pub trait GodotSet: GodotClass {
800        #[doc(hidden)]
801        type Recv: IntoVirtualMethodReceiver<Self>;
802
803        #[doc(hidden)]
804        fn __godot_set_property(
805            this: VirtualMethodReceiver<Self>,
806            property: StringName,
807            value: Variant,
808        ) -> bool;
809    }
810
811    #[doc(hidden)]
812    pub trait GodotGetPropertyList: GodotClass {
813        #[doc(hidden)]
814        type Recv: IntoVirtualMethodReceiver<Self>;
815
816        #[doc(hidden)]
817        fn __godot_get_property_list(
818            this: VirtualMethodReceiver<Self>,
819        ) -> Vec<crate::meta::PropertyInfo>;
820    }
821
822    #[doc(hidden)]
823    pub trait GodotPropertyGetRevert: GodotClass {
824        #[doc(hidden)]
825        type Recv: IntoVirtualMethodReceiver<Self>;
826
827        #[doc(hidden)]
828        fn __godot_property_get_revert(
829            this: VirtualMethodReceiver<Self>,
830            property: StringName,
831        ) -> Option<Variant>;
832    }
833
834    #[doc(hidden)]
835    pub trait GodotValidateProperty: GodotClass {
836        #[doc(hidden)]
837        type Recv: IntoVirtualMethodReceiver<Self>;
838
839        #[doc(hidden)]
840        fn __godot_validate_property(
841            this: VirtualMethodReceiver<Self>,
842            property: &mut PropertyInfo,
843        );
844    }
845
846    /// Auto-implemented for `#[godot_api] impl MyClass` blocks
847    pub trait ImplementsGodotApi: GodotClass {
848        #[doc(hidden)]
849        fn __register_methods();
850        #[doc(hidden)]
851        fn __register_constants();
852        #[doc(hidden)]
853        fn __register_rpcs(_: &mut dyn Any) {}
854    }
855
856    pub trait ImplementsGodotExports: GodotClass {
857        #[doc(hidden)]
858        fn __register_exports();
859    }
860
861    /// Auto-implemented for `#[godot_api] impl XyVirtual for MyClass` blocks
862    pub trait ImplementsGodotVirtual: GodotClass {
863        // Cannot use #[cfg(since_api = "4.4")] #[cfg_attr(published_docs, doc(cfg(since_api = "4.4")))] on the `hash` parameter, because the doc-postprocessing generates #[doc(cfg)],
864        // which isn't valid in parameter position.
865
866        #[cfg(before_api = "4.4")] #[cfg_attr(published_docs, doc(cfg(before_api = "4.4")))]
867        #[doc(hidden)]
868        fn __virtual_call(name: &str) -> sys::GDExtensionClassCallVirtual;
869
870        #[cfg(since_api = "4.4")] #[cfg_attr(published_docs, doc(cfg(since_api = "4.4")))]
871        #[doc(hidden)]
872        fn __virtual_call(name: &str, hash: u32) -> sys::GDExtensionClassCallVirtual;
873    }
874}