godot_core/builtin/
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//! Built-in types like `Vector2`, `GString` and `Variant`.
9//!
10//! Please read the [book chapter](https://godot-rust.github.io/book/godot-api/builtins.html) about builtin types.
11//!
12//! # API design
13//! API design behind the builtin types (and some wider parts of the library) is elaborated in the
14//! [extended documentation page](../__docs/index.html#builtin-api-design).
15
16// Re-export generated enums.
17pub use crate::gen::central::global_reexported_enums::{Corner, EulerOrder, Side, VariantOperator};
18// Not yet public.
19pub(crate) use crate::gen::central::VariantDispatch;
20pub use crate::sys::VariantType;
21// Re-export macros.
22#[allow(deprecated)] // dict
23pub use crate::{array, dict, real, reals, varray, vdict};
24
25#[doc(hidden)]
26pub mod __prelude_reexport {
27    #[rustfmt::skip] // Do not reorder.
28    use super::*;
29
30    pub use aabb::*;
31    pub use basis::*;
32    pub use callable::*;
33    pub use collections::containers::*;
34    pub use color::*;
35    pub use color_hsv::*;
36    pub use plane::*;
37    pub use projection::*;
38    pub use quaternion::*;
39    pub use real_inner::*;
40    pub use rect2::*;
41    pub use rect2i::*;
42    pub use rid::*;
43    pub use signal::*;
44    pub use string::{Encoding, GString, NodePath, StringName};
45    pub use transform2d::*;
46    pub use transform3d::*;
47    pub use variant::*;
48    pub use vectors::*;
49
50    pub use super::math::XformInv;
51    pub use super::{EulerOrder, Side, VariantOperator, VariantType};
52    pub use crate::{array, real, reals, varray, vdict, vslice};
53
54    #[allow(deprecated)]
55    #[rustfmt::skip] // Do not reorder.
56    pub use crate::dict;
57
58    #[cfg(feature = "trace")] // Test only.
59    pub use crate::static_sname;
60}
61
62pub use __prelude_reexport::*;
63
64/// Math-related functions and traits like [`ApproxEq`][math::ApproxEq].
65pub mod math;
66
67/// Iterator types for arrays and dictionaries.
68// Might rename this to `collections` or so.
69pub mod iter {
70    pub use super::collections::iterators::*;
71}
72
73/// Specialized types related to Godot's various string implementations.
74pub mod strings {
75    pub use super::string::{
76        ExGStringFind, ExGStringSplit, ExStringNameFind, ExStringNameSplit, TransientStringNameOrd,
77    };
78}
79
80pub(crate) mod meta_reexport {
81    pub use super::collections::PackedArrayElement;
82}
83
84// ----------------------------------------------------------------------------------------------------------------------------------------------
85// Implementation
86
87// Modules exporting declarative macros must appear first.
88mod macros;
89
90// Other modules
91mod aabb;
92mod basis;
93mod callable;
94mod collections;
95mod color;
96mod color_constants; // After color, so that constants are listed after methods in docs (alphabetic ensures that).
97mod color_hsv;
98mod plane;
99mod projection;
100mod quaternion;
101mod rect2;
102mod rect2i;
103mod rid;
104mod signal;
105mod string;
106mod transform2d;
107mod transform3d;
108mod variant;
109mod vectors;
110
111// Rename imports because we re-export a subset of types under same module names.
112#[path = "real.rs"]
113mod real_inner;
114
115#[doc(hidden)]
116pub mod inner {
117    pub use crate::gen::builtin_classes::*;
118}
119
120#[macro_export]
121macro_rules! declare_hash_u32_method {
122    ( $( $docs:tt )+ ) => {
123        $( $docs )+
124        pub fn hash_u32(&self) -> u32 {
125            self.as_inner().hash().try_into().expect("Godot hashes are uint32_t")
126        }
127    }
128}
129
130// ----------------------------------------------------------------------------------------------------------------------------------------------
131// Conversion functions
132
133pub(crate) fn to_i64(i: usize) -> i64 {
134    i.try_into().unwrap()
135}
136
137pub(crate) fn to_usize(i: i64) -> usize {
138    i.try_into().unwrap()
139}
140
141// ----------------------------------------------------------------------------------------------------------------------------------------------
142// #[test] utils for serde
143
144#[cfg(all(test, feature = "serde"))] #[cfg_attr(published_docs, doc(cfg(all(test, feature = "serde"))))]
145pub(crate) mod test_utils {
146    use serde::{Deserialize, Serialize};
147
148    pub(crate) fn roundtrip<T>(value: &T, expected_json: &str)
149    where
150        T: for<'a> Deserialize<'a> + Serialize + PartialEq + std::fmt::Debug,
151    {
152        let json: String = serde_json::to_string(value).unwrap();
153        let back: T = serde_json::from_str(json.as_str()).unwrap();
154
155        assert_eq!(back, *value, "serde round-trip changes value");
156        assert_eq!(
157            json, expected_json,
158            "value does not conform to expected JSON"
159        );
160    }
161}