From 1964e34d4ea5cd186954f684bd00c1be8b7973fe Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Sun, 22 Sep 2024 10:22:14 -0700 Subject: [PATCH] Revert incorrect `impl Arbitrary for Zoom`. I made this mistake in cfe2e932587ec38eed36f3fc957ba50b60f3bae4. We need the custom implementation to avoid generating out-of-range offsets. --- all-is-cubes/src/block/modifier/zoom.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/all-is-cubes/src/block/modifier/zoom.rs b/all-is-cubes/src/block/modifier/zoom.rs index 4d2914b91..4fa2f0bd9 100644 --- a/all-is-cubes/src/block/modifier/zoom.rs +++ b/all-is-cubes/src/block/modifier/zoom.rs @@ -13,7 +13,6 @@ use crate::universe; /// Design note: This is a struct separate from [`Modifier`] so that it can have a /// constructor accepting only valid bounds. #[derive(Clone, Debug, Eq, Hash, PartialEq)] -#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Zoom { /// Scale factor to zoom in by. scale: Resolution, @@ -155,6 +154,30 @@ impl universe::VisitHandles for Zoom { } } +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for Zoom { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + let scale = u.arbitrary()?; + let max_offset = GridCoordinate::from(scale) - 1; + Ok(Self::new( + scale, + GridPoint::new( + u.int_in_range(0..=max_offset)?, + u.int_in_range(0..=max_offset)?, + u.int_in_range(0..=max_offset)?, + ), + )) + } + + fn size_hint(depth: usize) -> (usize, Option) { + use arbitrary::{size_hint::and_all, Arbitrary}; + and_all(&[ + ::size_hint(depth), + <[GridCoordinate; 3] as Arbitrary>::size_hint(depth), + ]) + } +} + #[cfg(test)] mod tests { use super::*;