Skip to content

Commit 3c9ad03

Browse files
bors[bot]Bromeon
andauthored
Merge #625
625: Document GodotString and its trade-offs vs. standard String r=toasteater a=Bromeon Adds documentation about `GodotString` types and highlights when it should be preferred over standard `String`, and when not. Please correct me if some information is not accurate or other one is missing. Two points where I wasn't sure: 1. is the immutability really given, and will stay like this, or did I just miss mutating methods in the doc? 2. what character encoding does `GodotString` use? Co-authored-by: Jan Haller <[email protected]>
2 parents cd59838 + 9bb37ea commit 3c9ad03

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

gdnative-core/src/core_types/string.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,39 @@ use std::slice;
1111
use std::str;
1212

1313
/// 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)
1447
pub struct GodotString(pub(crate) sys::godot_string);
1548

1649
macro_rules! impl_methods {

0 commit comments

Comments
 (0)