Skip to content

Commit da8881d

Browse files
committed
Merge branch 'dictionary_api' into tmp
2 parents b5aebaf + efd3fad commit da8881d

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

gdnative-core/src/core_types/dictionary.rs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,56 @@ impl<Access: ThreadAccess> Dictionary<Access> {
6969
unsafe { (get_api().godot_dictionary_has_all)(self.sys(), keys.sys()) }
7070
}
7171

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.
7373
#[inline]
7474
pub fn get<K>(&self, key: K) -> Variant
7575
where
7676
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,
7787
{
7888
unsafe {
79-
Variant((get_api().godot_dictionary_get)(
89+
Variant((get_api().godot_dictionary_get_with_default)(
8090
self.sys(),
8191
key.to_variant().sys(),
92+
default.to_variant().sys(),
8293
))
8394
}
8495
}
8596

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.
87122
///
88123
/// # Panics
89124
///
@@ -266,6 +301,19 @@ impl Dictionary<Shared> {
266301
pub unsafe fn clear(&self) {
267302
(get_api().godot_dictionary_clear)(self.sys_mut())
268303
}
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+
}
269317
}
270318

271319
/// Operations allowed on Dictionaries that may only be shared on the current thread.
@@ -327,6 +375,20 @@ impl<Access: LocalThreadAccess> Dictionary<Access> {
327375
pub fn clear(&self) {
328376
unsafe { (get_api().godot_dictionary_clear)(self.sys_mut()) }
329377
}
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+
}
330392
}
331393

332394
/// Operations allowed on unique Dictionaries.

0 commit comments

Comments
 (0)