@@ -69,21 +69,45 @@ 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 if it exists .
73
73
#[ inline]
74
- pub fn get < K > ( & self , key : K ) -> Variant
74
+ pub fn get < K > ( & self , key : K ) -> Option < Variant >
75
75
where
76
76
K : ToVariant + ToVariantEq ,
77
+ {
78
+ let key = key. to_variant ( ) ;
79
+ // This should never return the default Nil, but there isn't a safe way to otherwise check
80
+ // if the entry exists in a single API call.
81
+ self . contains ( & key) . then ( || self . get_or_nil ( key) )
82
+ }
83
+
84
+ /// Returns a copy of the value corresponding to the key, or `default` if it doesn't exist
85
+ #[ inline]
86
+ pub fn get_or < K , D > ( & self , key : K , default : D ) -> Variant
87
+ where
88
+ K : ToVariant + ToVariantEq ,
89
+ D : ToVariant ,
77
90
{
78
91
unsafe {
79
- Variant ( ( get_api ( ) . godot_dictionary_get ) (
92
+ Variant ( ( get_api ( ) . godot_dictionary_get_with_default ) (
80
93
self . sys ( ) ,
81
94
key. to_variant ( ) . sys ( ) ,
95
+ default. to_variant ( ) . sys ( ) ,
82
96
) )
83
97
}
84
98
}
85
99
86
- /// Update an existing element corresponding ot the key.
100
+ /// Returns a copy of the element corresponding to the key, or `Nil` if it doesn't exist.
101
+ /// Shorthand for `self.get_or(key, Variant::new())`.
102
+ #[ inline]
103
+ pub fn get_or_nil < K > ( & self , key : K ) -> Variant
104
+ where
105
+ K : ToVariant + ToVariantEq ,
106
+ {
107
+ self . get_or ( key, Variant :: new ( ) )
108
+ }
109
+
110
+ /// Update an existing element corresponding to the key.
87
111
///
88
112
/// # Panics
89
113
///
@@ -106,12 +130,14 @@ impl<Access: ThreadAccess> Dictionary<Access> {
106
130
}
107
131
}
108
132
109
- /// Returns a reference to the value corresponding to the key.
133
+ /// Returns a reference to the value corresponding to the key, inserting a `Nil` value first if
134
+ /// it does not exist.
110
135
///
111
136
/// # Safety
112
137
///
113
138
/// The returned reference is invalidated if the same container is mutated through another
114
- /// reference.
139
+ /// reference, and other references may be invalidated if the entry does not already exist
140
+ /// (which causes this function to insert `Nil` and thus possibly re-allocate).
115
141
///
116
142
/// `Variant` is reference-counted and thus cheaply cloned. Consider using `get` instead.
117
143
#[ inline]
@@ -125,13 +151,16 @@ impl<Access: ThreadAccess> Dictionary<Access> {
125
151
) )
126
152
}
127
153
128
- /// Returns a mutable reference to the value corresponding to the key.
154
+ /// Returns a mutable reference to the value corresponding to the key, inserting a `Nil` value
155
+ /// first if it does not exist.
129
156
///
130
157
/// # Safety
131
158
///
132
159
/// The returned reference is invalidated if the same container is mutated through another
133
- /// reference. It is possible to create two mutable references to the same memory location
134
- /// if the same `key` is provided, causing undefined behavior.
160
+ /// reference, and other references may be invalidated if the `key` does not already exist
161
+ /// (which causes this function to insert `Nil` and thus possibly re-allocate). It is also
162
+ /// possible to create two mutable references to the same memory location if the same `key`
163
+ /// is provided, causing undefined behavior.
135
164
#[ inline]
136
165
#[ allow( clippy:: mut_from_ref) ]
137
166
pub unsafe fn get_mut_ref < K > ( & self , key : K ) -> & mut Variant
@@ -425,7 +454,7 @@ unsafe fn iter_next<Access: ThreadAccess>(
425
454
None
426
455
} else {
427
456
let key = Variant :: cast_ref ( next_ptr) . clone ( ) ;
428
- let value = dic. get ( & key) ;
457
+ let value = dic. get_or_nil ( & key) ;
429
458
* last_key = Some ( key. clone ( ) ) ;
430
459
Some ( ( key, value) )
431
460
}
@@ -591,7 +620,7 @@ godot_test!(test_dictionary {
591
620
let mut iter_keys = HashSet :: new( ) ;
592
621
let expected_keys = [ "foo" , "bar" ] . iter( ) . map( |& s| s. to_string( ) ) . collect:: <HashSet <_>>( ) ;
593
622
for ( key, value) in & dict {
594
- assert_eq!( value, dict. get( & key) ) ;
623
+ assert_eq!( Some ( value) , dict. get( & key) ) ;
595
624
if !iter_keys. insert( key. to_string( ) ) {
596
625
panic!( "key is already contained in set: {:?}" , key) ;
597
626
}
0 commit comments