godot_core/meta/mod.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
8//! Meta-information about Godot types, their properties and conversions between them.
9//!
10//! # Conversions between types
11//!
12//! ## Godot representation
13//!
14//! The library provides two traits [`FromGodot`] and [`ToGodot`], which are used at the Rust <-> Godot boundary, both in user-defined functions
15//! ([`#[func]`](../register/attr.godot_api.html#user-defined-functions)) and engine APIs ([`godot::classes` module](crate::classes)).
16//! Their `to_godot()` and `from_godot()` methods convert types from/to their _closest possible Godot type_ (e.g. `GString` instead of Rust
17//! `String`). You usually don't need to call these methods yourself, they are automatically invoked when passing objects to/from Godot.
18//!
19//! Most often, the two traits appear in pairs, however there are cases where only one of the two is implemented. For example, `&str` implements
20//! `ToGodot` but not `FromGodot`. Additionally, [`GodotConvert`] acts as a supertrait of both [`FromGodot`] and [`ToGodot`]. Its sole purpose
21//! is to define the "closest possible Godot type" [`GodotConvert::Via`].
22//!
23//! For fallible conversions, you can use [`FromGodot::try_from_godot()`].
24//!
25//! ## Variants
26//!
27//! [`ToGodot`] and [`FromGodot`] also implement a conversion to/from [`Variant`][crate::builtin::Variant], which is the most versatile Godot
28//! type. This conversion is available via `to_variant()` and `from_variant()` methods. These methods are also available directly on `Variant`
29//! itself, via `to()`, `try_to()` and `from()` functions.
30//!
31//! ## Class conversions
32//!
33//! Godot classes exist in a hierarchy. In OOP, it is usually possible to represent pointers to derived objects as pointer to their bases.
34//! For conversions between base and derived class objects, you can use `Gd` methods [`cast()`][crate::obj::Gd::cast],
35//! [`try_cast()`][crate::obj::Gd::try_cast] and [`upcast()`][crate::obj::Gd::upcast]. Upcasts are infallible.
36//!
37//! ## Argument conversions
38//!
39//! Rust does not support implicit conversions, however it has something very close: the `impl Into<T>` idiom, which can be used to convert
40//! "T-compatible" arguments into `T`.
41//!
42//! This library specializes this idea with the trait [`AsArg<T>`]. `AsArg` allows argument conversions from arguments into `T`.
43//! This is most interesting in the context of strings (so you can pass `&str` to a function expecting `GString`) and objects (pass
44//! `&Gd<Node2D>` to a function expecting `Node2D` objects).
45
46mod args;
47mod class_id;
48mod element_type;
49mod godot_convert;
50mod method_info;
51mod object_to_owned;
52mod param_tuple;
53mod property_info;
54mod signature;
55mod traits;
56mod uniform_object_deref;
57
58pub(crate) mod sealed;
59
60pub mod error;
61pub mod inspect;
62pub(crate) mod signed_range;
63
64// Public re-exports
65pub use args::*;
66#[expect(deprecated)]
67pub use class_id::{ClassId, ClassName};
68pub use element_type::{ElementScript, ElementType};
69pub use godot_convert::{FromGodot, GodotConvert, ToGodot};
70pub use method_info::MethodInfo;
71pub use object_to_owned::ObjectToOwned;
72pub use param_tuple::{InParamTuple, OutParamTuple, ParamTuple};
73pub use property_info::{PropertyHintInfo, PropertyInfo};
74#[cfg(feature = "trace")] #[cfg_attr(published_docs, doc(cfg(feature = "trace")))]
75pub use signature::trace;
76#[doc(hidden)]
77pub use signature::*;
78pub use signed_range::{wrapped, SignedRange};
79pub use traits::{ArrayElement, GodotImmutable, GodotType, PackedArrayElement};
80pub use uniform_object_deref::UniformObjectDeref;
81
82// Public due to signals emit() needing it. Should be made pub(crate) again if that changes.
83pub use crate::arg_into_owned;
84
85// Crate-local re-exports
86mod reexport_crate {
87 pub(crate) use super::traits::{
88 element_godot_type_name, element_variant_type, ffi_variant_type, ExtVariantType,
89 GodotFfiVariant, GodotNullableFfi,
90 };
91 // Private imports for this module only.
92 pub(super) use crate::registry::method::MethodParamOrReturnInfo;
93 pub(crate) use crate::{arg_into_ref, declare_arg_method, impl_godot_as_self};
94}
95pub(crate) use reexport_crate::*;
96
97// ----------------------------------------------------------------------------------------------------------------------------------------------
98
99/// Clean up various resources at end of usage.
100///
101/// # Safety
102/// Must not use meta facilities (e.g. `ClassId`) after this call.
103pub(crate) unsafe fn cleanup() {
104 class_id::cleanup();
105}