7
7
8
8
use godot_ffi as sys;
9
9
10
- use crate :: builtin:: meta:: { FromGodot , ToGodot } ;
10
+ use crate :: builtin:: meta:: { impl_godot_as_self , FromGodot , ToGodot } ;
11
11
use crate :: builtin:: { inner, Variant , VariantArray } ;
12
12
use crate :: property:: { builtin_type_string, Export , PropertyHintInfo , TypeStringHint , Var } ;
13
13
use sys:: types:: OpaqueDictionary ;
@@ -16,8 +16,6 @@ use sys::{ffi_methods, interface_fn, GodotFfi};
16
16
use std:: marker:: PhantomData ;
17
17
use std:: { fmt, ptr} ;
18
18
19
- use super :: meta:: impl_godot_as_self;
20
-
21
19
/// Godot's `Dictionary` type.
22
20
///
23
21
/// The keys and values of the dictionary are all `Variant`s, so they can be of different types.
@@ -211,48 +209,54 @@ impl Dictionary {
211
209
self . as_inner ( ) . merge ( other, overwrite)
212
210
}
213
211
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.
217
216
///
218
- /// To create a shallow copy, use [`Self::duplicate_shallow()`] instead.
217
+ /// To create a shallow copy, use [`Self::duplicate_shallow()`] instead.
219
218
/// To create a new reference to the same dictionary data, use [`clone()`][Clone::clone].
220
219
///
221
220
/// _Godot equivalent: `dict.duplicate(true)`_
222
221
pub fn duplicate_deep ( & self ) -> Self {
223
222
self . as_inner ( ) . duplicate ( true )
224
223
}
225
224
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.
229
229
///
230
- /// To create a deep copy, use [`Self::duplicate_deep()`] instead.
230
+ /// To create a deep copy, use [`Self::duplicate_deep()`] instead.
231
231
/// To create a new reference to the same dictionary data, use [`clone()`][Clone::clone].
232
232
///
233
233
/// _Godot equivalent: `dict.duplicate(false)`_
234
234
pub fn duplicate_shallow ( & self ) -> Self {
235
235
self . as_inner ( ) . duplicate ( false )
236
236
}
237
237
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`.
241
239
///
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.
245
247
pub fn iter_shared ( & self ) -> Iter < ' _ > {
246
248
Iter :: new ( self )
247
249
}
248
250
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.
252
255
///
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.
256
260
pub fn keys_shared ( & self ) -> Keys < ' _ > {
257
261
Keys :: new ( self )
258
262
}
@@ -504,8 +508,8 @@ impl<'a> DictionaryIter<'a> {
504
508
ptr:: addr_of_mut!( valid_u8) ,
505
509
)
506
510
} ;
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) ;
509
513
510
514
if has_next {
511
515
assert ! ( valid) ;
@@ -518,9 +522,9 @@ impl<'a> DictionaryIter<'a> {
518
522
519
523
// ----------------------------------------------------------------------------------------------------------------------------------------------
520
524
521
- /// An iterator over key-value pairs from a `Dictionary`.
525
+ /// Iterator over key-value pairs in a [ `Dictionary`] .
522
526
///
523
- /// See [Dictionary::iter_shared()] for more information about iteration over dictionaries.
527
+ /// See [` Dictionary::iter_shared()` ] for more information about iteration over dictionaries.
524
528
pub struct Iter < ' a > {
525
529
iter : DictionaryIter < ' a > ,
526
530
}
@@ -553,9 +557,9 @@ impl<'a> Iterator for Iter<'a> {
553
557
554
558
// ----------------------------------------------------------------------------------------------------------------------------------------------
555
559
556
- /// An iterator over keys from a `Dictionary`.
560
+ /// Iterator over keys in a [ `Dictionary`] .
557
561
///
558
- /// See [Dictionary::keys_shared()] for more information about iteration over dictionaries.
562
+ /// See [` Dictionary::keys_shared()` ] for more information about iteration over dictionaries.
559
563
pub struct Keys < ' a > {
560
564
iter : DictionaryIter < ' a > ,
561
565
}
@@ -595,10 +599,9 @@ impl<'a> Iterator for Keys<'a> {
595
599
596
600
// ----------------------------------------------------------------------------------------------------------------------------------------------
597
601
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)`.
600
603
///
601
- /// See [Dictionary::iter_shared()] for more information about iteration over dictionaries.
604
+ /// See [` Dictionary::iter_shared()` ] for more information about iteration over dictionaries.
602
605
pub struct TypedIter < ' a , K , V > {
603
606
iter : DictionaryIter < ' a > ,
604
607
_k : PhantomData < K > ,
@@ -631,9 +634,9 @@ impl<'a, K: FromGodot, V: FromGodot> Iterator for TypedIter<'a, K, V> {
631
634
632
635
// ----------------------------------------------------------------------------------------------------------------------------------------------
633
636
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`.
635
638
///
636
- /// See [Dictionary::iter_shared()] for more information about iteration over dictionaries.
639
+ /// See [` Dictionary::iter_shared()` ] for more information about iteration over dictionaries.
637
640
pub struct TypedKeys < ' a , K > {
638
641
iter : DictionaryIter < ' a > ,
639
642
_k : PhantomData < K > ,
@@ -660,6 +663,17 @@ impl<'a, K: FromGodot> Iterator for TypedKeys<'a, K> {
660
663
}
661
664
}
662
665
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
+
663
677
// ----------------------------------------------------------------------------------------------------------------------------------------------
664
678
665
679
/// Constructs [`Dictionary`] literals, close to Godot's own syntax.
0 commit comments