trait Number -> trait Coordinate

This commit is contained in:
🪞👃🪞 2024-10-26 13:35:12 +03:00
parent 859306983f
commit a25548c39c
3 changed files with 58 additions and 58 deletions

View file

@ -10,7 +10,7 @@ pub trait Engine: Send + Sync + Sized {
/// Render target /// Render target
type Output: Output<Self>; type Output: Output<Self>;
/// Unit of length /// Unit of length
type Unit: Number; type Unit: Coordinate;
/// Rectangle without offset /// Rectangle without offset
type Size: Size<Self::Unit> + From<[Self::Unit;2]> + Debug + Copy; type Size: Size<Self::Unit> + From<[Self::Unit;2]> + Debug + Copy;
/// Rectangle with offset /// Rectangle with offset

View file

@ -54,35 +54,3 @@ pub type Usually<T> = Result<T, Box<dyn Error>>;
/// Standard optional result type. /// Standard optional result type.
pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>; pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
/// Standard numeric type.
pub trait Number: Send + Sync + Copy
+ Add<Self, Output=Self>
+ Sub<Self, Output=Self>
+ Mul<Self, Output=Self>
+ Div<Self, Output=Self>
+ Ord + PartialEq + Eq
+ Debug + Display + Default
+ From<u16> + Into<u16>
+ Into<f64>
{
fn minus (self, other: Self) -> Self {
if self >= other {
self - other
} else {
0.into()
}
}
}
impl<T> Number for T where
T: Send + Sync + Copy
+ Add<Self, Output=Self>
+ Sub<Self, Output=Self>
+ Mul<Self, Output=Self>
+ Div<Self, Output=Self>
+ Ord + PartialEq + Eq
+ Debug + Display + Default
+ From<u16> + Into<u16>
+ Into<f64>
{}

View file

@ -1,5 +1,37 @@
use crate::*; use crate::*;
/// Standard numeric type.
pub trait Coordinate: Send + Sync + Copy
+ Add<Self, Output=Self>
+ Sub<Self, Output=Self>
+ Mul<Self, Output=Self>
+ Div<Self, Output=Self>
+ Ord + PartialEq + Eq
+ Debug + Display + Default
+ From<u16> + Into<u16>
+ Into<f64>
{
fn minus (self, other: Self) -> Self {
if self >= other {
self - other
} else {
0.into()
}
}
}
impl<T> Coordinate for T where
T: Send + Sync + Copy
+ Add<Self, Output=Self>
+ Sub<Self, Output=Self>
+ Mul<Self, Output=Self>
+ Div<Self, Output=Self>
+ Ord + PartialEq + Eq
+ Debug + Display + Default
+ From<u16> + Into<u16>
+ Into<f64>
{}
pub struct FixedAxis<T> { pub struct FixedAxis<T> {
pub start: T, pub start: T,
pub point: Option<T>, pub point: Option<T>,
@ -39,7 +71,7 @@ impl_axis_common!(ScaledAxis usize);
// TODO: return impl Point and impl Size instead of [N;x] // TODO: return impl Point and impl Size instead of [N;x]
// to disambiguate between usage of 2-"tuple"s // to disambiguate between usage of 2-"tuple"s
pub trait Size<N: Number> { pub trait Size<N: Coordinate> {
fn x (&self) -> N; fn x (&self) -> N;
fn y (&self) -> N; fn y (&self) -> N;
#[inline] fn w (&self) -> N { self.x() } #[inline] fn w (&self) -> N { self.x() }
@ -56,17 +88,17 @@ pub trait Size<N: Number> {
} }
} }
impl<N: Number> Size<N> for (N, N) { impl<N: Coordinate> Size<N> for (N, N) {
fn x (&self) -> N { self.0 } fn x (&self) -> N { self.0 }
fn y (&self) -> N { self.1 } fn y (&self) -> N { self.1 }
} }
impl<N: Number> Size<N> for [N;2] { impl<N: Coordinate> Size<N> for [N;2] {
fn x (&self) -> N { self[0] } fn x (&self) -> N { self[0] }
fn y (&self) -> N { self[1] } fn y (&self) -> N { self[1] }
} }
pub trait Area<N: Number>: Copy { pub trait Area<N: Coordinate>: Copy {
fn x (&self) -> N; fn x (&self) -> N;
fn y (&self) -> N; fn y (&self) -> N;
fn w (&self) -> N; fn w (&self) -> N;
@ -117,14 +149,14 @@ pub trait Area<N: Number>: Copy {
} }
} }
impl<N: Number> Area<N> for (N, N, N, N) { impl<N: Coordinate> Area<N> for (N, N, N, N) {
#[inline] fn x (&self) -> N { self.0 } #[inline] fn x (&self) -> N { self.0 }
#[inline] fn y (&self) -> N { self.1 } #[inline] fn y (&self) -> N { self.1 }
#[inline] fn w (&self) -> N { self.2 } #[inline] fn w (&self) -> N { self.2 }
#[inline] fn h (&self) -> N { self.3 } #[inline] fn h (&self) -> N { self.3 }
} }
impl<N: Number> Area<N> for [N;4] { impl<N: Coordinate> Area<N> for [N;4] {
#[inline] fn x (&self) -> N { self[0] } #[inline] fn x (&self) -> N { self[0] }
#[inline] fn y (&self) -> N { self[1] } #[inline] fn y (&self) -> N { self[1] }
#[inline] fn w (&self) -> N { self[2] } #[inline] fn w (&self) -> N { self[2] }
@ -338,7 +370,7 @@ impl<T> Align<T> {
} }
} }
fn align<T, N: Number, R: Area<N> + From<[N;4]>> (align: &Align<T>, outer: R, inner: R) -> Option<R> { fn align<T, N: Coordinate, R: Area<N> + From<[N;4]>> (align: &Align<T>, outer: R, inner: R) -> Option<R> {
if outer.w() < inner.w() || outer.h() < inner.h() { if outer.w() < inner.w() || outer.h() < inner.h() {
None None
} else { } else {
@ -381,7 +413,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Align<T> {
} }
/// Enforce fixed size of drawing area /// Enforce fixed size of drawing area
pub enum Fixed<U: Number, T> { pub enum Fixed<U: Coordinate, T> {
/// Enforce fixed width /// Enforce fixed width
X(U, T), X(U, T),
/// Enforce fixed height /// Enforce fixed height
@ -389,7 +421,7 @@ pub enum Fixed<U: Number, T> {
/// Enforce fixed width and height /// Enforce fixed width and height
XY(U, U, T), XY(U, U, T),
} }
impl<N: Number, T> Fixed<N, T> { impl<N: Coordinate, T> Fixed<N, T> {
pub fn inner (&self) -> &T { pub fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
} }
@ -417,7 +449,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Fixed<E::Unit, T> {
} }
/// Enforce minimum size of drawing area /// Enforce minimum size of drawing area
pub enum Min<U: Number, T> { pub enum Min<U: Coordinate, T> {
/// Enforce minimum width /// Enforce minimum width
X(U, T), X(U, T),
/// Enforce minimum height /// Enforce minimum height
@ -425,7 +457,7 @@ pub enum Min<U: Number, T> {
/// Enforce minimum width and height /// Enforce minimum width and height
XY(U, U, T), XY(U, U, T),
} }
impl<N: Number, T> Min<N, T> { impl<N: Coordinate, T> Min<N, T> {
pub fn inner (&self) -> &T { pub fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
} }
@ -448,7 +480,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Min<E::Unit, T> {
} }
/// Enforce maximum size of drawing area /// Enforce maximum size of drawing area
pub enum Max<U: Number, T> { pub enum Max<U: Coordinate, T> {
/// Enforce maximum width /// Enforce maximum width
X(U, T), X(U, T),
/// Enforce maximum height /// Enforce maximum height
@ -457,7 +489,7 @@ pub enum Max<U: Number, T> {
XY(U, U, T), XY(U, U, T),
} }
impl<N: Number, T> Max<N, T> { impl<N: Coordinate, T> Max<N, T> {
fn inner (&self) -> &T { fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
} }
@ -480,7 +512,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
} }
/// Expand drawing area /// Expand drawing area
pub enum Grow<N: Number, T> { pub enum Grow<N: Coordinate, T> {
/// Increase width /// Increase width
X(N, T), X(N, T),
/// Increase height /// Increase height
@ -489,7 +521,7 @@ pub enum Grow<N: Number, T> {
XY(N, N, T) XY(N, N, T)
} }
impl<N: Number, T> Grow<N, T> { impl<N: Coordinate, T> Grow<N, T> {
fn inner (&self) -> &T { fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
} }
@ -512,7 +544,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Grow<E::Unit, T> {
} }
/// Shrink drawing area /// Shrink drawing area
pub enum Shrink<N: Number, T> { pub enum Shrink<N: Coordinate, T> {
/// Decrease width /// Decrease width
X(N, T), X(N, T),
/// Decrease height /// Decrease height
@ -521,7 +553,7 @@ pub enum Shrink<N: Number, T> {
XY(N, N, T), XY(N, N, T),
} }
impl<N: Number, T: Widget> Shrink<N, T> { impl<N: Coordinate, T: Widget> Shrink<N, T> {
fn inner (&self) -> &T { fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
} }
@ -553,7 +585,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Shrink<E::Unit, T> {
} }
/// Shrink from each side /// Shrink from each side
pub enum Inset<N: Number, T> { pub enum Inset<N: Coordinate, T> {
/// Decrease width /// Decrease width
X(N, T), X(N, T),
/// Decrease height /// Decrease height
@ -562,14 +594,14 @@ pub enum Inset<N: Number, T> {
XY(N, N, T), XY(N, N, T),
} }
impl<N: Number, T: Widget> Inset<N, T> { impl<N: Coordinate, T: Widget> Inset<N, T> {
pub fn inner (&self) -> &T { pub fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
} }
} }
/// Grow on each side /// Grow on each side
pub enum Outset<N: Number, T> { pub enum Outset<N: Coordinate, T> {
/// Increase width /// Increase width
X(N, T), X(N, T),
/// Increase height /// Increase height
@ -578,7 +610,7 @@ pub enum Outset<N: Number, T> {
XY(N, N, T), XY(N, N, T),
} }
impl<N: Number, T: Widget> Outset<N, T> { impl<N: Coordinate, T: Widget> Outset<N, T> {
pub fn inner (&self) -> &T { pub fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
} }
@ -623,7 +655,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Outset<E::Unit, T> {
} }
/// Move origin point of drawing area /// Move origin point of drawing area
pub enum Push<N: Number, T: Widget> { pub enum Push<N: Coordinate, T: Widget> {
/// Move origin to the right /// Move origin to the right
X(N, T), X(N, T),
/// Move origin downwards /// Move origin downwards
@ -632,7 +664,7 @@ pub enum Push<N: Number, T: Widget> {
XY(N, N, T), XY(N, N, T),
} }
impl<N: Number, T: Widget> Push<N, T> { impl<N: Coordinate, T: Widget> Push<N, T> {
pub fn inner (&self) -> &T { pub fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
} }
@ -661,7 +693,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Push<E::Unit, T> {
} }
/// Move origin point of drawing area /// Move origin point of drawing area
pub enum Pull<N: Number, T: Widget> { pub enum Pull<N: Coordinate, T: Widget> {
/// Move origin to the right /// Move origin to the right
X(N, T), X(N, T),
/// Move origin downwards /// Move origin downwards
@ -670,7 +702,7 @@ pub enum Pull<N: Number, T: Widget> {
XY(N, N, T), XY(N, N, T),
} }
impl<N: Number, T: Widget> Pull<N, T> { impl<N: Coordinate, T: Widget> Pull<N, T> {
pub fn inner (&self) -> &T { pub fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
} }