Skip to content

Commit

Permalink
Merge #809
Browse files Browse the repository at this point in the history
809: Relax Dictionary bound for keys: ToVariant -> OwnedToVariant r=Bromeon a=Bromeon

Fixes #752

bors try

Co-authored-by: Jan Haller <bromeon@gmail.com>
  • Loading branch information
bors[bot] and Bromeon committed Nov 2, 2021
2 parents 8c5cda8 + b5b40d0 commit cc96860
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions gdnative-core/src/core_types/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use std::iter::{Extend, FromIterator};
use std::marker::PhantomData;

use crate::core_types::GodotString;
use crate::private::get_api;
use crate::sys;

use crate::core_types::OwnedToVariant;
use crate::core_types::ToVariant;
use crate::core_types::ToVariantEq;
use crate::core_types::Variant;
use crate::core_types::VariantArray;
use crate::core_types::{GodotString, OwnedToVariant, ToVariantEq, Variant, VariantArray};
use crate::object::NewRef;
use std::fmt;

Expand Down Expand Up @@ -56,9 +51,9 @@ impl<Access: ThreadAccess> Dictionary<Access> {
#[inline]
pub fn contains<K>(&self, key: K) -> bool
where
K: ToVariant + ToVariantEq,
K: OwnedToVariant + ToVariantEq,
{
unsafe { (get_api().godot_dictionary_has)(self.sys(), key.to_variant().sys()) }
unsafe { (get_api().godot_dictionary_has)(self.sys(), key.owned_to_variant().sys()) }
}

/// Returns true if the `Dictionary` has all of the keys in the given array.
Expand All @@ -71,9 +66,9 @@ impl<Access: ThreadAccess> Dictionary<Access> {
#[inline]
pub fn get<K>(&self, key: K) -> Option<Variant>
where
K: ToVariant + ToVariantEq,
K: OwnedToVariant + ToVariantEq,
{
let key = key.to_variant();
let key = key.owned_to_variant();
if self.contains(&key) {
// This should never return the default Nil, but there isn't a safe way to otherwise check
// if the entry exists in a single API call.
Expand All @@ -87,14 +82,14 @@ impl<Access: ThreadAccess> Dictionary<Access> {
#[inline]
pub fn get_or<K, D>(&self, key: K, default: D) -> Variant
where
K: ToVariant + ToVariantEq,
D: ToVariant,
K: OwnedToVariant + ToVariantEq,
D: OwnedToVariant,
{
unsafe {
Variant((get_api().godot_dictionary_get_with_default)(
self.sys(),
key.to_variant().sys(),
default.to_variant().sys(),
key.owned_to_variant().sys(),
default.owned_to_variant().sys(),
))
}
}
Expand All @@ -104,7 +99,7 @@ impl<Access: ThreadAccess> Dictionary<Access> {
#[inline]
pub fn get_or_nil<K>(&self, key: K) -> Variant
where
K: ToVariant + ToVariantEq,
K: OwnedToVariant + ToVariantEq,
{
self.get_or(key, Variant::new())
}
Expand All @@ -117,10 +112,10 @@ impl<Access: ThreadAccess> Dictionary<Access> {
#[inline]
pub fn update<K, V>(&self, key: K, val: V)
where
K: ToVariant + ToVariantEq,
K: OwnedToVariant + ToVariantEq,
V: OwnedToVariant,
{
let key = key.to_variant();
let key = key.owned_to_variant();
assert!(self.contains(&key), "Can only update entries that exist");

unsafe {
Expand All @@ -145,11 +140,11 @@ impl<Access: ThreadAccess> Dictionary<Access> {
#[inline]
pub unsafe fn get_ref<K>(&self, key: K) -> &Variant
where
K: ToVariant + ToVariantEq,
K: OwnedToVariant + ToVariantEq,
{
Variant::cast_ref((get_api().godot_dictionary_operator_index_const)(
self.sys(),
key.to_variant().sys(),
key.owned_to_variant().sys(),
))
}

Expand All @@ -167,11 +162,11 @@ impl<Access: ThreadAccess> Dictionary<Access> {
#[allow(clippy::mut_from_ref)]
pub unsafe fn get_mut_ref<K>(&self, key: K) -> &mut Variant
where
K: ToVariant + ToVariantEq,
K: OwnedToVariant + ToVariantEq,
{
Variant::cast_mut_ref((get_api().godot_dictionary_operator_index)(
self.sys_mut(),
key.to_variant().sys(),
key.owned_to_variant().sys(),
))
}

Expand Down Expand Up @@ -308,9 +303,9 @@ impl<Access: LocalThreadAccess> Dictionary<Access> {
#[inline]
pub fn erase<K>(&self, key: K)
where
K: ToVariant + ToVariantEq,
K: OwnedToVariant + ToVariantEq,
{
unsafe { (get_api().godot_dictionary_erase)(self.sys_mut(), key.to_variant().sys()) }
unsafe { (get_api().godot_dictionary_erase)(self.sys_mut(), key.owned_to_variant().sys()) }
}

/// Clears the `Dictionary`, removing all key-value pairs.
Expand Down

0 comments on commit cc96860

Please sign in to comment.