Skip to content

Commit 8b1dc41

Browse files
committed
potential Dictionary API improvements
1 parent fa59997 commit 8b1dc41

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

gdnative-core/src/core_types/dictionary.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ impl<Access: ThreadAccess> Dictionary<Access> {
7272
/// Returns a copy of the value corresponding to the key.
7373
#[inline]
7474
pub fn get<K>(&self, key: K) -> Variant
75+
where
76+
K: ToVariant + ToVariantEq,
77+
{
78+
self.get_or(Variant::new(), key)
79+
}
80+
81+
#[inline]
82+
pub fn get_or_insert_default<K>(&self, key: K) -> Variant
7583
where
7684
K: ToVariant + ToVariantEq,
7785
{
@@ -83,6 +91,36 @@ impl<Access: ThreadAccess> Dictionary<Access> {
8391
}
8492
}
8593

94+
/// Returns a copy of the value corresponding to the key, or `default` if it doesn't exist
95+
#[inline]
96+
pub fn get_or<K, D>(&self, default: D, key: K) -> Variant
97+
where
98+
K: ToVariant + ToVariantEq,
99+
D: ToVariant + ToVariantEq,
100+
{
101+
unsafe {
102+
Variant((get_api().godot_dictionary_get_with_default)(
103+
self.sys(),
104+
key.to_variant().sys(),
105+
default.to_variant().sys(),
106+
))
107+
}
108+
}
109+
110+
/// Returns a copy of the value corresponding to the key if it exists and is not `Nil`, or `None` otherwise
111+
#[inline]
112+
pub fn get_or_none<K>(&self, key: K) -> Option<Variant>
113+
where
114+
K: ToVariant + ToVariantEq,
115+
{
116+
let result = self.get(key);
117+
if result.is_nil() {
118+
None
119+
} else {
120+
Some(result)
121+
}
122+
}
123+
86124
/// Update an existing element corresponding ot the key.
87125
///
88126
/// # Panics

0 commit comments

Comments
 (0)