@@ -11,6 +11,39 @@ use std::slice;
11
11
use std:: str;
12
12
13
13
/// Godot's reference-counted string type.
14
+ ///
15
+ /// This is the Rust binding of GDScript's `String` type. It represents the native string class
16
+ /// used within the Godot engine, and as such has different memory layout and characteristics than
17
+ /// `std::string::String`.
18
+ ///
19
+ /// `GodotString` is reference-counted like most types in godot-rust. Thus, its `clone()` method
20
+ /// does not return a copy of the string, but simply another instance which shares the same backing
21
+ /// string. Furthermore, `GodotString` is immutable and does not offer any write APIs. If you need
22
+ /// to modify Godot strings, convert them to Rust strings, perform the modifications and convert back.
23
+ /// In GDScript, strings have copy-on-write semantics, which guarantees that `GodotString` instances
24
+ /// in Rust are independent of their GDScript counterparts. A modification of a string in GDScript
25
+ /// (which was previously passed to Rust) will not be reflected in Rust.
26
+ ///
27
+ /// When interfacing with the Godot engine API through godot-rust, you often have the choice between
28
+ /// `std::string::String` and `gdnative::core_types::GodotString`. In user methods that are exposed to
29
+ /// Godot through the `#[export]` macro, both types can be used as parameters and return types, and any
30
+ /// conversions are done transparently.
31
+ /// For auto-generated binding APIs in `gdnative::api`, return types are `GodotString`, but parameters
32
+ /// are declared `impl Into<GodotString>`, allowing `String` or `&str` to be passed. In addition, the
33
+ /// two types can always be explicitly converted using `GodotString::from_str()` and
34
+ /// `GodotString::display/to_string()`.
35
+ ///
36
+ /// As a general guideline, use `GodotString` if:
37
+ /// * your strings are very large, so you can avoid copying them
38
+ /// * you need specific operations only available in Godot (e.g. `sha256_text()`, `c_escape()`, ...)
39
+ /// * you primarily pass them between different Godot APIs, without string processing in user code
40
+ ///
41
+ /// Use Rust's `String` if:
42
+ /// * you need to modify the string
43
+ /// * you would like to decouple part of your code from Godot (e.g. independent game logic, standalone tests)
44
+ /// * you want a standard type for interoperability with third-party code (e.g. `regex` crate)
45
+ /// * you have a large number of method calls per string instance (which are more expensive due to indirectly calling into Godot)
46
+ /// * you need UTF-8 encoding (`GodotString`'s encoding is platform-dependent and unspecified)
14
47
pub struct GodotString ( pub ( crate ) sys:: godot_string ) ;
15
48
16
49
macro_rules! impl_methods {
0 commit comments