-
-
Notifications
You must be signed in to change notification settings - Fork 251
Checked cpp and commented detailing why all godot hashes are hash_u32. Renamed and added 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?
Checked cpp and commented detailing why all godot hashes are hash_u32. Renamed and added tests #1366
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,10 +290,13 @@ impl<T: ArrayElement> Array<T> { | |
/// 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. | ||
pub fn hash(&self) -> u32 { | ||
pub fn hash_u32(&self) -> u32 { | ||
// The GDExtension interface only deals in `i64`, but the engine's own `hash()` function | ||
// actually returns `uint32_t`. | ||
Comment on lines
294
to
295
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
self.as_inner().hash().try_into().unwrap() | ||
self.as_inner() | ||
.hash() | ||
.try_into() | ||
.expect("Godot hashes are uint32_t") | ||
} | ||
|
||
/// Returns the first element in the array, or `None` if the array is empty. | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -310,8 +310,13 @@ impl Variant { | |||||||||||||||||||||||||||||||||
/// Return Godot's hash value for the variant. | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// _Godot equivalent : `@GlobalScope.hash()`_ | ||||||||||||||||||||||||||||||||||
pub fn hash(&self) -> i64 { | ||||||||||||||||||||||||||||||||||
/// @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") | ||||||||||||||||||||||||||||||||||
Comment on lines
+313
to
+319
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Suggested change
idk if we should keep info about There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 static GDExtensionInt gdextension_variant_hash(GDExtensionConstVariantPtr p_self) {
const Variant *self = (const Variant *)p_self;
return self->hash();
} |
||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/// Interpret the `Variant` as `bool`. | ||||||||||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
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
and called as
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. concreteType
). Ctrl+c ctrl+v ftwThere 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.
Uh oh!
There was an error while loading. Please reload this page.
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.