Skip to content

Commit b760f02

Browse files
committed
renamed builtin hash -> hash_u32; added tests
1 parent 4a58210 commit b760f02

File tree

8 files changed

+50
-8
lines changed

8 files changed

+50
-8
lines changed

godot-core/src/builtin/callable.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,13 @@ impl Callable {
446446
InstanceId::try_from_i64(id)
447447
}
448448

449-
/// Returns the 32-bit hash value of this callable's object.
450-
///
451-
/// _Godot equivalent: `hash`_
449+
crate::declare_hash_u32_method! {
450+
/// Returns the 32-bit hash value of this callable's object.
451+
///
452+
/// _Godot equivalent: `hash`_
453+
}
454+
455+
#[deprecated = "renamed to hash_u32"]
452456
pub fn hash(&self) -> u32 {
453457
self.as_inner().hash().try_into().unwrap()
454458
}

godot-core/src/builtin/collections/dictionary.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ impl Dictionary {
285285
old_value
286286
}
287287

288-
/// Returns a 32-bit integer hash value representing the dictionary and its contents.
288+
crate::declare_hash_u32_method! {
289+
/// Returns a 32-bit integer hash value representing the dictionary and its contents.
290+
}
291+
292+
#[deprecated = "renamed to hash_u32"]
289293
#[must_use]
290294
pub fn hash(&self) -> u32 {
291295
self.as_inner().hash().try_into().unwrap()

godot-core/src/builtin/macros.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ macro_rules! impl_builtin_traits_inner {
103103
( Hash for $Type:ty ) => {
104104
impl std::hash::Hash for $Type {
105105
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
106-
self.hash().hash(state)
106+
// The GDExtension interface only deals in `int64_t`, but the engine's own `hash()` function
107+
// actually returns `uint32_t`.
108+
self.hash_u32().hash(state)
107109
}
108110
}
109111
};

godot-core/src/builtin/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ pub mod inner {
116116
pub use crate::gen::builtin_classes::*;
117117
}
118118

119+
#[macro_export]
120+
macro_rules! declare_hash_u32_method {
121+
($ ($docs:tt)+ ) => {
122+
$( $docs )+
123+
pub fn hash_u32(&self) -> u32 {
124+
self.as_inner().hash().try_into().expect("Godot hashes are uint32_t")
125+
}
126+
}
127+
}
128+
119129
// ----------------------------------------------------------------------------------------------------------------------------------------------
120130
// Conversion functions
121131

godot-core/src/builtin/string/gstring.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ impl GString {
156156
self.as_inner().length().try_into().unwrap()
157157
}
158158

159-
/// Returns a 32-bit integer hash value representing the string.
159+
crate::declare_hash_u32_method! {
160+
/// Returns a 32-bit integer hash value representing the string.
161+
}
162+
163+
#[deprecated = "renamed to hash_u32"]
160164
pub fn hash(&self) -> u32 {
161165
self.as_inner()
162166
.hash()

godot-core/src/builtin/string/node_path.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ impl NodePath {
117117
self.get_name_count() + self.get_subname_count()
118118
}
119119

120-
/// Returns a 32-bit integer hash value representing the string.
120+
crate::declare_hash_u32_method! {
121+
/// Returns a 32-bit integer hash value representing the string.
122+
}
123+
124+
#[deprecated = "renamed to hash_u32"]
121125
pub fn hash(&self) -> u32 {
122126
self.as_inner()
123127
.hash()

godot-core/src/builtin/string/string_name.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ impl StringName {
139139
self.as_inner().length() as usize
140140
}
141141

142-
/// Returns a 32-bit integer hash value representing the string.
142+
crate::declare_hash_u32_method! {
143+
/// Returns a 32-bit integer hash value representing the string.
144+
}
145+
146+
#[deprecated = "renamed to hash_u32"]
143147
pub fn hash(&self) -> u32 {
144148
self.as_inner()
145149
.hash()

godot-core/src/builtin/variant/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ impl Variant {
310310
/// Return Godot's hash value for the variant.
311311
///
312312
/// _Godot equivalent : `@GlobalScope.hash()`_
313+
pub fn hash_u32(&self) -> u32 {
314+
// @GlobalScope.hash() actually calls the VariantUtilityFunctions::hash(&Variant) function (cpp).
315+
// This function calls the passed reference's `hash` method, which returns a uint32_t.
316+
// Therefore, casting this function to u32 is always fine.
317+
unsafe { interface_fn!(variant_hash)(self.var_sys()) }
318+
.try_into()
319+
.expect("Godot hashes are uint32_t")
320+
}
321+
322+
#[deprecated = "renamed to hash_u32 and type changed to u32"]
313323
pub fn hash(&self) -> i64 {
314324
unsafe { interface_fn!(variant_hash)(self.var_sys()) }
315325
}

0 commit comments

Comments
 (0)