Skip to content

Assert that Vulkan array-getters return the same length#534

Merged
MarijnS95 merged 1 commit intomasterfrom
assert
Jan 5, 2022
Merged

Assert that Vulkan array-getters return the same length#534
MarijnS95 merged 1 commit intomasterfrom
assert

Conversation

@MarijnS95
Copy link
Copy Markdown
Collaborator

Originally introduced in #489 this inserts the array-length equality check everywhere else: in the supposedly invalid and inexistant event where Vulkan suddenly returns less items (count has been modified) than were originally queried through respective _len() functions (or more likely: a slice of invalid length passed by the user) and some elements at the end of the slice are left uninitialized, panic.

Wherever there is valid concern or possibility for this to happen - or to circumvent assertions and panics altogether - mutable references to mutable slices should be passed allowing the length to be promptly updated.

&mut count,
out.as_mut_ptr(),
);
assert_eq!(count, out.len() as u32);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use as _ ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the advantage of not being explicit about this (trivial to write) type?

That said, it's probably better to cast count to usize to prevent a possible truncation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am always wondering wether to use as _ or be explicit...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filnet There are probably no real API guidelines but it's probably better to be explicit unless intentionally hiding long type casts.

I've also turned on the trivial-casts and trivial-numereric-casts default-allow rustc warning locally to notice that there are quite a few casts to the same type, mostly as _ which was copied verbatim without the programmer really knowing or caring about the source and target type.

In either case we should aim to flip that warning on and make the code ever so slightly more readable by removing stray as casts.

That said, it's probably better to cast count to usize to prevent a possible truncation.

On second thought I did this by intention to match the truncation from let mut count = out.len() as u32;. However, that means - if someone manages to pass a slice longer than u32::max at all - this assert won't fail even if it should. Erring on the side of correctness, this is now flipped.

Copy link
Copy Markdown
Collaborator

@Ralith Ralith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built-in sanity-check asserts feels like it might be a bit opinionated compared to ash's status quo, but none of this is hotpath stuff anyway.

Originally introduced in [#489] this inserts the array-length equality
check everywhere else: in the supposedly invalid and inexistant event
where Vulkan suddenly returns less items (`count` has been modified)
than were originally queried through respective `_len()` functions (or
more likely: a slice of invalid length passed by the user) and some
elements at the end of the slice are left uninitialized, panic.

Wherever there is valid concern or possibility for this to happen - or
to circumvent assertions and panics altogether - mutable references to
mutable slices should be passed allowing the length to be promptly
updated.

[#489]: #489 (comment)
@MarijnS95 MarijnS95 merged commit 570b554 into master Jan 5, 2022
@MarijnS95 MarijnS95 deleted the assert branch January 5, 2022 23:17
MarijnS95 added a commit that referenced this pull request Jan 11, 2022


And drop the deprecated =/- markdown syntax from our readme: this is
analogous to #/## for a h1/h2 header, instead of defining a title and
(usually smaller font) subtitle or description.
MarijnS95 added a commit that referenced this pull request Jan 17, 2022


And drop the deprecated =/- markdown syntax from our readme: this is
analogous to #/## for a h1/h2 header, instead of defining a title and
(usually smaller font) subtitle or description.
MarijnS95 added a commit that referenced this pull request Jan 17, 2022


And drop the deprecated =/- markdown syntax from our readme: this is
analogous to #/## for a h1/h2 header, instead of defining a title and
(usually smaller font) subtitle or description.
MarijnS95 added a commit that referenced this pull request Jan 17, 2022
)

And drop the deprecated =/- markdown syntax from our readme: this is
analogous to #/## for a h1/h2 header, instead of defining a title and
(usually smaller font) subtitle or description.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants