-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Changes from all commits
12fc0bc
873a7b6
bb130d6
7689635
82a1ef8
10fc050
ca22396
79d2527
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -1505,6 +1511,14 @@ pub enum TextureAccessError { | |
WrongDimension, | ||
} | ||
|
||
/// An error that occurs when an image cannot be resized. | ||
#[derive(Error, Debug)] | ||
pub enum ResizeError { | ||
/// Failed to resize an Image because it has no data. | ||
#[error("unable to resize an image without data")] | ||
ResizedWithoutData, | ||
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. Nit nit:
|
||
} | ||
|
||
/// An error that occurs when loading a texture. | ||
#[derive(Error, Debug)] | ||
pub enum TextureError { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,7 +173,8 @@ pub fn update_viewport_render_target_size( | |
}; | ||
let image = images.get_mut(image_handle).unwrap(); | ||
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. 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); | ||
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. Comment here about why we're ignoring the Result please. |
||
} else { | ||
image.texture_descriptor.size = size; | ||
} | ||
|
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. |
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.
Missing docs here and for the enum variant. Doesn't need to be fancy.