Skip to content

Conversation

KirmesBude
Copy link
Contributor

Objective

Remove panics from image reinterpretation methods.

Solution

Introduce new errors and emit those instead of panicking.

Testing

  • Did you test these changes? If so, how?
    • CI + changed examples
  • Are there any parts that need more testing?
    • No
  • How can other people (reviewers) test your changes? Is there anything specific they need to know?
    • examples, no
  • If relevant, what platforms did you test these changes on, and are there any important ones you can't test?
    • linux, amd cpu, amd gpu

Moved out of #20536

Comment on lines 1753 to 1765
/// An error that occurs when reinterpreting the image.
#[derive(Error, Debug)]
pub enum TextureReinterpretationError {
#[error("incompatible sizes: old = {old:?} new = {new:?}")]
IncompatibleSizes { old: Extent3d, new: Extent3d },
#[error("must be a 2d image")]
WrongDimension,
#[error("must not already be a layered image")]
InvalidLayerCount,
#[error("can not evenly divide height = {height} by layers = {layers}")]
HeightNotDivisableByLayers { height: u32, layers: u32 },
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need some help on naming.

@IQuick143 IQuick143 added A-Rendering Drawing game state to the screen A-Assets Load files from disk to use for things like images, models, and sounds D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward S-Needs-Help The author needs help finishing this PR. labels Aug 30, 2025
@IQuick143 IQuick143 added C-Refinement Improves output quality, without fixing a clear bug or adding new functionality. M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Aug 30, 2025
Copy link
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes.

image.reinterpret_stacked_2d_as_array(image.height() / image.width());
image
.reinterpret_stacked_2d_as_array(image.height() / image.width())
.unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

This is a user-facing example. This should, at the minimum, use expect, and in the best case handle the error/bubble it up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It behaves the same way as before. Thought about adding a comment. Good call on using expect - let me know what you think

@@ -1741,6 +1750,19 @@ pub enum TranscodeFormat {
Rgb8,
}

/// An error that occurs when reinterpreting the image.
Copy link
Member

Choose a reason for hiding this comment

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

s/the/a

This should mention the functions that might return it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not familiar with what "s/the/a" means.
I was looking at the other Errors in this file and this looks the same to me. The other ones do not mention the functions either. Do you have an example from elsewhere in bevy that I can base this on?

Copy link
Contributor

Choose a reason for hiding this comment

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

"s/the/a" means subsitute (or search and replace) "the" by "a". It comes from sed or perl.

So it should be "a image" instead of "the image".

As a non-native speaker I would've used "an image" though, but that may be wrong.

Copy link
Member

Choose a reason for hiding this comment

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

I am not familiar with what "s/the/a" means.

Sorry about that, bad habit of mine. Should've made a suggestion instead.

As a non-native speaker I would've used "an image" though, but that may be wrong.

This would be grammatically correct.

self.texture_descriptor.size,
new_size
);
/// Changes the `size` if the total number of data elements (pixels) remains the same.
Copy link
Member

Choose a reason for hiding this comment

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

The docs here should mention what errors it might return.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just for my understanding: This makes sense here, because it returns TextureReinterpretationError, but will only ever construct one variant. Correct?
I will add it.

///
/// # Panics
/// Panics if the texture is not 2D, has more than one layers or is not evenly dividable into
/// the `layers`.
Copy link
Member

Choose a reason for hiding this comment

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

These should be changed into docs on what errors it might return.

Copy link
Contributor Author

@KirmesBude KirmesBude Aug 30, 2025

Choose a reason for hiding this comment

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

I did not see this with other functions. Here it could return every enum member. Should I still add something to the comment here?

Copy link
Member

Choose a reason for hiding this comment

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

We should be documenting errors and what cases might cause them, otherwise it may be difficult for consumers of these APIs to know how to handle them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well this lead me down a rabbit hole. I just kept the original comment and changed to # Errors. Apparently this is sometimes a thing in rust and even has a clippy lint.
Not sure if this is used anywhere in bevy at the moment, but the documentation concerning errors seem inconsistent anyways. Let me know if this is good now.

@KirmesBude KirmesBude changed the title Image reinterpretation methods no longer panic Image::reinterpret_size and Image::reinterpret_stacked_2d_as_array now return a Result Aug 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds A-Rendering Drawing game state to the screen C-Refinement Improves output quality, without fixing a clear bug or adding new functionality. D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Help The author needs help finishing this PR. S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants