Skip to content

Commit 359ec1b

Browse files
committed
Usage docs for Array + Dictionary
1 parent 918fbbf commit 359ec1b

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

godot-core/src/builtin/collections/array.rs

+54
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,60 @@ use sys::{ffi_methods, interface_fn, GodotFfi};
4545
/// If you want to create a copy of the data, use [`duplicate_shallow()`][Self::duplicate_shallow]
4646
/// or [`duplicate_deep()`][Self::duplicate_deep].
4747
///
48+
/// # Typed array example
49+
///
50+
/// ```no_run
51+
/// # use godot::prelude::*;
52+
/// // Create typed Array<i64> and add values.
53+
/// let mut array = Array::new();
54+
/// array.push(10);
55+
/// array.push(20);
56+
/// array.push(30);
57+
///
58+
/// // Or create the same array in a single expression.
59+
/// let array = array![10, 20, 30];
60+
///
61+
/// // Access elements.
62+
/// let value: i64 = array.at(0); // 10
63+
/// let maybe: Option<i64> = array.get(3); // None
64+
///
65+
/// // Iterate over i64 elements.
66+
/// for value in array.iter_shared() {
67+
/// println!("{value}");
68+
/// }
69+
///
70+
/// // Clone array (shares the reference), and overwrite elements through clone.
71+
/// let mut cloned = array.clone();
72+
/// cloned.set(0, 50); // [50, 20, 30]
73+
/// cloned.remove(1); // [50, 30]
74+
/// cloned.pop(); // [50]
75+
///
76+
/// // Changes will be reflected in the original array.
77+
/// assert_eq!(array.len(), 1);
78+
/// assert_eq!(array.front(), Some(50));
79+
/// ```
80+
///
81+
/// # Untyped array example
82+
///
83+
/// ```no_run
84+
/// # use godot::prelude::*;
85+
/// // VariantArray allows dynamic element types.
86+
/// let mut array = VariantArray::new();
87+
/// array.push(10.to_variant());
88+
/// array.push("Hello".to_variant());
89+
///
90+
/// // Or equivalent, use the `varray!` macro which converts each element.
91+
/// let array = varray![10, "Hello"];
92+
///
93+
/// // Access elements.
94+
/// let value: Variant = array.at(0);
95+
/// let value: i64 = array.at(0).to(); // Variant::to() extracts i64.
96+
/// let maybe: Result<i64, _> = array.at(1).try_to(); // "Hello" is not i64 -> Err.
97+
/// let maybe: Option<Variant> = array.get(3);
98+
///
99+
/// // ...and so on.
100+
/// ```
101+
///
48102
/// # Thread safety
49103
///
50104
/// Usage is safe if the `Array` is used on a single thread only. Concurrent reads on

godot-core/src/builtin/collections/dictionary.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,53 @@ use std::{fmt, ptr};
2323
/// The keys and values of the dictionary are all `Variant`s, so they can be of different types.
2424
/// Variants are designed to be generally cheap to clone.
2525
///
26+
/// # Dictionary example
27+
///
28+
/// ```no_run
29+
/// # use godot::prelude::*;
30+
/// // Create empty dictionary and add key-values pairs.
31+
/// let mut dict = Dictionary::new();
32+
/// dict.set("str", "Hello");
33+
/// dict.set("num", 23);
34+
///
35+
/// // Keys don't need to be strings.
36+
/// let coord = Vector2i::new(0, 1);
37+
/// dict.set(coord, "Tile77");
38+
///
39+
/// // Or create the same dictionary in a single expression.
40+
/// let dict = dict! {
41+
/// "str": "Hello",
42+
/// "num": 23,
43+
/// coord: "Tile77",
44+
/// };
45+
///
46+
/// // Access elements.
47+
/// let value: Variant = dict.at("str");
48+
/// let value: GString = dict.at("str").to(); // Variant::to() extracts GString.
49+
/// let maybe: Option<Variant> = dict.get("absent_key");
50+
///
51+
/// // Iterate over key-value pairs as (Variant, Variant).
52+
/// for (key, value) in dict.iter_shared() {
53+
/// println!("{key} => {value}");
54+
/// }
55+
///
56+
/// // Use typed::<K, V>() to get typed iterators.
57+
/// for (key, value) in dict.iter_shared().typed::<GString, Variant>() {
58+
/// println!("{key} => {value}");
59+
/// }
60+
///
61+
/// // Clone dictionary (shares the reference), and overwrite elements through clone.
62+
/// let mut cloned = dict.clone();
63+
/// cloned.remove("num");
64+
///
65+
/// // Overwrite with set(); use insert() to get the previous value.
66+
/// let prev = cloned.insert("str", "Goodbye"); // prev == Some("Hello")
67+
///
68+
/// // Changes will be reflected in the original dictionary.
69+
/// assert_eq!(dict.at("str"), "Goodbye".to_variant());
70+
/// assert_eq!(dict.get("num"), None);
71+
/// ```
72+
///
2673
/// # Thread safety
2774
///
2875
/// The same principles apply as for [`VariantArray`]. Consult its documentation for details.
@@ -246,7 +293,7 @@ impl Dictionary {
246293
/// Note that it's possible to modify the `Dictionary` through another reference while iterating over it. This will not result in
247294
/// unsoundness or crashes, but will cause the iterator to behave in an unspecified way.
248295
///
249-
/// Use `iter_shared().typed::<K, V>()` to iterate over `(K, V)` pairs instead.
296+
/// Use `dict.iter_shared().typed::<K, V>()` to iterate over `(K, V)` pairs instead.
250297
pub fn iter_shared(&self) -> Iter<'_> {
251298
Iter::new(self)
252299
}
@@ -259,7 +306,7 @@ impl Dictionary {
259306
/// Note that it's possible to modify the `Dictionary` through another reference while iterating over it. This will not result in
260307
/// unsoundness or crashes, but will cause the iterator to behave in an unspecified way.
261308
///
262-
/// Use `.keys_shared.typed::<K>()` to iterate over `K` keys instead.
309+
/// Use `dict.keys_shared().typed::<K>()` to iterate over `K` keys instead.
263310
pub fn keys_shared(&self) -> Keys<'_> {
264311
Keys::new(self)
265312
}

godot-core/src/builtin/quaternion.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use crate::builtin::math::{ApproxEq, FloatExt, GlamConv, GlamType};
1212
use crate::builtin::{inner, real, Basis, EulerOrder, RQuat, RealConv, Vector3};
1313
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
1414

15+
/// Unit quaternion to represent 3D rotations.
16+
///
17+
/// See also [`Quaternion`](https://docs.godotengine.org/en/stable/classes/class_quaternion.html) in the Godot documentation.
1518
#[derive(Copy, Clone, PartialEq, Debug)]
1619
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1720
#[repr(C)]

0 commit comments

Comments
 (0)