diff --git a/crates/texture/src/lib.rs b/crates/texture/src/lib.rs index 78966f6ff..2aaef3e8b 100644 --- a/crates/texture/src/lib.rs +++ b/crates/texture/src/lib.rs @@ -134,9 +134,11 @@ pub struct Image { data: Arc>, } +/// Images are equal when their size, kind and floats are equal. Floats compare as `f32` does, so +/// an image holding NaN is unequal to every image, its clones included. impl PartialEq for Image { fn eq(&self, o: &Image) -> bool { - self.w == o.w && self.h == o.h && self.kind == o.kind && (Arc::ptr_eq(&self.data, &o.data) || self.data == o.data) + self.w == o.w && self.h == o.h && self.kind == o.kind && self.data == o.data } } @@ -739,6 +741,18 @@ mod tests { } } + #[test] + fn equality_does_not_depend_on_buffer_sharing() { + let img = Image::filled(2, 1, Kind::Gray, [f32::NAN; 4]); + let rebuilt = Image::filled(2, 1, Kind::Gray, [f32::NAN; 4]); + // NaN pixels compare unequal as floats do, whether or not the buffer is shared. + assert_ne!(img, rebuilt); + assert_ne!(img, img.clone()); + let finite = Image::filled(2, 1, Kind::Gray, [0.5; 4]); + assert_eq!(finite, finite.clone()); + assert_eq!(finite, Image::filled(2, 1, Kind::Gray, [0.5; 4])); + } + #[test] fn box_shrink_averages_large_hdr_values() { let img = Image::filled(2, 2, Kind::Color, [f32::MAX, -f32::MAX, 1.0, 1.0]);