#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use {
num::PrimInt,
std::{
fmt::Debug,
ops::{Add, Sub},
},
};
pub(crate) type Type<U> = (U, U);
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
pub struct Point<U> {
pub x: U, pub y: U, }
impl<U> Debug for Point<U>
where
U: PrimInt + Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}x{:?}", self.x, self.y)
}
}
impl<U> From<Type<U>> for Point<U>
where
U: PrimInt,
{
fn from((x, y): Type<U>) -> Self {
Self { x, y }
}
}
impl<U> From<&Type<U>> for Point<U>
where
U: PrimInt,
{
fn from((x, y): &Type<U>) -> Self {
Self { x: *x, y: *y }
}
}
impl<U> From<Point<U>> for Type<U>
where
U: PrimInt,
{
fn from(value: Point<U>) -> Self {
(value.x, value.y)
}
}
impl<U> Add for Point<U>
where
U: PrimInt,
{
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x().saturating_add(other.x()),
y: self.y().saturating_add(other.y()),
}
}
}
impl<U> Sub for Point<U>
where
U: PrimInt,
{
type Output = Self;
fn sub(self, other: Self) -> Self {
Self {
x: self.x().saturating_sub(other.x()),
y: self.y().saturating_sub(other.y()),
}
}
}
impl<U> Point<U>
where
U: PrimInt,
{
pub fn x(&self) -> U {
self.x
}
pub fn y(&self) -> U {
self.y
}
}