Skip to content

Commit

Permalink
Minor fixes from bromeon
Browse files Browse the repository at this point in the history
Run rustfmt
  • Loading branch information
lilizoey committed Jan 28, 2023
1 parent 3786a0a commit 752bdac
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 138 deletions.
91 changes: 45 additions & 46 deletions godot-core/src/builtin/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use super::Array;
/// Godot's `Dictionary` type.
///
/// The keys and values of the array are all `Variant`, so they can all be of different types.
///
/// Keys are assumed to be cheap to clone, this will usually be the case especially for
/// value types and reference counted types.
/// Variants are designed to be generally cheap to clone.
///
/// # Thread safety
///
Expand All @@ -34,7 +32,7 @@ impl Dictionary {
Self::default()
}

/// Removes all key-value pairs from the dictionary. Equivalent to `clear` in godot.
/// Removes all key-value pairs from the dictionary. Equivalent to `clear` in Godot.
pub fn clear(&mut self) {
self.as_inner().clear()
}
Expand All @@ -46,7 +44,7 @@ impl Dictionary {
/// To create a shallow copy, use [`duplicate_shallow()`] instead. To create a new reference to
/// the same array data, use [`share()`].
///
/// Equivalent to `dictionary.duplicate(true)` in godot.
/// Equivalent to `dictionary.duplicate(true)` in Godot.
pub fn duplicate_deep(&self) -> Self {
self.as_inner().duplicate(true)
}
Expand All @@ -58,14 +56,14 @@ impl Dictionary {
/// To create a deep copy, use [`duplicate_deep()`] instead. To create a new reference to the
/// same dictionary data, use [`share()`].
///
/// Equivalent to `dictionary.duplicate(false)` in godot.
/// Equivalent to `dictionary.duplicate(false)` in Godot.
pub fn duplicate_shallow(&self) -> Self {
self.as_inner().duplicate(false)
}

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

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

if !key.is_nil() || self.contains_key(key.clone()) {
Expand All @@ -89,9 +87,9 @@ impl Dictionary {
/// Returns the value at the key in the dictionary, if there is
/// one.
///
/// Unlike `get` in godot, this will return `None` if there is
/// Unlike `get` in Godot, this will return `None` if there is
/// no value with the given key.
pub fn get(&self, key: impl ToVariant) -> Option<Variant> {
pub fn get<K: ToVariant>(&self, key: K) -> Option<Variant> {
let key = key.to_variant();
if !self.contains_key(key.clone()) {
return None;
Expand All @@ -104,22 +102,22 @@ impl Dictionary {
/// method does not let you differentiate `NIL` values stored as values from
/// absent keys. If you need that, use `get()`.
///
/// This is equivalent to `get` in godot.
pub fn get_or_nil(&self, key: impl ToVariant) -> Variant {
/// This is equivalent to `get` in Godot.
pub fn get_or_nil<K: ToVariant>(&self, key: K) -> Variant {
self.as_inner().get(key.to_variant(), Variant::nil())
}

/// Returns `true` if the dictionary contains the given key.
///
/// This is equivalent to `has` in godot.
pub fn contains_key(&self, key: impl ToVariant) -> bool {
/// This is equivalent to `has` in Godot.
pub fn contains_key<K: ToVariant>(&self, key: K) -> bool {
let key = key.to_variant();
self.as_inner().has(key)
}

/// Returns `true` if the dictionary contains all the given keys.
///
/// This is equivalent to `has_all` in godot.
/// This is equivalent to `has_all` in Godot.
pub fn contains_all_keys(&self, keys: Array) -> bool {
self.as_inner().has_all(keys)
}
Expand Down Expand Up @@ -149,56 +147,56 @@ impl Dictionary {
/// If overwrite is true, it will overwrite pre-existing keys. Otherwise
/// it will not.
///
/// This is equivalent to `merge` in godot.
/// This is equivalent to `merge` in Godot.
pub fn extend_dictionary(&mut self, other: Self, overwrite: bool) {
self.as_inner().merge(other, overwrite)
}

/// Returns the number of entries in the dictionary.
///
/// This is equivalent to `size` in godot.
/// This is equivalent to `size` in Godot.
pub fn len(&self) -> usize {
self.as_inner().size().try_into().unwrap()
}

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

/// Get the pointer corresponding to the given key in the dictionary,
/// if there exists no value at the given key then a new one is created
/// and initialized to a nil variant.
fn get_ptr_mut(&mut self, key: impl ToVariant) -> *mut Variant {
fn get_ptr_mut<K: ToVariant>(&mut self, key: K) -> *mut Variant {
let key = key.to_variant();
unsafe {
let ptr =
(interface_fn!(dictionary_operator_index))(self.sys_mut(), key.var_sys_const())
as *mut Variant;
// dictionary_operator_index initializes the value so it wont be null
(interface_fn!(dictionary_operator_index))(self.sys_mut(), key.var_sys_const());
assert!(!ptr.is_null());
// i think it might be safe to turn this into a &mut Variant?
ptr
ptr as *mut Variant
}
}

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

/// Set a key to a given value
pub fn set(&mut self, key: impl ToVariant, value: impl ToVariant) {
/// Set a key to a given value.
pub fn set<K: ToVariant, V: ToVariant>(&mut self, key: K, value: V) {
let key = key.to_variant();
unsafe {
*self.get_ptr_mut(key) = value.to_variant();
Expand All @@ -211,16 +209,19 @@ impl Dictionary {
}
}

/// Creates a `Dictionary` from the given `T`. Each key and value are
/// Creates a `Dictionary` from the given `I`. Each key and value are
/// converted to a `Variant`.
impl<'a, 'b, K, V, T> From<T> for Dictionary
impl<'a, 'b, K, V, I> From<I> for Dictionary
where
T: IntoIterator<Item = (&'a K, &'b V)>,
I: IntoIterator<Item = (&'a K, &'b V)>,
K: ToVariant + 'a,
V: ToVariant + 'b,
{
fn from(iterable: T) -> Self {
iterable.into_iter().map(|(key,value)| (key.to_variant(), value.to_variant())).collect()
fn from(iterable: I) -> Self {
iterable
.into_iter()
.map(|(key, value)| (key.to_variant(), value.to_variant()))
.collect()
}
}

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

impl<K: ToVariant, V: ToVariant> FromIterator<(K, V)> for Dictionary {
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let mut dict = Dictionary::new();
for (k, v) in iter.into_iter() {
dict.set(k.to_variant(), v.to_variant())
}
dict.extend(iter);
dict
}
}
Expand Down Expand Up @@ -316,13 +315,13 @@ impl Share for Dictionary {
}

/// Creates a new dictionary with the given keys and values, the syntax mirrors
/// godot's dictionary creation syntax.
/// Godot's dictionary creation syntax.
///
/// Any value can be used as a key, but to use an expression you need to surround it
/// in `()` or `{}`
/// in `()` or `{}`.
///
/// Example
/// ```rust, no_run
/// ```no_run
/// # #[macro_use] extern crate godot_core;
/// # fn main() {
/// let key = "my_key";
Expand All @@ -340,9 +339,9 @@ macro_rules! dict {
{
let mut d = $crate::builtin::Dictionary::new();
$(
// otherwise `(1 + 2): true` would complain even though you can't write
// 1 + 2: true
#[allow(unused_parens)]
// `cargo check` complains that `(1 + 2): true` has unused parens, even though it's not
// possible to omit the parens.
#[allow(unused_parens)]
d.set($key, $value);
)*
d
Expand Down
2 changes: 1 addition & 1 deletion godot-core/src/builtin/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use godot_ffi as sys;
use sys::types::OpaqueString;
use sys::{ffi_methods, interface_fn, GodotFfi};

use super::{Variant, VariantConversionError, ToVariant, FromVariant};
use super::{FromVariant, ToVariant, Variant, VariantConversionError};

#[repr(C, align(8))]
pub struct GodotString {
Expand Down
Loading

0 comments on commit 752bdac

Please sign in to comment.