Skip to content

Commit

Permalink
Revert incorrect impl Arbitrary for Zoom.
Browse files Browse the repository at this point in the history
I made this mistake in cfe2e93.
We need the custom implementation to avoid generating out-of-range
offsets.
  • Loading branch information
kpreid committed Sep 22, 2024
1 parent af2835d commit 1964e34
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion all-is-cubes/src/block/modifier/zoom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Self> {
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<usize>) {
use arbitrary::{size_hint::and_all, Arbitrary};
and_all(&[
<Resolution as Arbitrary>::size_hint(depth),
<[GridCoordinate; 3] as Arbitrary>::size_hint(depth),
])
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 1964e34

Please sign in to comment.