@@ -51,8 +51,8 @@ impl Dictionary {
51
51
self . as_inner ( ) . duplicate ( true )
52
52
}
53
53
54
- /// Returns a shallow copy of the dictionary. All dictionary keys and values are copied, but
55
- /// any reference types (such as `Array`, `Dictionary` and `Object`) will still refer to the
54
+ /// Returns a shallow copy of the dictionary. All dictionary keys and values are copied, but
55
+ /// any reference types (such as `Array`, `Dictionary` and `Object`) will still refer to the
56
56
/// same value.
57
57
///
58
58
/// To create a deep copy, use [`duplicate_deep()`] instead. To create a new reference to the
@@ -65,7 +65,7 @@ impl Dictionary {
65
65
66
66
/// Removes a key from the map, and returns the value associated with
67
67
/// the key if the key was in the dictionary.
68
- pub fn remove < K : ToVariant > ( & mut self , key : K ) -> Option < Variant > {
68
+ pub fn remove ( & mut self , key : impl ToVariant ) -> Option < Variant > {
69
69
let key = key. to_variant ( ) ;
70
70
let old_value = self . get ( key. clone ( ) ) ;
71
71
self . as_inner ( ) . erase ( key) ;
@@ -76,8 +76,8 @@ impl Dictionary {
76
76
///
77
77
/// Unlike in godot, this will return `None` if the key does not exist
78
78
/// and `Some(nil)` the key is `null`.
79
- pub fn find_key_by_value ( & self , value : Variant ) -> Option < Variant > {
80
- let key = self . as_inner ( ) . find_key ( value) ;
79
+ pub fn find_key_by_value ( & self , value : impl ToVariant ) -> Option < Variant > {
80
+ let key = self . as_inner ( ) . find_key ( value. to_variant ( ) ) ;
81
81
82
82
if !key. is_nil ( ) || self . contains_key ( key. clone ( ) ) {
83
83
Some ( key)
@@ -91,7 +91,7 @@ impl Dictionary {
91
91
///
92
92
/// Unlike `get` in godot, this will return `None` if there is
93
93
/// no value with the given key.
94
- pub fn get < K : ToVariant > ( & self , key : K ) -> Option < Variant > {
94
+ pub fn get ( & self , key : impl ToVariant ) -> Option < Variant > {
95
95
let key = key. to_variant ( ) ;
96
96
if !self . contains_key ( key. clone ( ) ) {
97
97
return None ;
@@ -100,19 +100,19 @@ impl Dictionary {
100
100
Some ( self . get_or_nil ( key) )
101
101
}
102
102
103
- /// Returns the value at the key in the dictionary, or nil otherwise. This
104
- /// method does not let you differentiate `NIL` values stored as values from
103
+ /// Returns the value at the key in the dictionary, or nil otherwise. This
104
+ /// method does not let you differentiate `NIL` values stored as values from
105
105
/// absent keys. If you need that, use `get()`.
106
106
///
107
107
/// This is equivalent to `get` in godot.
108
- pub fn get_or_nil < K : ToVariant > ( & self , key : K ) -> Variant {
108
+ pub fn get_or_nil ( & self , key : impl ToVariant ) -> Variant {
109
109
self . as_inner ( ) . get ( key. to_variant ( ) , Variant :: nil ( ) )
110
110
}
111
111
112
112
/// Returns `true` if the dictionary contains the given key.
113
113
///
114
114
/// This is equivalent to `has` in godot.
115
- pub fn contains_key < K : ToVariant > ( & self , key : K ) -> bool {
115
+ pub fn contains_key ( & self , key : impl ToVariant ) -> bool {
116
116
let key = key. to_variant ( ) ;
117
117
self . as_inner ( ) . has ( key)
118
118
}
@@ -164,7 +164,7 @@ impl Dictionary {
164
164
/// Get the pointer corresponding to the given key in the dictionary,
165
165
/// this pointer is null if there is no value at the given key.
166
166
#[ allow( dead_code) ] // TODO: remove function if it turns out i'll never actually get any use out of it
167
- fn get_ptr < K : ToVariant > ( & self , key : K ) -> * const Variant {
167
+ fn get_ptr ( & self , key : impl ToVariant ) -> * const Variant {
168
168
let key = key. to_variant ( ) ;
169
169
unsafe {
170
170
( interface_fn ! ( dictionary_operator_index_const) ) ( self . sys_const ( ) , key. var_sys_const ( ) )
@@ -175,7 +175,7 @@ impl Dictionary {
175
175
/// Get the pointer corresponding to the given key in the dictionary,
176
176
/// if there exists no value at the given key then a new one is created
177
177
/// and initialized to a nil variant.
178
- fn get_ptr_mut < K : ToVariant > ( & mut self , key : K ) -> * mut Variant {
178
+ fn get_ptr_mut ( & mut self , key : impl ToVariant ) -> * mut Variant {
179
179
let key = key. to_variant ( ) ;
180
180
unsafe {
181
181
let ptr =
@@ -190,41 +190,21 @@ impl Dictionary {
190
190
191
191
/// Insert a value at the given key, returning the value
192
192
/// that previously was at that key if there was one.
193
- pub fn insert < K : ToVariant > ( & mut self , key : K , value : Variant ) -> Option < Variant > {
193
+ pub fn insert ( & mut self , key : impl ToVariant , value : impl ToVariant ) -> Option < Variant > {
194
194
let key = key. to_variant ( ) ;
195
195
let old_value = self . get ( key. clone ( ) ) ;
196
196
self . set ( key, value) ;
197
197
old_value
198
198
}
199
199
200
200
/// Set a key to a given value
201
- pub fn set < K : ToVariant > ( & mut self , key : K , value : Variant ) {
201
+ pub fn set ( & mut self , key : impl ToVariant , value : impl ToVariant ) {
202
202
let key = key. to_variant ( ) ;
203
203
unsafe {
204
- * self . get_ptr_mut ( key) = value;
204
+ * self . get_ptr_mut ( key) = value. to_variant ( ) ;
205
205
}
206
206
}
207
207
208
- /// Convert this dictionary to a strongly typed rust `HashMap`. If the conversion
209
- /// fails for any key or value, an error is returned.
210
- pub fn try_to_hashmap < K : FromVariant + Eq + std:: hash:: Hash , V : FromVariant > (
211
- & self ,
212
- ) -> Result < HashMap < K , V > , VariantConversionError > {
213
- let mut map = HashMap :: new ( ) ;
214
- for key in self . keys ( ) . into_iter ( ) {
215
- map. insert ( key. try_to ( ) ?, self . get ( key) . unwrap ( ) . try_to ( ) ?) ;
216
- }
217
- Ok ( map)
218
- }
219
-
220
- /// Convert the keys of this dictionary to a strongly typed rust `HashSet`. If the
221
- /// conversion fails for any key, an error is returned.
222
- pub fn try_to_hashset < K : FromVariant + Eq + std:: hash:: Hash > (
223
- & self ,
224
- ) -> Result < HashSet < K > , VariantConversionError > {
225
- Ok ( self . keys ( ) . try_to_vec :: < K > ( ) ?. into_iter ( ) . collect ( ) )
226
- }
227
-
228
208
#[ doc( hidden) ]
229
209
pub fn as_inner ( & self ) -> inner:: InnerDictionary {
230
210
inner:: InnerDictionary :: from_outer ( self )
@@ -236,7 +216,7 @@ impl Dictionary {
236
216
impl < K : ToVariant , V : ToVariant > From < & HashMap < K , V > > for Dictionary {
237
217
fn from ( map : & HashMap < K , V > ) -> Self {
238
218
let mut dict = Dictionary :: new ( ) ;
239
- for ( k, v) in map. into_iter ( ) {
219
+ for ( k, v) in map. iter ( ) {
240
220
dict. insert ( k. to_variant ( ) , v. to_variant ( ) ) ;
241
221
}
242
222
dict
@@ -245,16 +225,53 @@ impl<K: ToVariant, V: ToVariant> From<&HashMap<K, V>> for Dictionary {
245
225
246
226
/// Creates a `Dictionary` from the given `HashSet`. Each key is converted
247
227
/// to a `Variant`, and the values are all `true`.
228
+ ///
229
+ ///
248
230
impl < K : ToVariant > From < & HashSet < K > > for Dictionary {
249
231
fn from ( set : & HashSet < K > ) -> Self {
250
232
let mut dict = Dictionary :: new ( ) ;
251
- for k in set. into_iter ( ) {
233
+ for k in set. iter ( ) {
252
234
dict. insert ( k. to_variant ( ) , true . to_variant ( ) ) ;
253
235
}
254
236
dict
255
237
}
256
238
}
257
239
240
+ /// Convert this dictionary to a strongly typed rust `HashMap`. If the conversion
241
+ /// fails for any key or value, an error is returned.
242
+ ///
243
+ /// Will be replaced by a proper iteration implementation.
244
+ impl < K : FromVariant + Eq + std:: hash:: Hash , V : FromVariant > TryFrom < & Dictionary > for HashMap < K , V > {
245
+ type Error = VariantConversionError ;
246
+
247
+ fn try_from ( dictionary : & Dictionary ) -> Result < Self , Self :: Error > {
248
+ // TODO: try to panic or something if modified while iterating
249
+ // Though probably better to fix when implementing iteration proper
250
+ dictionary
251
+ . keys ( )
252
+ . iter ( )
253
+ . zip ( dictionary. values ( ) . iter ( ) )
254
+ . map ( |( key, value) | Ok ( ( K :: try_from_variant ( & key) ?, V :: try_from_variant ( & value) ?) ) )
255
+ . collect ( )
256
+ }
257
+ }
258
+
259
+ /// Convert the keys of this dictionary to a strongly typed rust `HashSet`. If the
260
+ /// conversion fails for any key, an error is returned.
261
+ impl < K : FromVariant + Eq + std:: hash:: Hash > TryFrom < & Dictionary > for HashSet < K > {
262
+ type Error = VariantConversionError ;
263
+
264
+ fn try_from ( dictionary : & Dictionary ) -> Result < Self , Self :: Error > {
265
+ // TODO: try to panic or something if modified while iterating
266
+ // Though probably better to fix when implementing iteration proper
267
+ dictionary
268
+ . keys ( )
269
+ . iter ( )
270
+ . map ( |key| K :: try_from_variant ( & key) )
271
+ . collect ( )
272
+ }
273
+ }
274
+
258
275
/// Inserts all key-values from the iterator into the dictionary,
259
276
/// replacing values with existing keys with new values returned
260
277
/// from the iterator.
@@ -335,7 +352,7 @@ macro_rules! dict {
335
352
{
336
353
let mut d = :: godot:: builtin:: Dictionary :: new( ) ;
337
354
$(
338
- d. set( $key, :: godot :: builtin :: Variant :: from ( $value) ) ;
355
+ d. set( $key, $value) ;
339
356
) *
340
357
d
341
358
}
0 commit comments