Skip to content

Commit 7a83936

Browse files
committed
implemented hash_u32 macro implementer
1 parent 641a271 commit 7a83936

File tree

7 files changed

+24
-39
lines changed

7 files changed

+24
-39
lines changed

godot-core/src/builtin/callable.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,10 @@ 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`_
452-
pub fn hash_u32(&self) -> u32 {
453-
self.as_inner()
454-
.hash()
455-
.try_into()
456-
.expect("Godot hashes are uint32_t")
449+
crate::declare_hash_u32_method! {
450+
/// Returns the 32-bit hash value of this callable's object.
451+
///
452+
/// _Godot equivalent: `hash`_
457453
}
458454

459455
#[deprecated = "renamed to hash_u32"]

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,11 @@ impl<T: ArrayElement> Array<T> {
285285
self.as_inner().is_empty()
286286
}
287287

288-
/// Returns a 32-bit integer hash value representing the array and its contents.
288+
crate::declare_hash_u32_method! {/// Returns a 32-bit integer hash value representing the array and its contents.
289289
///
290290
/// Note: Arrays with equal content will always produce identical hash values. However, the
291291
/// reverse is not true. Returning identical hash values does not imply the arrays are equal,
292292
/// because different arrays can have identical hash values due to hash collisions.
293-
pub fn hash_u32(&self) -> u32 {
294-
self.as_inner()
295-
.hash()
296-
.try_into()
297-
.expect("Godot hashes are uint32_t")
298293
}
299294

300295
#[deprecated = "renamed to hash_u32"]

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,8 @@ impl Dictionary {
285285
old_value
286286
}
287287

288-
/// Returns a 32-bit integer hash value representing the dictionary and its contents.
289-
#[must_use]
290-
pub fn hash_u32(&self) -> u32 {
291-
self.as_inner()
292-
.hash()
293-
.try_into()
294-
.expect("Godot hashes are uint32_t")
288+
crate::declare_hash_u32_method! {
289+
/// Returns a 32-bit integer hash value representing the dictionary and its contents.
295290
}
296291

297292
#[deprecated = "renamed to hash_u32"]

godot-core/src/builtin/mod.rs

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

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

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,8 @@ impl GString {
156156
self.as_inner().length().try_into().unwrap()
157157
}
158158

159-
/// Returns a 32-bit integer hash value representing the string.
160-
pub fn hash_u32(&self) -> u32 {
161-
self.as_inner()
162-
.hash()
163-
.try_into()
164-
.expect("Godot hashes are uint32_t")
159+
crate::declare_hash_u32_method! {
160+
/// Returns a 32-bit integer hash value representing the string.
165161
}
166162

167163
#[deprecated = "renamed to hash_u32"]

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,8 @@ impl NodePath {
117117
self.get_name_count() + self.get_subname_count()
118118
}
119119

120-
/// Returns a 32-bit integer hash value representing the string.
121-
pub fn hash_u32(&self) -> u32 {
122-
self.as_inner()
123-
.hash()
124-
.try_into()
125-
.expect("Godot hashes are uint32_t")
120+
crate::declare_hash_u32_method! {
121+
/// Returns a 32-bit integer hash value representing the string.
126122
}
127123

128124
#[deprecated = "renamed to hash_u32"]

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,8 @@ impl StringName {
139139
self.as_inner().length() as usize
140140
}
141141

142-
/// Returns a 32-bit integer hash value representing the string.
143-
pub fn hash_u32(&self) -> u32 {
144-
self.as_inner()
145-
.hash()
146-
.try_into()
147-
.expect("Godot hashes are uint32_t")
142+
crate::declare_hash_u32_method! {
143+
/// Returns a 32-bit integer hash value representing the string.
148144
}
149145

150146
#[deprecated = "renamed to hash_u32"]

0 commit comments

Comments
 (0)