Skip to content

Commit

Permalink
Implement Arbitrary for all remaining types. (#528)
Browse files Browse the repository at this point in the history
* `BoolVector2D`
* `BoolVector3D`
* `Box2D`
* `Box3D`
* `HomogeneousVector`
* `Length`
* `Point3D`
* `RigidTransform3D`
* `Rotation2D`
* `Rotation3D`
* `Size3D`
* `Translation3D`
* `Vector3D`
  • Loading branch information
kpreid authored Jul 15, 2024
1 parent 66f44de commit f0ff55d
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/box2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ impl<T: fmt::Debug, U> fmt::Debug for Box2D<T, U> {
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, U> arbitrary::Arbitrary<'a> for Box2D<T, U>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Box2D::new(
arbitrary::Arbitrary::arbitrary(u)?,
arbitrary::Arbitrary::arbitrary(u)?,
))
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, U> Zeroable for Box2D<T, U> {}

Expand Down
13 changes: 13 additions & 0 deletions src/box3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ impl<T: fmt::Debug, U> fmt::Debug for Box3D<T, U> {
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, U> arbitrary::Arbitrary<'a> for Box3D<T, U>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Box3D::new(
arbitrary::Arbitrary::arbitrary(u)?,
arbitrary::Arbitrary::arbitrary(u)?,
))
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, U> Zeroable for Box3D<T, U> {}

Expand Down
17 changes: 17 additions & 0 deletions src/homogen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ where
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, U> arbitrary::Arbitrary<'a> for HomogeneousVector<T, U>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let (x, y, z, w) = arbitrary::Arbitrary::arbitrary(u)?;
Ok(HomogeneousVector {
x,
y,
z,
w,
_unit: PhantomData,
})
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, U> Zeroable for HomogeneousVector<T, U> {}

Expand Down
10 changes: 10 additions & 0 deletions src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ where
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, U> arbitrary::Arbitrary<'a> for Length<T, U>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Length(arbitrary::Arbitrary::arbitrary(u)?, PhantomData))
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, U> Zeroable for Length<T, U> {}

Expand Down
16 changes: 16 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,22 @@ where
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, U> arbitrary::Arbitrary<'a> for Point3D<T, U>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let (x, y, z) = arbitrary::Arbitrary::arbitrary(u)?;
Ok(Point3D {
x,
y,
z,
_unit: PhantomData,
})
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, U> Zeroable for Point3D<T, U> {}

Expand Down
13 changes: 13 additions & 0 deletions src/rigid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ impl<T: Clone, Src, Dst> Clone for RigidTransform3D<T, Src, Dst> {
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, Src, Dst> arbitrary::Arbitrary<'a> for RigidTransform3D<T, Src, Dst>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(RigidTransform3D {
rotation: arbitrary::Arbitrary::arbitrary(u)?,
translation: arbitrary::Arbitrary::arbitrary(u)?,
})
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, Src, Dst> Zeroable for RigidTransform3D<T, Src, Dst> {}

Expand Down
24 changes: 24 additions & 0 deletions src/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ where
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, Src, Dst> arbitrary::Arbitrary<'a> for Rotation2D<T, Src, Dst>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Rotation2D::new(arbitrary::Arbitrary::arbitrary(u)?))
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, Src, Dst> Zeroable for Rotation2D<T, Src, Dst> {}

Expand Down Expand Up @@ -312,6 +322,20 @@ where
}
}

/// Note: the quaternions produced by this implementation are not normalized
/// (nor even necessarily finite). That is, this is not appropriate to use to
/// choose an actual “arbitrary rotation”, at least not without postprocessing.
#[cfg(feature = "arbitrary")]
impl<'a, T, Src, Dst> arbitrary::Arbitrary<'a> for Rotation3D<T, Src, Dst>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let (i, j, k, r) = arbitrary::Arbitrary::arbitrary(u)?;
Ok(Rotation3D::quaternion(i, j, k, r))
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, Src, Dst> Zeroable for Rotation3D<T, Src, Dst> {}

Expand Down
10 changes: 10 additions & 0 deletions src/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,16 @@ impl<T: NumCast, Src, Dst> Scale<T, Src, Dst> {
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, Src, Dst> arbitrary::Arbitrary<'a> for Scale<T, Src, Dst>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Scale::new(arbitrary::Arbitrary::arbitrary(u)?))
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, Src, Dst> Zeroable for Scale<T, Src, Dst> {}

Expand Down
16 changes: 16 additions & 0 deletions src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,22 @@ where
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, U> arbitrary::Arbitrary<'a> for Size3D<T, U>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let (width, height, depth) = arbitrary::Arbitrary::arbitrary(u)?;
Ok(Size3D {
width,
height,
depth,
_unit: PhantomData,
})
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, U> Zeroable for Size3D<T, U> {}

Expand Down
16 changes: 16 additions & 0 deletions src/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,22 @@ pub struct Translation3D<T, Src, Dst> {
pub _unit: PhantomData<(Src, Dst)>,
}

#[cfg(feature = "arbitrary")]
impl<'a, T, Src, Dst> arbitrary::Arbitrary<'a> for Translation3D<T, Src, Dst>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let (x, y, z) = arbitrary::Arbitrary::arbitrary(u)?;
Ok(Translation3D {
x,
y,
z,
_unit: PhantomData,
})
}
}

impl<T: Copy, Src, Dst> Copy for Translation3D<T, Src, Dst> {}

impl<T: Clone, Src, Dst> Clone for Translation3D<T, Src, Dst> {
Expand Down
37 changes: 37 additions & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,22 @@ where
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, U> arbitrary::Arbitrary<'a> for Vector3D<T, U>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let (x, y, z) = arbitrary::Arbitrary::arbitrary(u)?;
Ok(Vector3D {
x,
y,
z,
_unit: PhantomData,
})
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable, U> Zeroable for Vector3D<T, U> {}

Expand Down Expand Up @@ -2054,6 +2070,27 @@ impl BoolVector3D {
}
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for BoolVector2D {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(BoolVector2D {
x: arbitrary::Arbitrary::arbitrary(u)?,
y: arbitrary::Arbitrary::arbitrary(u)?,
})
}
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for BoolVector3D {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(BoolVector3D {
x: arbitrary::Arbitrary::arbitrary(u)?,
y: arbitrary::Arbitrary::arbitrary(u)?,
z: arbitrary::Arbitrary::arbitrary(u)?,
})
}
}

/// Convenience constructor.
#[inline]
pub const fn vec2<T, U>(x: T, y: T) -> Vector2D<T, U> {
Expand Down

0 comments on commit f0ff55d

Please sign in to comment.