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