@@ -69,21 +69,56 @@ impl<Access: ThreadAccess> Dictionary<Access> {
69
69
unsafe { ( get_api ( ) . godot_dictionary_has_all ) ( self . sys ( ) , keys. sys ( ) ) }
70
70
}
71
71
72
- /// Returns a copy of the value corresponding to the key.
72
+ /// Returns a copy of the value corresponding to the key, or `Nil` if it doesn't exist .
73
73
#[ inline]
74
74
pub fn get < K > ( & self , key : K ) -> Variant
75
75
where
76
76
K : ToVariant + ToVariantEq ,
77
+ {
78
+ self . get_or ( Variant :: new ( ) , key)
79
+ }
80
+
81
+ /// Returns a copy of the value corresponding to the key, or `default` if it doesn't exist
82
+ #[ inline]
83
+ pub fn get_or < K , D > ( & self , default : D , key : K ) -> Variant
84
+ where
85
+ K : ToVariant + ToVariantEq ,
86
+ D : ToVariant + ToVariantEq ,
77
87
{
78
88
unsafe {
79
- Variant ( ( get_api ( ) . godot_dictionary_get ) (
89
+ Variant ( ( get_api ( ) . godot_dictionary_get_with_default ) (
80
90
self . sys ( ) ,
81
91
key. to_variant ( ) . sys ( ) ,
92
+ default. to_variant ( ) . sys ( ) ,
82
93
) )
83
94
}
84
95
}
85
96
86
- /// Update an existing element corresponding ot the key.
97
+ /// Returns a copy of the element corresponding to the key if it exists.
98
+ #[ inline]
99
+ pub fn try_get < K > ( & self , key : K ) -> Option < Variant >
100
+ where
101
+ K : ToVariant + ToVariantEq ,
102
+ {
103
+ let key = key. to_variant ( ) ;
104
+ self . contains ( key. clone ( ) ) . then ( || self . get ( key) )
105
+ }
106
+
107
+ /// Returns a copy of the element corresponding to the key if it exists and is not `Nil`.
108
+ #[ inline]
109
+ pub fn get_non_nil < K > ( & self , key : K ) -> Option < Variant >
110
+ where
111
+ K : ToVariant + ToVariantEq ,
112
+ {
113
+ let result = self . get ( key) ;
114
+ if result. is_nil ( ) {
115
+ None
116
+ } else {
117
+ Some ( result)
118
+ }
119
+ }
120
+
121
+ /// Update an existing element corresponding to the key.
87
122
///
88
123
/// # Panics
89
124
///
@@ -266,6 +301,19 @@ impl Dictionary<Shared> {
266
301
pub unsafe fn clear ( & self ) {
267
302
( get_api ( ) . godot_dictionary_clear ) ( self . sys_mut ( ) )
268
303
}
304
+
305
+ /// Returns a copy of the value corresponding to the key, inserting `Nil` first if it does not exist.
306
+ #[ doc_variant_collection_safety]
307
+ #[ inline]
308
+ pub unsafe fn get_or_insert_nil < K > ( & self , key : K ) -> Variant
309
+ where
310
+ K : ToVariant + ToVariantEq ,
311
+ {
312
+ Variant ( ( get_api ( ) . godot_dictionary_get ) (
313
+ self . sys ( ) ,
314
+ key. to_variant ( ) . sys ( ) ,
315
+ ) )
316
+ }
269
317
}
270
318
271
319
/// Operations allowed on Dictionaries that may only be shared on the current thread.
@@ -327,6 +375,20 @@ impl<Access: LocalThreadAccess> Dictionary<Access> {
327
375
pub fn clear ( & self ) {
328
376
unsafe { ( get_api ( ) . godot_dictionary_clear ) ( self . sys_mut ( ) ) }
329
377
}
378
+
379
+ /// Returns a copy of the value corresponding to the key, inserting `Nil` first if it does not exist.
380
+ #[ inline]
381
+ pub fn get_or_insert_nil < K > ( & self , key : K ) -> Variant
382
+ where
383
+ K : ToVariant + ToVariantEq ,
384
+ {
385
+ unsafe {
386
+ Variant ( ( get_api ( ) . godot_dictionary_get ) (
387
+ self . sys ( ) ,
388
+ key. to_variant ( ) . sys ( ) ,
389
+ ) )
390
+ }
391
+ }
330
392
}
331
393
332
394
/// Operations allowed on unique Dictionaries.
0 commit comments