Skip to content

Return Result from image::resize #19380

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

FlippinBerger
Copy link
Contributor

Objective

Return a Result when resizing an image so that we can return an Err when image.data is None.

Fixes #19376

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use labels May 26, 2025
@alice-i-cecile alice-i-cecile requested a review from Vrixyz May 26, 2025 20:30
@alice-i-cecile alice-i-cecile added M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 26, 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.

@@ -1505,6 +1510,12 @@ pub enum TextureAccessError {
WrongDimension,
}

#[derive(Error, Debug)]
pub enum ResizeError {
Copy link
Member

Choose a reason for hiding this comment

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

Missing docs here and for the enum variant. Doesn't need to be fancy.

@@ -173,7 +173,7 @@ pub fn update_viewport_render_target_size(
};
let image = images.get_mut(image_handle).unwrap();
if image.data.is_some() {
image.resize(size);
let _ = image.resize(size);
Copy link
Member

Choose a reason for hiding this comment

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

Comment here about why we're ignoring the Result please.

@@ -108,7 +108,7 @@ fn setup_camera(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
};

// Fill image.data with zeroes
canvas.resize(canvas_size);
let _ = canvas.resize(canvas_size);
Copy link
Member

Choose a reason for hiding this comment

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

I think this is probably better to bubble 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.

@alice-i-cecile How does bevy prefer to have errors bubble up at the system level? I see some other examples are simply hitting these with an .unwrap() to panic. Is that sufficient for the example folder?

For future knowledge, what's the go-to play in this scenario when we don't want the system to panic?

@@ -851,14 +851,19 @@ impl Image {

/// Resizes the image to the new size, by removing information or appending 0 to the `data`.
/// Does not properly scale the contents of the image.
pub fn resize(&mut self, size: Extent3d) {
self.texture_descriptor.size = size;
/// This method does nothing when self.data is None.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// This method does nothing when self.data is None.
/// This method does nothing and returns an error when `self.data` is `None`.

pub enum ResizeError {
/// Failed to resize an Image because it has no data.
#[error("unable to resize an image without data")]
ResizedWithoutData,
Copy link
Member

Choose a reason for hiding this comment

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

Nit nit:

ImageWithoutData ?

  • we're already in the context of a resize.
  • resized could be interpreted as an action which occured during the error, but it failed.

@@ -173,7 +173,8 @@ pub fn update_viewport_render_target_size(
};
let image = images.get_mut(image_handle).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't leave a direct code suggestion because it covers a deleted line, but these should just be

        let _ = images.get_mut(image_handle).unwrap().resize(size);

@SpecificProtagonist
Copy link
Contributor

I kinda disagree with the issue in the first place: Image::resize successfully resizes the image even if there's no CPU-side buffer (it still sets texture_descriptor.size in that case).

@alice-i-cecile
Copy link
Member

I kinda disagree with the issue in the first place: Image::resize successfully resizes the image even if there's no CPU-side buffer (it still sets texture_descriptor.size in that case).

Hmm, that's a good point. Maybe we should just be updating the documentation instead?

@alice-i-cecile alice-i-cecile added S-Needs-Design This issue requires design work to think about how it would best be accomplished and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Design This issue requires design work to think about how it would best be accomplished
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Return a Result from Image::resize
4 participants