godot/lib.rs
1#![cfg_attr(published_docs, feature(doc_cfg))]
2/*
3 * Copyright (c) godot-rust; Bromeon and contributors.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 */
8
9//! # Rust bindings for Godot 4
10//!
11//! The **gdext** library implements Rust bindings for the [Godot](https://godotengine.org) engine, more precisely its version 4.
12//! It does so using the GDExtension API, a C interface to integrate third-party language bindings with the engine.
13//!
14//! This API doc is accompanied by the [book](https://github.com/godot-rust/book), which provides tutorials
15//! that guide you along the way.
16//!
17//! An overview of fundamental types and concepts can be found on [this page](__docs).
18//!
19//!
20//! ## Module organization
21//!
22//! The contains generated code, which is derived from the GDExtension API specification. This code spans the official Godot API and
23//! is mostly the same as the API you would use in GDScript.
24//!
25//! The Godot API is divided into several modules:
26//!
27//! * [`builtin`]: Built-in types, such as `Vector2`, `Color`, and `String`.
28//! * [`classes`]: Godot classes, such as `Node`, `RefCounted` or `Resource`.
29//! * [`global`]: Global functions and enums, such as `godot_print!`, `smoothstep` or `JoyAxis`.
30//!
31//! In addition to generated code, we provide a framework that allows you to easily interface the Godot engine.
32//! Noteworthy modules in this context are:
33//!
34//! * [`register`], used to register **your own** Rust symbols (classes, methods, constants etc.) with Godot.
35//! * [`obj`], everything related to handling Godot objects, such as the `Gd<T>` type.
36//! * [`tools`], higher-level utilities that extend the generated code, e.g. `load<T>()`.
37//! * [`meta`], fundamental information about types, properties and conversions.
38//! * [`init`], entry point and global library configuration.
39//!
40//! The [`prelude`] contains often-imported symbols; feel free to `use godot::prelude::*` in your code.
41//! <br><br>
42//!
43//!
44//! ## Public API
45//!
46//! Some symbols in the API are not intended for users, however Rust's visibility feature is not strong enough to express that in all cases
47//! (for example, proc-macros and separated crates may need access to internals).
48//!
49//! The following API symbols are considered private:
50//!
51//! * Symbols annotated with `#[doc(hidden)]`.
52//! * Any of the dependency crates (crate `godot` is the only public interface).
53//! * Modules named `private` and all their contents.
54//!
55//! This means there are **no guarantees** regarding API stability, robustness or correctness. Problems arising from using private APIs are
56//! not considered bugs, and anything relying on them may stop working without announcement. Please refrain from using undocumented and
57//! private features; if you are missing certain functionality, bring it up for discussion instead. This allows us to improve the library!
58//! <br><br>
59//!
60//!
61//! ## Cargo features
62//!
63//! The following features can be enabled for this crate. All of them are off by default.
64//!
65//! Avoid `default-features = false` unless you know exactly what you are doing; it will disable some required internal features.
66//!
67//! _Godot version and configuration:_
68//!
69//! * **`api-4-{minor}`**
70//! * **`api-4-{minor}-{patch}`**
71//! * **`api-custom`**
72//!
73//! Sets the [**API level**](https://godot-rust.github.io/book/toolchain/godot-version.html) to the specified Godot version,
74//! or a custom-built local binary.
75//! You can use at most one `api-*` feature. If absent, the current Godot minor version is used, with patch level 0.<br><br>
76//!
77//! * **`double-precision`**
78//!
79//! Use `f64` instead of `f32` for the floating-point type [`real`][type@builtin::real]. Requires Godot to be compiled with the
80//! scons flag `precision=double`.<br><br>
81//!
82//! * **`experimental-godot-api`**
83//!
84//! Access to `godot::classes` APIs that Godot marks "experimental". These are under heavy development and may change at any time.
85//! If you opt in to this feature, expect breaking changes at compile and runtime.
86//!
87//! _Rust functionality toggles:_
88//!
89//! * **`lazy-function-tables`**
90//!
91//! Instead of loading all engine function pointers at startup, load them lazily on first use. This reduces startup time and RAM usage, but
92//! incurs additional overhead in each FFI call. Also, you lose the guarantee that once the library has booted, all function pointers are
93//! truly available. Function calls may thus panic only at runtime, possibly in deeply nested code paths.
94//! This feature is not yet thread-safe and can thus not be combined with `experimental-threads`.<br><br>
95//!
96//! * **`experimental-threads`**
97//!
98//! Experimental threading support. This adds synchronization to access the user instance in `Gd<T>` and disables several single-thread checks.
99//! The safety aspects are not ironed out yet; there is a high risk of unsoundness at the moment.
100//! As this evolves, it is very likely that the API becomes stricter.<br><br>
101//!
102//! * **`experimental-wasm`**
103//!
104//! Support for WebAssembly exports is still a work-in-progress and is not yet well tested. This feature is in place for users
105//! to explicitly opt in to any instabilities or rough edges that may result.
106//!
107//! By default, Wasm threads are enabled and require the flag `"-C", "link-args=-sUSE_PTHREADS=1"` in the `wasm32-unknown-unknown` target.
108//! This must be kept in sync with Godot's Web export settings (threading support enabled). To disable it, use **additionally* the feature
109//! `experimental-wasm-nothreads`.<br><br>
110//!
111//! * **`experimental-wasm-nothreads`**
112//!
113//! Requires the `experimental-wasm` feature. Disables threading support for WebAssembly exports. This needs to be kept in sync with
114//! Godot's Web export setting (threading support disabled), and must _not_ use the `"-C", "link-args=-sUSE_PTHREADS=1"` flag in the
115//! `wasm32-unknown-unknown` target.<br><br>
116//!
117//! * **`codegen-rustfmt`**
118//!
119//! Use rustfmt to format generated binding code. Because rustfmt is so slow, this is detrimental to initial compile time.
120//! Without it, we use a lightweight and fast custom formatter to enable basic human readability.<br><br>
121//!
122//! * **`register-docs`**
123//!
124//! Generates documentation for your structs from your Rust documentation.
125//! Documentation is visible in Godot via `F1` -> searching for that class.
126//! This feature requires at least Godot 4.3.
127//! See also: [`#[derive(GodotClass)]`](register/derive.GodotClass.html#documentation)
128//!
129//! _Integrations:_
130//!
131//! * **`serde`**
132//!
133//! Implement the [serde](https://serde.rs/) traits `Serialize` and `Deserialize` traits for certain built-in types.
134//! The serialized representation underlies **no stability guarantees** and may change at any time, even without a SemVer-breaking change.
135//!
136
137#![doc(
138 html_logo_url = "https://raw.githubusercontent.com/godot-rust/assets/master/gdext/ferris.svg"
139)]
140
141#[cfg(doc)]
142pub mod __docs;
143
144// ----------------------------------------------------------------------------------------------------------------------------------------------
145// Validations
146
147#[cfg(all(feature = "lazy-function-tables", feature = "experimental-threads"))]
148compile_error!("Thread safety for lazy function pointers is not yet implemented.");
149
150#[cfg(all(
151 feature = "experimental-wasm-nothreads",
152 feature = "experimental-threads"
153))]
154compile_error!("Cannot use 'experimental-threads' with a nothreads Wasm build yet.");
155
156#[cfg(all(target_family = "wasm", not(feature = "experimental-wasm")))]
157compile_error!("Must opt-in using `experimental-wasm` Cargo feature; keep in mind that this is work in progress");
158
159// See also https://github.com/godotengine/godot/issues/86346.
160#[cfg(all(feature = "double-precision", not(feature = "api-custom")))]
161compile_error!("The feature `double-precision` currently requires `api-custom` due to incompatibilities in the GDExtension API JSON.");
162
163// Note: #[cfg]s are not emitted in this crate, so move checks for those up to godot-core.
164
165// ----------------------------------------------------------------------------------------------------------------------------------------------
166// Modules
167
168#[doc(inline)]
169pub use godot_core::{builtin, classes, global, meta, obj, tools};
170
171#[doc(hidden)]
172pub use godot_core::possibly_docs as docs;
173
174#[doc(hidden)]
175pub use godot_core::sys;
176
177/// Entry point and global init/shutdown of the library.
178pub mod init {
179 pub use godot_core::init::*;
180
181 // Re-exports
182 pub use godot_macros::gdextension;
183}
184
185/// Register/export Rust symbols to Godot: classes, methods, enums...
186pub mod register {
187 pub use godot_core::registry::property;
188 pub use godot_macros::{godot_api, godot_dyn, Export, GodotClass, GodotConvert, Var};
189
190 #[cfg(feature = "__codegen-full")]
191 pub use godot_core::registry::RpcConfig;
192
193 /// Re-exports used by proc-macro API.
194 #[doc(hidden)]
195 pub mod private {
196 #[cfg(feature = "__codegen-full")]
197 pub use godot_core::registry::class::auto_register_rpcs;
198
199 pub use godot_core::registry::godot_register_wrappers::*;
200 pub use godot_core::registry::{constant, method};
201 }
202}
203
204/// Testing facilities (unstable).
205#[doc(hidden)]
206pub mod test {
207 pub use godot_macros::{bench, itest};
208}
209
210#[doc(hidden)]
211pub use godot_core::__deprecated;
212#[doc(hidden)]
213pub use godot_core::private;
214
215/// Often-imported symbols.
216pub mod prelude;