77
88use godot_ffi as sys;
99
10- use crate :: builtin:: meta:: { FromGodot , ToGodot } ;
10+ use crate :: builtin:: meta:: { impl_godot_as_self , FromGodot , ToGodot } ;
1111use crate :: builtin:: { inner, Variant , VariantArray } ;
1212use crate :: property:: { builtin_type_string, Export , PropertyHintInfo , TypeStringHint , Var } ;
1313use sys:: types:: OpaqueDictionary ;
@@ -16,8 +16,6 @@ use sys::{ffi_methods, interface_fn, GodotFfi};
1616use std:: marker:: PhantomData ;
1717use std:: { fmt, ptr} ;
1818
19- use super :: meta:: impl_godot_as_self;
20-
2119/// Godot's `Dictionary` type.
2220///
2321/// The keys and values of the dictionary are all `Variant`s, so they can be of different types.
@@ -211,48 +209,54 @@ impl Dictionary {
211209 self . as_inner ( ) . merge ( other, overwrite)
212210 }
213211
214- /// Returns a deep copy of the dictionary. All nested arrays and dictionaries are duplicated and
215- /// will not be shared with the original dictionary. Note that any `Object`-derived elements will
216- /// still be shallow copied.
212+ /// Deep copy, duplicating nested collections.
213+ ///
214+ /// All nested arrays and dictionaries are duplicated and will not be shared with the original dictionary.
215+ /// Note that any `Object`-derived elements will still be shallow copied.
217216 ///
218- /// To create a shallow copy, use [`Self::duplicate_shallow()`] instead.
217+ /// To create a shallow copy, use [`Self::duplicate_shallow()`] instead.
219218 /// To create a new reference to the same dictionary data, use [`clone()`][Clone::clone].
220219 ///
221220 /// _Godot equivalent: `dict.duplicate(true)`_
222221 pub fn duplicate_deep ( & self ) -> Self {
223222 self . as_inner ( ) . duplicate ( true )
224223 }
225224
226- /// Returns a shallow copy of the dictionary. All dictionary keys and values are copied, but
227- /// any reference types (such as `Array`, `Dictionary` and `Object`) will still refer to the
228- /// same value.
225+ /// Shallow copy, copying elements but sharing nested collections.
226+ ///
227+ /// All dictionary keys and values are copied, but any reference types (such as `Array`, `Dictionary` and `Gd<T>` objects)
228+ /// will still refer to the same value.
229229 ///
230- /// To create a deep copy, use [`Self::duplicate_deep()`] instead.
230+ /// To create a deep copy, use [`Self::duplicate_deep()`] instead.
231231 /// To create a new reference to the same dictionary data, use [`clone()`][Clone::clone].
232232 ///
233233 /// _Godot equivalent: `dict.duplicate(false)`_
234234 pub fn duplicate_shallow ( & self ) -> Self {
235235 self . as_inner ( ) . duplicate ( false )
236236 }
237237
238- /// Returns an iterator over the key-value pairs of the `Dictionary`. The pairs are each of type `(Variant, Variant)`.
239- /// Each pair references the original `Dictionary`, but instead of a `&`-reference to key-value pairs as
240- /// you might expect, the iterator returns a (cheap, shallow) copy of each key-value pair.
238+ /// Returns an iterator over the key-value pairs of the `Dictionary`.
241239 ///
242- /// Note that it's possible to modify the `Dictionary` through another reference while iterating
243- /// over it. This will not result in unsoundness or crashes, but will cause the iterator to
244- /// behave in an unspecified way.
240+ /// The pairs are each of type `(Variant, Variant)`. Each pair references the original `Dictionary`, but instead of a `&`-reference
241+ /// to key-value pairs as you might expect, the iterator returns a (cheap, shallow) copy of each key-value pair.
242+ ///
243+ /// Note that it's possible to modify the `Dictionary` through another reference while iterating over it. This will not result in
244+ /// unsoundness or crashes, but will cause the iterator to behave in an unspecified way.
245+ ///
246+ /// Use `iter_shared().typed::<K, V>()` to iterate over `(K, V)` pairs instead.
245247 pub fn iter_shared ( & self ) -> Iter < ' _ > {
246248 Iter :: new ( self )
247249 }
248250
249- /// Returns an iterator over the keys `Dictionary`. The keys are each of type `Variant`. Each key references
250- /// the original `Dictionary`, but instead of a `&`-reference to keys pairs as you might expect, the
251- /// iterator returns a (cheap, shallow) copy of each key pair.
251+ /// Returns an iterator over the keys in a `Dictionary`.
252+ ///
253+ /// The keys are each of type `Variant`. Each key references the original `Dictionary`, but instead of a `&`-reference to keys pairs
254+ /// as you might expect, the iterator returns a (cheap, shallow) copy of each key pair.
252255 ///
253- /// Note that it's possible to modify the `Dictionary` through another reference while iterating
254- /// over it. This will not result in unsoundness or crashes, but will cause the iterator to
255- /// behave in an unspecified way.
256+ /// Note that it's possible to modify the `Dictionary` through another reference while iterating over it. This will not result in
257+ /// unsoundness or crashes, but will cause the iterator to behave in an unspecified way.
258+ ///
259+ /// Use `.keys_shared.typed::<K>()` to iterate over `K` keys instead.
256260 pub fn keys_shared ( & self ) -> Keys < ' _ > {
257261 Keys :: new ( self )
258262 }
@@ -504,8 +508,8 @@ impl<'a> DictionaryIter<'a> {
504508 ptr:: addr_of_mut!( valid_u8) ,
505509 )
506510 } ;
507- let valid = super :: u8_to_bool ( valid_u8) ;
508- let has_next = super :: u8_to_bool ( has_next) ;
511+ let valid = u8_to_bool ( valid_u8) ;
512+ let has_next = u8_to_bool ( has_next) ;
509513
510514 if has_next {
511515 assert ! ( valid) ;
@@ -518,9 +522,9 @@ impl<'a> DictionaryIter<'a> {
518522
519523// ----------------------------------------------------------------------------------------------------------------------------------------------
520524
521- /// An iterator over key-value pairs from a `Dictionary`.
525+ /// Iterator over key-value pairs in a [ `Dictionary`] .
522526///
523- /// See [Dictionary::iter_shared()] for more information about iteration over dictionaries.
527+ /// See [` Dictionary::iter_shared()` ] for more information about iteration over dictionaries.
524528pub struct Iter < ' a > {
525529 iter : DictionaryIter < ' a > ,
526530}
@@ -553,9 +557,9 @@ impl<'a> Iterator for Iter<'a> {
553557
554558// ----------------------------------------------------------------------------------------------------------------------------------------------
555559
556- /// An iterator over keys from a `Dictionary`.
560+ /// Iterator over keys in a [ `Dictionary`] .
557561///
558- /// See [Dictionary::keys_shared()] for more information about iteration over dictionaries.
562+ /// See [` Dictionary::keys_shared()` ] for more information about iteration over dictionaries.
559563pub struct Keys < ' a > {
560564 iter : DictionaryIter < ' a > ,
561565}
@@ -595,10 +599,9 @@ impl<'a> Iterator for Keys<'a> {
595599
596600// ----------------------------------------------------------------------------------------------------------------------------------------------
597601
598- /// An iterator over key-value pairs from a `Dictionary` that will attempt to convert each
599- /// key-value pair into a typed `(K, V)`.
602+ /// [`Dictionary`] iterator that converts each key-value pair into a typed `(K, V)`.
600603///
601- /// See [Dictionary::iter_shared()] for more information about iteration over dictionaries.
604+ /// See [` Dictionary::iter_shared()` ] for more information about iteration over dictionaries.
602605pub struct TypedIter < ' a , K , V > {
603606 iter : DictionaryIter < ' a > ,
604607 _k : PhantomData < K > ,
@@ -631,9 +634,9 @@ impl<'a, K: FromGodot, V: FromGodot> Iterator for TypedIter<'a, K, V> {
631634
632635// ----------------------------------------------------------------------------------------------------------------------------------------------
633636
634- /// An iterator over keys from a `Dictionary` that will attempt to convert each key into a `K`.
637+ /// [ `Dictionary`] iterator that converts each key into a typed `K`.
635638///
636- /// See [Dictionary::iter_shared()] for more information about iteration over dictionaries.
639+ /// See [` Dictionary::iter_shared()` ] for more information about iteration over dictionaries.
637640pub struct TypedKeys < ' a , K > {
638641 iter : DictionaryIter < ' a > ,
639642 _k : PhantomData < K > ,
@@ -660,6 +663,17 @@ impl<'a, K: FromGodot> Iterator for TypedKeys<'a, K> {
660663 }
661664}
662665
666+ // ----------------------------------------------------------------------------------------------------------------------------------------------
667+ // Helper functions
668+
669+ fn u8_to_bool ( u : u8 ) -> bool {
670+ match u {
671+ 0 => false ,
672+ 1 => true ,
673+ _ => panic ! ( "Invalid boolean value {u}" ) ,
674+ }
675+ }
676+
663677// ----------------------------------------------------------------------------------------------------------------------------------------------
664678
665679/// Constructs [`Dictionary`] literals, close to Godot's own syntax.
0 commit comments