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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions crates/bevy_image/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,14 +851,20 @@ 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 returns an error when `self.data` is `None` indicating it
/// was not able to be resized.
pub fn resize(&mut self, size: Extent3d) -> Result<(), ResizeError> {
if let Some(ref mut data) = self.data {
self.texture_descriptor.size = size;
data.resize(
size.volume() * self.texture_descriptor.format.pixel_size(),
0,
);

return Ok(());
}

Err(ResizeError::ResizedWithoutData)
}

/// Changes the `size`, asserting that the total number of data elements (pixels) remains the
Expand Down Expand Up @@ -1505,6 +1511,14 @@ pub enum TextureAccessError {
WrongDimension,
}

/// An error that occurs when an image cannot be resized.
#[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.

/// 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.

}

/// An error that occurs when loading a texture.
#[derive(Error, Debug)]
pub enum TextureError {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ui/src/widget/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

if image.data.is_some() {
image.resize(size);
// Safe to ignore the Result here, because we know image has data.
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.

} else {
image.texture_descriptor.size = size;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/pixel_grid_snap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn setup_camera(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
};

// Fill image.data with zeroes
canvas.resize(canvas_size);
canvas.resize(canvas_size).unwrap();

let image_handle = images.add(canvas);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Image resizing now returns Result
pull_requests: [19380]
---
`bevy_image::Image::resize` now returns a `Result<(), ResizeError>` to allow for an error to be returned when`image.data` is `None` indicating that the operation was a no-op due to the lack of data to resize.

Before we were setting the `image.texture_descriptor.size` even if `image.data` was `None`. Now we only set this size if `image.data` exists.

To migrate code from previous versions all calls of `Image::reize` will need to check the new `Result` return value of the method and handle it however makes sense for the surrounding context.