@@ -12,9 +12,7 @@ use super::Array;
12
12
/// Godot's `Dictionary` type.
13
13
///
14
14
/// The keys and values of the array are all `Variant`, so they can all be of different types.
15
- ///
16
- /// Keys are assumed to be cheap to clone, this will usually be the case especially for
17
- /// value types and reference counted types.
15
+ /// Variants are designed to be generally cheap to clone.
18
16
///
19
17
/// # Thread safety
20
18
///
@@ -34,7 +32,7 @@ impl Dictionary {
34
32
Self :: default ( )
35
33
}
36
34
37
- /// Removes all key-value pairs from the dictionary. Equivalent to `clear` in godot .
35
+ /// Removes all key-value pairs from the dictionary. Equivalent to `clear` in Godot .
38
36
pub fn clear ( & mut self ) {
39
37
self . as_inner ( ) . clear ( )
40
38
}
@@ -46,7 +44,7 @@ impl Dictionary {
46
44
/// To create a shallow copy, use [`duplicate_shallow()`] instead. To create a new reference to
47
45
/// the same array data, use [`share()`].
48
46
///
49
- /// Equivalent to `dictionary.duplicate(true)` in godot .
47
+ /// Equivalent to `dictionary.duplicate(true)` in Godot .
50
48
pub fn duplicate_deep ( & self ) -> Self {
51
49
self . as_inner ( ) . duplicate ( true )
52
50
}
@@ -58,14 +56,14 @@ impl Dictionary {
58
56
/// To create a deep copy, use [`duplicate_deep()`] instead. To create a new reference to the
59
57
/// same dictionary data, use [`share()`].
60
58
///
61
- /// Equivalent to `dictionary.duplicate(false)` in godot .
59
+ /// Equivalent to `dictionary.duplicate(false)` in Godot .
62
60
pub fn duplicate_shallow ( & self ) -> Self {
63
61
self . as_inner ( ) . duplicate ( false )
64
62
}
65
63
66
64
/// Removes a key from the map, and returns the value associated with
67
65
/// the key if the key was in the dictionary.
68
- pub fn remove ( & mut self , key : impl ToVariant ) -> Option < Variant > {
66
+ pub fn remove < K : ToVariant > ( & mut self , key : K ) -> Option < Variant > {
69
67
let key = key. to_variant ( ) ;
70
68
let old_value = self . get ( key. clone ( ) ) ;
71
69
self . as_inner ( ) . erase ( key) ;
@@ -74,9 +72,9 @@ impl Dictionary {
74
72
75
73
/// Returns the first key whose associated value is `value`, if one exists.
76
74
///
77
- /// Unlike in godot , this will return `None` if the key does not exist
75
+ /// Unlike in Godot , this will return `None` if the key does not exist
78
76
/// and `Some(nil)` the key is `null`.
79
- pub fn find_key_by_value ( & self , value : impl ToVariant ) -> Option < Variant > {
77
+ pub fn find_key_by_value < V : ToVariant > ( & self , value : V ) -> Option < Variant > {
80
78
let key = self . as_inner ( ) . find_key ( value. to_variant ( ) ) ;
81
79
82
80
if !key. is_nil ( ) || self . contains_key ( key. clone ( ) ) {
@@ -89,9 +87,9 @@ impl Dictionary {
89
87
/// Returns the value at the key in the dictionary, if there is
90
88
/// one.
91
89
///
92
- /// Unlike `get` in godot , this will return `None` if there is
90
+ /// Unlike `get` in Godot , this will return `None` if there is
93
91
/// no value with the given key.
94
- pub fn get ( & self , key : impl ToVariant ) -> Option < Variant > {
92
+ pub fn get < K : ToVariant > ( & self , key : K ) -> Option < Variant > {
95
93
let key = key. to_variant ( ) ;
96
94
if !self . contains_key ( key. clone ( ) ) {
97
95
return None ;
@@ -104,22 +102,22 @@ impl Dictionary {
104
102
/// method does not let you differentiate `NIL` values stored as values from
105
103
/// absent keys. If you need that, use `get()`.
106
104
///
107
- /// This is equivalent to `get` in godot .
108
- pub fn get_or_nil ( & self , key : impl ToVariant ) -> Variant {
105
+ /// This is equivalent to `get` in Godot .
106
+ pub fn get_or_nil < K : ToVariant > ( & self , key : K ) -> Variant {
109
107
self . as_inner ( ) . get ( key. to_variant ( ) , Variant :: nil ( ) )
110
108
}
111
109
112
110
/// Returns `true` if the dictionary contains the given key.
113
111
///
114
- /// This is equivalent to `has` in godot .
115
- pub fn contains_key ( & self , key : impl ToVariant ) -> bool {
112
+ /// This is equivalent to `has` in Godot .
113
+ pub fn contains_key < K : ToVariant > ( & self , key : K ) -> bool {
116
114
let key = key. to_variant ( ) ;
117
115
self . as_inner ( ) . has ( key)
118
116
}
119
117
120
118
/// Returns `true` if the dictionary contains all the given keys.
121
119
///
122
- /// This is equivalent to `has_all` in godot .
120
+ /// This is equivalent to `has_all` in Godot .
123
121
pub fn contains_all_keys ( & self , keys : Array ) -> bool {
124
122
self . as_inner ( ) . has_all ( keys)
125
123
}
@@ -149,56 +147,56 @@ impl Dictionary {
149
147
/// If overwrite is true, it will overwrite pre-existing keys. Otherwise
150
148
/// it will not.
151
149
///
152
- /// This is equivalent to `merge` in godot .
150
+ /// This is equivalent to `merge` in Godot .
153
151
pub fn extend_dictionary ( & mut self , other : Self , overwrite : bool ) {
154
152
self . as_inner ( ) . merge ( other, overwrite)
155
153
}
156
154
157
155
/// Returns the number of entries in the dictionary.
158
156
///
159
- /// This is equivalent to `size` in godot .
157
+ /// This is equivalent to `size` in Godot .
160
158
pub fn len ( & self ) -> usize {
161
159
self . as_inner ( ) . size ( ) . try_into ( ) . unwrap ( )
162
160
}
163
161
164
162
/// Get the pointer corresponding to the given key in the dictionary,
165
163
/// this pointer is null if there is no value at the given key.
166
164
#[ allow( dead_code) ] // TODO: remove function if it turns out i'll never actually get any use out of it
167
- fn get_ptr ( & self , key : impl ToVariant ) -> * const Variant {
165
+ fn get_ptr < K : ToVariant > ( & self , key : K ) -> * const Variant {
168
166
let key = key. to_variant ( ) ;
169
167
unsafe {
170
- ( interface_fn ! ( dictionary_operator_index_const) ) ( self . sys_const ( ) , key. var_sys_const ( ) )
171
- as * const Variant
168
+ let ptr = ( interface_fn ! ( dictionary_operator_index_const) ) (
169
+ self . sys_const ( ) ,
170
+ key. var_sys_const ( ) ,
171
+ ) ;
172
+ ptr as * const Variant
172
173
}
173
174
}
174
175
175
176
/// Get the pointer corresponding to the given key in the dictionary,
176
177
/// if there exists no value at the given key then a new one is created
177
178
/// and initialized to a nil variant.
178
- fn get_ptr_mut ( & mut self , key : impl ToVariant ) -> * mut Variant {
179
+ fn get_ptr_mut < K : ToVariant > ( & mut self , key : K ) -> * mut Variant {
179
180
let key = key. to_variant ( ) ;
180
181
unsafe {
181
182
let ptr =
182
- ( interface_fn ! ( dictionary_operator_index) ) ( self . sys_mut ( ) , key. var_sys_const ( ) )
183
- as * mut Variant ;
184
- // dictionary_operator_index initializes the value so it wont be null
183
+ ( interface_fn ! ( dictionary_operator_index) ) ( self . sys_mut ( ) , key. var_sys_const ( ) ) ;
185
184
assert ! ( !ptr. is_null( ) ) ;
186
- // i think it might be safe to turn this into a &mut Variant?
187
- ptr
185
+ ptr as * mut Variant
188
186
}
189
187
}
190
188
191
189
/// Insert a value at the given key, returning the value
192
190
/// that previously was at that key if there was one.
193
- pub fn insert ( & mut self , key : impl ToVariant , value : impl ToVariant ) -> Option < Variant > {
191
+ pub fn insert < K : ToVariant , V : ToVariant > ( & mut self , key : K , value : V ) -> Option < Variant > {
194
192
let key = key. to_variant ( ) ;
195
193
let old_value = self . get ( key. clone ( ) ) ;
196
194
self . set ( key, value) ;
197
195
old_value
198
196
}
199
197
200
- /// Set a key to a given value
201
- pub fn set ( & mut self , key : impl ToVariant , value : impl ToVariant ) {
198
+ /// Set a key to a given value.
199
+ pub fn set < K : ToVariant , V : ToVariant > ( & mut self , key : K , value : V ) {
202
200
let key = key. to_variant ( ) ;
203
201
unsafe {
204
202
* self . get_ptr_mut ( key) = value. to_variant ( ) ;
@@ -211,16 +209,19 @@ impl Dictionary {
211
209
}
212
210
}
213
211
214
- /// Creates a `Dictionary` from the given `T `. Each key and value are
212
+ /// Creates a `Dictionary` from the given `I `. Each key and value are
215
213
/// converted to a `Variant`.
216
- impl < ' a , ' b , K , V , T > From < T > for Dictionary
214
+ impl < ' a , ' b , K , V , I > From < I > for Dictionary
217
215
where
218
- T : IntoIterator < Item = ( & ' a K , & ' b V ) > ,
216
+ I : IntoIterator < Item = ( & ' a K , & ' b V ) > ,
219
217
K : ToVariant + ' a ,
220
218
V : ToVariant + ' b ,
221
219
{
222
- fn from ( iterable : T ) -> Self {
223
- iterable. into_iter ( ) . map ( |( key, value) | ( key. to_variant ( ) , value. to_variant ( ) ) ) . collect ( )
220
+ fn from ( iterable : I ) -> Self {
221
+ iterable
222
+ . into_iter ( )
223
+ . map ( |( key, value) | ( key. to_variant ( ) , value. to_variant ( ) ) )
224
+ . collect ( )
224
225
}
225
226
}
226
227
@@ -263,19 +264,17 @@ impl<K: FromVariant + Eq + std::hash::Hash> TryFrom<&Dictionary> for HashSet<K>
263
264
/// replacing values with existing keys with new values returned
264
265
/// from the iterator.
265
266
impl < K : ToVariant , V : ToVariant > Extend < ( K , V ) > for Dictionary {
266
- fn extend < T : IntoIterator < Item = ( K , V ) > > ( & mut self , iter : T ) {
267
+ fn extend < I : IntoIterator < Item = ( K , V ) > > ( & mut self , iter : I ) {
267
268
for ( k, v) in iter. into_iter ( ) {
268
269
self . set ( k. to_variant ( ) , v. to_variant ( ) )
269
270
}
270
271
}
271
272
}
272
273
273
274
impl < K : ToVariant , V : ToVariant > FromIterator < ( K , V ) > for Dictionary {
274
- fn from_iter < T : IntoIterator < Item = ( K , V ) > > ( iter : T ) -> Self {
275
+ fn from_iter < I : IntoIterator < Item = ( K , V ) > > ( iter : I ) -> Self {
275
276
let mut dict = Dictionary :: new ( ) ;
276
- for ( k, v) in iter. into_iter ( ) {
277
- dict. set ( k. to_variant ( ) , v. to_variant ( ) )
278
- }
277
+ dict. extend ( iter) ;
279
278
dict
280
279
}
281
280
}
@@ -316,13 +315,13 @@ impl Share for Dictionary {
316
315
}
317
316
318
317
/// Creates a new dictionary with the given keys and values, the syntax mirrors
319
- /// godot 's dictionary creation syntax.
318
+ /// Godot 's dictionary creation syntax.
320
319
///
321
320
/// Any value can be used as a key, but to use an expression you need to surround it
322
- /// in `()` or `{}`
321
+ /// in `()` or `{}`.
323
322
///
324
323
/// Example
325
- /// ```rust, no_run
324
+ /// ```no_run
326
325
/// # #[macro_use] extern crate godot_core;
327
326
/// # fn main() {
328
327
/// let key = "my_key";
@@ -340,9 +339,9 @@ macro_rules! dict {
340
339
{
341
340
let mut d = $crate:: builtin:: Dictionary :: new( ) ;
342
341
$(
343
- // otherwise `(1 + 2): true` would complain even though you can't write
344
- // 1 + 2: true
345
- #[ allow( unused_parens) ]
342
+ // Check complains that `(1 + 2): true` has unused parens, even though it's not
343
+ // possible to omit the parens.
344
+ #[ allow( unused_parens) ]
346
345
d. set( $key, $value) ;
347
346
) *
348
347
d
0 commit comments