use crate::*; use std::fmt::Debug; pub trait Size: From<[N;2]> + Debug + Copy { fn x (&self) -> N; fn y (&self) -> N; #[inline] fn w (&self) -> N { self.x() } #[inline] fn h (&self) -> N { self.y() } #[inline] fn wh (&self) -> [N;2] { [self.x(), self.y()] } #[inline] fn clip_w (&self, w: N) -> [N;2] { [self.w().min(w), self.h()] } #[inline] fn clip_h (&self, h: N) -> [N;2] { [self.w(), self.h().min(h)] } #[inline] fn expect_min (&self, w: N, h: N) -> Usually<&Self> { if self.w() < w || self.h() < h { Err(format!("min {w}x{h}").into()) } else { Ok(self) } } #[inline] fn zero () -> [N;2] { [N::zero(), N::zero()] } #[inline] fn to_area_pos (&self) -> [N;4] { let [x, y] = self.wh(); [x, y, 0.into(), 0.into()] } #[inline] fn to_area_size (&self) -> [N;4] { let [w, h] = self.wh(); [0.into(), 0.into(), w, h] } } impl Size for (N, N) { #[inline] fn x (&self) -> N { self.0 } #[inline] fn y (&self) -> N { self.1 } } impl Size for [N;2] { #[inline] fn x (&self) -> N { self[0] } #[inline] fn y (&self) -> N { self[1] } }