Skip to content

Commit c6d87c9

Browse files
committed
Minor fixes from bromeon
Run rustfmt
1 parent 3786a0a commit c6d87c9

File tree

3 files changed

+71
-138
lines changed

3 files changed

+71
-138
lines changed

godot-core/src/builtin/dictionary.rs

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use super::Array;
1212
/// Godot's `Dictionary` type.
1313
///
1414
/// The keys and values of the array are all `Variant`, so they can all be of different types.
15-
///
16-
/// Keys are assumed to be cheap to clone, this will usually be the case especially for
17-
/// value types and reference counted types.
15+
/// Variants are designed to be generally cheap to clone.
1816
///
1917
/// # Thread safety
2018
///
@@ -34,7 +32,7 @@ impl Dictionary {
3432
Self::default()
3533
}
3634

37-
/// Removes all key-value pairs from the dictionary. Equivalent to `clear` in godot.
35+
/// Removes all key-value pairs from the dictionary. Equivalent to `clear` in Godot.
3836
pub fn clear(&mut self) {
3937
self.as_inner().clear()
4038
}
@@ -46,7 +44,7 @@ impl Dictionary {
4644
/// To create a shallow copy, use [`duplicate_shallow()`] instead. To create a new reference to
4745
/// the same array data, use [`share()`].
4846
///
49-
/// Equivalent to `dictionary.duplicate(true)` in godot.
47+
/// Equivalent to `dictionary.duplicate(true)` in Godot.
5048
pub fn duplicate_deep(&self) -> Self {
5149
self.as_inner().duplicate(true)
5250
}
@@ -58,14 +56,14 @@ impl Dictionary {
5856
/// To create a deep copy, use [`duplicate_deep()`] instead. To create a new reference to the
5957
/// same dictionary data, use [`share()`].
6058
///
61-
/// Equivalent to `dictionary.duplicate(false)` in godot.
59+
/// Equivalent to `dictionary.duplicate(false)` in Godot.
6260
pub fn duplicate_shallow(&self) -> Self {
6361
self.as_inner().duplicate(false)
6462
}
6563

6664
/// Removes a key from the map, and returns the value associated with
6765
/// the key if the key was in the dictionary.
68-
pub fn remove(&mut self, key: impl ToVariant) -> Option<Variant> {
66+
pub fn remove<K: ToVariant>(&mut self, key: K) -> Option<Variant> {
6967
let key = key.to_variant();
7068
let old_value = self.get(key.clone());
7169
self.as_inner().erase(key);
@@ -74,9 +72,9 @@ impl Dictionary {
7472

7573
/// Returns the first key whose associated value is `value`, if one exists.
7674
///
77-
/// Unlike in godot, this will return `None` if the key does not exist
75+
/// Unlike in Godot, this will return `None` if the key does not exist
7876
/// and `Some(nil)` the key is `null`.
79-
pub fn find_key_by_value(&self, value: impl ToVariant) -> Option<Variant> {
77+
pub fn find_key_by_value<V: ToVariant>(&self, value: V) -> Option<Variant> {
8078
let key = self.as_inner().find_key(value.to_variant());
8179

8280
if !key.is_nil() || self.contains_key(key.clone()) {
@@ -89,9 +87,9 @@ impl Dictionary {
8987
/// Returns the value at the key in the dictionary, if there is
9088
/// one.
9189
///
92-
/// Unlike `get` in godot, this will return `None` if there is
90+
/// Unlike `get` in Godot, this will return `None` if there is
9391
/// no value with the given key.
94-
pub fn get(&self, key: impl ToVariant) -> Option<Variant> {
92+
pub fn get<K: ToVariant>(&self, key: K) -> Option<Variant> {
9593
let key = key.to_variant();
9694
if !self.contains_key(key.clone()) {
9795
return None;
@@ -104,22 +102,22 @@ impl Dictionary {
104102
/// method does not let you differentiate `NIL` values stored as values from
105103
/// absent keys. If you need that, use `get()`.
106104
///
107-
/// This is equivalent to `get` in godot.
108-
pub fn get_or_nil(&self, key: impl ToVariant) -> Variant {
105+
/// This is equivalent to `get` in Godot.
106+
pub fn get_or_nil<K: ToVariant>(&self, key: K) -> Variant {
109107
self.as_inner().get(key.to_variant(), Variant::nil())
110108
}
111109

112110
/// Returns `true` if the dictionary contains the given key.
113111
///
114-
/// This is equivalent to `has` in godot.
115-
pub fn contains_key(&self, key: impl ToVariant) -> bool {
112+
/// This is equivalent to `has` in Godot.
113+
pub fn contains_key<K: ToVariant>(&self, key: K) -> bool {
116114
let key = key.to_variant();
117115
self.as_inner().has(key)
118116
}
119117

120118
/// Returns `true` if the dictionary contains all the given keys.
121119
///
122-
/// This is equivalent to `has_all` in godot.
120+
/// This is equivalent to `has_all` in Godot.
123121
pub fn contains_all_keys(&self, keys: Array) -> bool {
124122
self.as_inner().has_all(keys)
125123
}
@@ -149,56 +147,56 @@ impl Dictionary {
149147
/// If overwrite is true, it will overwrite pre-existing keys. Otherwise
150148
/// it will not.
151149
///
152-
/// This is equivalent to `merge` in godot.
150+
/// This is equivalent to `merge` in Godot.
153151
pub fn extend_dictionary(&mut self, other: Self, overwrite: bool) {
154152
self.as_inner().merge(other, overwrite)
155153
}
156154

157155
/// Returns the number of entries in the dictionary.
158156
///
159-
/// This is equivalent to `size` in godot.
157+
/// This is equivalent to `size` in Godot.
160158
pub fn len(&self) -> usize {
161159
self.as_inner().size().try_into().unwrap()
162160
}
163161

164162
/// Get the pointer corresponding to the given key in the dictionary,
165163
/// this pointer is null if there is no value at the given key.
166164
#[allow(dead_code)] // TODO: remove function if it turns out i'll never actually get any use out of it
167-
fn get_ptr(&self, key: impl ToVariant) -> *const Variant {
165+
fn get_ptr<K: ToVariant>(&self, key: K) -> *const Variant {
168166
let key = key.to_variant();
169167
unsafe {
170-
(interface_fn!(dictionary_operator_index_const))(self.sys_const(), key.var_sys_const())
171-
as *const Variant
168+
let ptr = (interface_fn!(dictionary_operator_index_const))(
169+
self.sys_const(),
170+
key.var_sys_const(),
171+
);
172+
ptr as *const Variant
172173
}
173174
}
174175

175176
/// Get the pointer corresponding to the given key in the dictionary,
176177
/// if there exists no value at the given key then a new one is created
177178
/// and initialized to a nil variant.
178-
fn get_ptr_mut(&mut self, key: impl ToVariant) -> *mut Variant {
179+
fn get_ptr_mut<K: ToVariant>(&mut self, key: K) -> *mut Variant {
179180
let key = key.to_variant();
180181
unsafe {
181182
let ptr =
182-
(interface_fn!(dictionary_operator_index))(self.sys_mut(), key.var_sys_const())
183-
as *mut Variant;
184-
// dictionary_operator_index initializes the value so it wont be null
183+
(interface_fn!(dictionary_operator_index))(self.sys_mut(), key.var_sys_const());
185184
assert!(!ptr.is_null());
186-
// i think it might be safe to turn this into a &mut Variant?
187-
ptr
185+
ptr as *mut Variant
188186
}
189187
}
190188

191189
/// Insert a value at the given key, returning the value
192190
/// that previously was at that key if there was one.
193-
pub fn insert(&mut self, key: impl ToVariant, value: impl ToVariant) -> Option<Variant> {
191+
pub fn insert<K: ToVariant, V: ToVariant>(&mut self, key: K, value: V) -> Option<Variant> {
194192
let key = key.to_variant();
195193
let old_value = self.get(key.clone());
196194
self.set(key, value);
197195
old_value
198196
}
199197

200-
/// Set a key to a given value
201-
pub fn set(&mut self, key: impl ToVariant, value: impl ToVariant) {
198+
/// Set a key to a given value.
199+
pub fn set<K: ToVariant, V: ToVariant>(&mut self, key: K, value: V) {
202200
let key = key.to_variant();
203201
unsafe {
204202
*self.get_ptr_mut(key) = value.to_variant();
@@ -211,16 +209,19 @@ impl Dictionary {
211209
}
212210
}
213211

214-
/// Creates a `Dictionary` from the given `T`. Each key and value are
212+
/// Creates a `Dictionary` from the given `I`. Each key and value are
215213
/// converted to a `Variant`.
216-
impl<'a, 'b, K, V, T> From<T> for Dictionary
214+
impl<'a, 'b, K, V, I> From<I> for Dictionary
217215
where
218-
T: IntoIterator<Item = (&'a K, &'b V)>,
216+
I: IntoIterator<Item = (&'a K, &'b V)>,
219217
K: ToVariant + 'a,
220218
V: ToVariant + 'b,
221219
{
222-
fn from(iterable: T) -> Self {
223-
iterable.into_iter().map(|(key,value)| (key.to_variant(), value.to_variant())).collect()
220+
fn from(iterable: I) -> Self {
221+
iterable
222+
.into_iter()
223+
.map(|(key, value)| (key.to_variant(), value.to_variant()))
224+
.collect()
224225
}
225226
}
226227

@@ -263,19 +264,17 @@ impl<K: FromVariant + Eq + std::hash::Hash> TryFrom<&Dictionary> for HashSet<K>
263264
/// replacing values with existing keys with new values returned
264265
/// from the iterator.
265266
impl<K: ToVariant, V: ToVariant> Extend<(K, V)> for Dictionary {
266-
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
267+
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
267268
for (k, v) in iter.into_iter() {
268269
self.set(k.to_variant(), v.to_variant())
269270
}
270271
}
271272
}
272273

273274
impl<K: ToVariant, V: ToVariant> FromIterator<(K, V)> for Dictionary {
274-
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
275+
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
275276
let mut dict = Dictionary::new();
276-
for (k, v) in iter.into_iter() {
277-
dict.set(k.to_variant(), v.to_variant())
278-
}
277+
dict.extend(iter);
279278
dict
280279
}
281280
}
@@ -316,13 +315,13 @@ impl Share for Dictionary {
316315
}
317316

318317
/// Creates a new dictionary with the given keys and values, the syntax mirrors
319-
/// godot's dictionary creation syntax.
318+
/// Godot's dictionary creation syntax.
320319
///
321320
/// Any value can be used as a key, but to use an expression you need to surround it
322-
/// in `()` or `{}`
321+
/// in `()` or `{}`.
323322
///
324323
/// Example
325-
/// ```rust, no_run
324+
/// ```no_run
326325
/// # #[macro_use] extern crate godot_core;
327326
/// # fn main() {
328327
/// let key = "my_key";
@@ -340,9 +339,9 @@ macro_rules! dict {
340339
{
341340
let mut d = $crate::builtin::Dictionary::new();
342341
$(
343-
// otherwise `(1 + 2): true` would complain even though you can't write
344-
// 1 + 2: true
345-
#[allow(unused_parens)]
342+
// Check complains that `(1 + 2): true` has unused parens, even though it's not
343+
// possible to omit the parens.
344+
#[allow(unused_parens)]
346345
d.set($key, $value);
347346
)*
348347
d

godot-core/src/builtin/string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use godot_ffi as sys;
1010
use sys::types::OpaqueString;
1111
use sys::{ffi_methods, interface_fn, GodotFfi};
1212

13-
use super::{Variant, VariantConversionError, ToVariant, FromVariant};
13+
use super::{FromVariant, ToVariant, Variant, VariantConversionError};
1414

1515
#[repr(C, align(8))]
1616
pub struct GodotString {

0 commit comments

Comments
 (0)