From 7b6cf42e4fab702026342661cbfb0b5f3d661b59 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Fri, 22 Nov 2024 15:50:49 +0100 Subject: [PATCH] geometry: require Ord for coordinates, to implement asserts in rectangles --- src/geometry.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/geometry.rs b/src/geometry.rs index 8583415..b6ea16f 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -3,14 +3,14 @@ use std::marker::PhantomData; pub trait CoordinateSpace {} -#[derive(PartialEq, Eq, Clone, Copy)] +#[derive(PartialEq, Eq, Clone, Copy, PartialOrd, Ord)] pub struct Coordinates, S: CoordinateSpace> { pub x: T, pub y: T, space: PhantomData, } -impl + Debug, S: CoordinateSpace> Debug for Coordinates { +impl + Debug + PartialOrd, S: CoordinateSpace> Debug for Coordinates { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_tuple("@") .field(&self.x) @@ -19,8 +19,8 @@ impl + Debug, S: CoordinateSpace> Debug for Coordinates } } -pub trait CoordLimits { - type Data: Sized; +pub trait CoordLimits: PartialOrd + PartialEq { + type Data: Sized + PartialOrd + PartialEq; const MIN: Self::Data; const MAX: Self::Data; } @@ -87,7 +87,7 @@ pub struct Coord8 { pub type VirtualCoordinates = Coordinates; pub type PhysicalCoordinates = Coordinates; -#[derive(PartialEq, Eq, Copy, Clone, Debug)] +#[derive(PartialEq, Eq, Copy, Clone, Debug, PartialOrd)] pub struct Rectangle, Space: CoordinateSpace> { pub top_left: Coordinates, pub bottom_right: Coordinates @@ -95,6 +95,8 @@ pub struct Rectangle, Space: Coordinate impl, Space: CoordinateSpace> Rectangle { pub fn new(top_left: Coordinates, bottom_right: Coordinates) -> Self { + assert!(top_left.x <= bottom_right.x); + assert!(top_left.y <= bottom_right.y); Self { top_left, bottom_right