From 47baffee9d7f74c061103a1ab79176dcf08a1cb8 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 19:55:39 -0800 Subject: [PATCH] craft-texture: compare images by value whether or not the buffer is shared Image equality no longer short-cuts on a shared buffer. An image holding NaN compared equal to its clone but unequal to the same image built again; it is now unequal to both, as f32 comparison gives. --- crates/texture/src/lib.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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]);