-
-
Notifications
You must be signed in to change notification settings - Fork 253
Rename builtin hash()
-> hash_u32()
; add tests
#1366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Rename builtin hash()
-> hash_u32()
; add tests
#1366
Conversation
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-1366 |
/// @GlobalScope.hash() actually calls the VariantUtilityFunctions::hash(&Variant) function (cpp). | ||
/// This function calls the passed reference's `hash` method, which returns a uint32_t. | ||
/// Therefore, casting this function to u32 is always safe | ||
pub fn hash_u32(&self) -> u32 { | ||
unsafe { interface_fn!(variant_hash)(self.var_sys()) } | ||
.try_into() | ||
.expect("Godot hashes are uint32_t") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is public thus included in docs and IDE tips. IMO user doesn't need to know about safety guarantees – especially if they are always held. I would just use the same docs as for everything else:
/// @GlobalScope.hash() actually calls the VariantUtilityFunctions::hash(&Variant) function (cpp). | |
/// This function calls the passed reference's `hash` method, which returns a uint32_t. | |
/// Therefore, casting this function to u32 is always safe | |
pub fn hash_u32(&self) -> u32 { | |
unsafe { interface_fn!(variant_hash)(self.var_sys()) } | |
.try_into() | |
.expect("Godot hashes are uint32_t") | |
/// Returns a 32-bit unsigned integer hash value representing the Variant. | |
/// | |
/// _Godot equivalent : `@GlobalScope.hash()`_ | |
pub fn hash_u32(&self) -> u32 { | |
// Note: Calls the passed reference's underlying `hash` method which returns an uint32_t. | |
// SAFETY: we pass in the valid pointer. | |
unsafe { interface_fn!(variant_hash)(self.var_sys()) } | |
.try_into() | |
.expect("Godot hashes are uint32_t") |
idk if we should keep info about VariantUtilityFunctions::hash(&Variant)
, your call 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that this should be in comment rather than RustDoc.
Note that the int conversion isn't safety relevant, so shouldn't be part of // SAFETY:
. Making a mistake here will result in a worse hash function, not UB.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll move it into a non-safety dev comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true true, I updated my suggestion
to elaborate – the only way to cause an UB here is by passing invalid GDExtensionConstVariantPtr
.
static GDExtensionInt gdextension_variant_hash(GDExtensionConstVariantPtr p_self) {
const Variant *self = (const Variant *)p_self;
return self->hash();
}
godot-core/src/builtin/callable.rs
Outdated
pub fn hash_u32(&self) -> u32 { | ||
self.as_inner() | ||
.hash() | ||
.try_into() | ||
.expect("Godot hashes are uin32_t") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I wondered if it would be worth it to put it into same macro (or even covering it directly in builtin), such as
macro_rules! impl_hash_32 {
(
$( #[$maybe_docs:meta] )*
$Type:ty
) => {
impl $Type {
#[doc=concat!("Returns a 32-bit integer hash value representing the ", stringify!($Type), " and its contents.")]
$( #[$maybe_docs] )*
pub fn hash_u32(&self) -> u32 {
self.as_inner()
.hash()
.try_into()
.expect("Godot hashes are uint32_t")
}
}
}
}
and called as
impl_hash_32!(
///
/// Note: Arrays with equal content will always produce identical hash values. However, the
/// reverse is not true. Returning identical hash values does not imply the arrays are equal,
/// because different arrays can have identical hash values due to hash collisions.
Array<T>
);
(...)
impl_hash_32!(StringName);
Since having one source for multiple instances of same code would be nice – but IMO it is not worth it getting into consideration all the differences (impl<T:ArrayElement> Array<T>
vs. concrete Type
). Ctrl+c ctrl+v ftw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give that a crack too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't sweat too much though (unless you'll find/figure out something nice); In this context repetition is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chucking it in as you suggested won't work because we'd end up with multiple impl
blocks for the same struct. I could define hash_u32
as the sole member of a GodotHash
trait, which we could then implement in built-ins, but then you would need to have the trait in scope whenever calling hash_u32
. This would be really annoying. I think I'll just leave this as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the macro not just define the fn
instead of the impl
? And then place it inside an existing impl
.
I don't see why we need traits here...
// The GDExtension interface only deals in `i64`, but the engine's own `hash()` function | ||
// actually returns `uint32_t`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would cut out this comment and move it to builtin Hash macro.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, outside of this one SAFETY
comment exposed in public docs
and just to confirm, in my view hash_u32
shortcut (avoiding conflict with Hash::hash
) is fine too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renames of public functions are breaking changes, as such it would have to wait for v0.5. We can merge it sooner if you provide the old methods with a deprecation (no docs, just "renamed to xy
" in deprecation notice).
/// @GlobalScope.hash() actually calls the VariantUtilityFunctions::hash(&Variant) function (cpp). | ||
/// This function calls the passed reference's `hash` method, which returns a uint32_t. | ||
/// Therefore, casting this function to u32 is always safe | ||
pub fn hash_u32(&self) -> u32 { | ||
unsafe { interface_fn!(variant_hash)(self.var_sys()) } | ||
.try_into() | ||
.expect("Godot hashes are uint32_t") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that this should be in comment rather than RustDoc.
Note that the int conversion isn't safety relevant, so shouldn't be part of // SAFETY:
. Making a mistake here will result in a worse hash function, not UB.
@Bromeon Added deprecated methods too. |
.expect("Godot hashes are uint32_t") | ||
} | ||
|
||
#[deprecated = "renamed to hash_32 and type changed to u32"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[deprecated = "renamed to hash_32 and type changed to u32"] | |
#[deprecated = "renamed to `hash_u32` and type changed to u32"] |
godot-core/src/builtin/callable.rs
Outdated
pub fn hash_u32(&self) -> u32 { | ||
self.as_inner() | ||
.hash() | ||
.try_into() | ||
.expect("Godot hashes are uin32_t") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the macro not just define the fn
instead of the impl
? And then place it inside an existing impl
.
I don't see why we need traits here...
godot-core/src/builtin/macros.rs
Outdated
// The GDExtension interface only deals in `i64`, but the engine's own `hash()` function | ||
// actually returns `uint32_t`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// The GDExtension interface only deals in `i64`, but the engine's own `hash()` function | |
// actually returns `uint32_t`. | |
// The GDExtension interface only deals in `int64_t`, but the engine's own `hash()` function | |
// actually returns `uint32_t`. |
hash()
-> hash_u32()
; add tests
@Bromeon Yes Ican do that. I was thinking of these other bulitin macros you guys have. |
Just writing docstring comments above a macro! call doesn't work
So in terms of making the appropriate docstrings available, I'm stuck between two solutions - neither of which I particularly like. The first is only implementing an
Which then lets you write
The other is accepting a docstring in the macro itself
What do you think, should I use any of these methods, is there another method I don't know about, or should we just live with the code duplication? |
(edited since it wasn't important, oops) As for details – I would like such macros to be self-contained, i.e. I would expect Macros which don't generate seperate impl (i.e. are not self contained) && are not more-or-less intuitive shortcuts (like Sometimes, non-intuitively, repetition is fine – strictly admitting to DRY is silly. It depends on what is simpler to grasp and/or ignore (meaning that, as long as it satisfies these two aforementioned principles, it is up to individual/collective taste. I'm not making it too simple, yeah). |
Not sure I agree, imo it's not mentally very challenging to call it Also note that separate impl blocks appear separated in RustDoc, so this implementation detail leaks into user docs. We sometimes accept this, but I'm still not sure if it's helpful to see an impl just with one |
We do it a lot in macros obviously, but haven't seen it in the rest of the code.
oooh, that's very valid point. Silly! In such a case I would go with repetition For example this one looks silly: make_hash_32_fn!(
/// Returns a 32-bit integer hash value representing the array and its contents.
///
/// Note: Arrays with equal content will always produce identical hash values. However, the
/// reverse is not true. Returning identical hash values does not imply the arrays are equal,
/// because different arrays can have identical hash values due to hash collisions.
); This one would be fine, but gains are minuscule 🤔 : /// Returns a 32-bit integer hash value representing ....
pub fn hash(&self) -> u32 {
builtin_hash_u32!()
} |
Although I think that a macro like this should be fine? Then we just call this within the pre-existing impl
|
Let's not bikeshed this too much. As mentioned, we already do this, just use the same approach: gdext/godot-core/src/builtin/string/gstring.rs Lines 244 to 260 in 245ef3a
|
Yes, ok. I’ll implement it this way shortly.
…On Wed 15. Oct 2025 at 16:33, Jan Haller ***@***.***> wrote:
*Bromeon* left a comment (godot-rust/gdext#1366)
<#1366 (comment)>
Let's not bikeshed this too much. As mentioned, we already do this, just
use the same approach:
https://github.com/godot-rust/gdext/blob/245ef3aa8248d6ed0a6902dc2de5e43979dd5fe9/godot-core/src/builtin/string/gstring.rs#L244-L260
—
Reply to this email directly, view it on GitHub
<#1366 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANBBS3RYPDMFD3GFXFYCW333XZLJ5AVCNFSM6AAAAACI7MHPICVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTIMBWG43DINRVHE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
I think this is finally it, what do you think? @Bromeon |
Please have some patience, only 3 days have passed since your previous message. Keep in mind that maintainer time is limited and there have been PRs with higher priority (e.g. bugs) over the course of the last couple of days. |
Sorry, wasn’t sure about tempo.
There was lots of activity at first (which I didn’t expect) so I figured
I’d strike while the iron is hot.
I’ll have patience ;)
…On Mon 20. Oct 2025 at 07:59, Jan Haller ***@***.***> wrote:
*Bromeon* left a comment (godot-rust/gdext#1366)
<#1366 (comment)>
Please have some patience, only 3 days have passed since your previous
message. Keep in mind that maintainer time is limited and there have been
PRs with higher priority (e.g. bugs) over the course of the last couple of
days.
—
Reply to this email directly, view it on GitHub
<#1366 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANBBS3TN3KOXIEEHUIF4AG33YR24PAVCNFSM6AAAAACI7MHPICVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTIMRQGY3TQNJWGI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
assert_eq!(v_first, v_second); | ||
assert_eq!(v_first.hash_u32(), v_second.hash_u32()); | ||
assert_ne!(v_first, v_third); | ||
assert_ne!(v_first.hash_u32(), v_third.hash_u32()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not guaranteed, just quite likely. The relation is:
(a == b) implies (a.hash() == b.hash())
and not
(a != b) implies (a.hash() != b.hash())
It's OK to keep this, but there should be a comment such as:
assert_ne!(v_first.hash_u32(), v_third.hash_u32()); | |
assert_ne!(v_first.hash_u32(), v_third.hash_u32()); // not guaranteed, but very likely. |
everywhere, also in other such tests.
godot-core/src/builtin/mod.rs
Outdated
pub use crate::gen::builtin_classes::*; | ||
} | ||
|
||
// common hashing macro implementer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment could as well be removed, the macro name itself contains more information than it 🙂
} | ||
|
||
/// Returns a 32-bit integer hash value representing the array and its contents. | ||
crate::declare_hash_u32_method! {/// Returns a 32-bit integer hash value representing the array and its contents. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crate::declare_hash_u32_method! {/// Returns a 32-bit integer hash value representing the array and its contents. | |
crate::declare_hash_u32_method! { | |
/// Returns a 32-bit integer hash value representing the array and its contents. |
also indentation of subsequent lines.
Thank you! Please squash the commits to 1, then it should be ready. |
37af2b0
to
e46bab9
Compare
I think by periodically merging in master I made a huge mess of everything (sorry about that, I thought it was rebasing). I will see if I can fix this absolute mess without fucking up the git history, if not I'll try again with a different branch. |
e46bab9
to
13923ef
Compare
Check the link in my message, as long as the total diff of all changes is fine, it's actually very easy to reduce to 1 commit 🙂 |
13923ef
to
0f32289
Compare
What I did was some brutally bad git moves (was doing some rebasing the way I normally do but yeah...). It's fine now, I got it back in order with some cherry-picks and shuffling. The changelist was pretty messed up before. I hope now all is well |
0f32289
to
b760f02
Compare
It looks like you accidentally reverted something, because CI passed in earlier iterations. The different hashes are mentioned in this issue, so you could always do a |
b760f02
to
22f56a0
Compare
22f56a0
to
fe630c4
Compare
Should be fine now, I did indeed accidentally revert some stuff in the chaos. Just checked check.sh on my machine locally, |
Aiming to fix issue #505