geometry.rs

  1use core::fmt::Debug;
  2use derive_more::{Add, AddAssign, Div, Mul, Sub, SubAssign};
  3use refineable::Refineable;
  4use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign};
  5
  6#[derive(Refineable, Default, Add, AddAssign, Sub, SubAssign, Copy, Debug, PartialEq, Eq, Hash)]
  7#[refineable(debug)]
  8#[repr(C)]
  9pub struct Point<T: Clone + Debug> {
 10    pub x: T,
 11    pub y: T,
 12}
 13
 14pub fn point<T: Clone + Debug>(x: T, y: T) -> Point<T> {
 15    Point { x, y }
 16}
 17
 18impl<T: Clone + Debug> Point<T> {
 19    pub fn new(x: T, y: T) -> Self {
 20        Self { x, y }
 21    }
 22
 23    pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Point<U> {
 24        Point {
 25            x: f(self.x.clone()),
 26            y: f(self.y.clone()),
 27        }
 28    }
 29}
 30
 31impl Point<Pixels> {
 32    pub fn scale(&self, factor: f32) -> Point<ScaledPixels> {
 33        Point {
 34            x: self.x.scale(factor),
 35            y: self.y.scale(factor),
 36        }
 37    }
 38}
 39
 40impl<T, Rhs> Mul<Rhs> for Point<T>
 41where
 42    T: Mul<Rhs, Output = T> + Clone + Debug,
 43    Rhs: Clone + Debug,
 44{
 45    type Output = Point<T>;
 46
 47    fn mul(self, rhs: Rhs) -> Self::Output {
 48        Point {
 49            x: self.x * rhs.clone(),
 50            y: self.y * rhs,
 51        }
 52    }
 53}
 54
 55impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Point<T> {
 56    fn mul_assign(&mut self, rhs: S) {
 57        self.x = self.x.clone() * rhs.clone();
 58        self.y = self.y.clone() * rhs;
 59    }
 60}
 61
 62impl<T: Clone + Debug + Sub<Output = T>> SubAssign<Size<T>> for Point<T> {
 63    fn sub_assign(&mut self, rhs: Size<T>) {
 64        self.x = self.x.clone() - rhs.width;
 65        self.y = self.y.clone() - rhs.height;
 66    }
 67}
 68
 69impl<T: Clone + Debug + Add<Output = T> + Copy> AddAssign<T> for Point<T> {
 70    fn add_assign(&mut self, rhs: T) {
 71        self.x = self.x.clone() + rhs;
 72        self.y = self.y.clone() + rhs;
 73    }
 74}
 75
 76impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Point<T> {
 77    type Output = Self;
 78
 79    fn div(self, rhs: S) -> Self::Output {
 80        Self {
 81            x: self.x / rhs.clone(),
 82            y: self.y / rhs,
 83        }
 84    }
 85}
 86
 87impl<T: Clone + Debug + std::cmp::PartialOrd> Point<T> {
 88    pub fn max(&self, other: &Self) -> Self {
 89        Point {
 90            x: if self.x >= other.x {
 91                self.x.clone()
 92            } else {
 93                other.x.clone()
 94            },
 95            y: if self.y >= other.y {
 96                self.y.clone()
 97            } else {
 98                other.y.clone()
 99            },
100        }
101    }
102}
103
104impl<T: Clone + Debug> Clone for Point<T> {
105    fn clone(&self) -> Self {
106        Self {
107            x: self.x.clone(),
108            y: self.y.clone(),
109        }
110    }
111}
112
113#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq, Div, Hash)]
114#[refineable(debug)]
115#[repr(C)]
116pub struct Size<T: Clone + Debug> {
117    pub width: T,
118    pub height: T,
119}
120
121pub fn size<T: Clone + Debug>(width: T, height: T) -> Size<T> {
122    Size { width, height }
123}
124
125impl<T: Clone + Debug> Size<T> {
126    pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Size<U> {
127        Size {
128            width: f(self.width.clone()),
129            height: f(self.height.clone()),
130        }
131    }
132}
133
134impl Size<Pixels> {
135    pub fn scale(&self, factor: f32) -> Size<ScaledPixels> {
136        Size {
137            width: self.width.scale(factor),
138            height: self.height.scale(factor),
139        }
140    }
141}
142
143impl<T: Clone + Debug + Ord> Size<T> {
144    pub fn max(&self, other: &Self) -> Self {
145        Size {
146            width: if self.width >= other.width {
147                self.width.clone()
148            } else {
149                other.width.clone()
150            },
151            height: if self.height >= other.height {
152                self.height.clone()
153            } else {
154                other.height.clone()
155            },
156        }
157    }
158}
159
160impl<T, Rhs> Mul<Rhs> for Size<T>
161where
162    T: Mul<Rhs, Output = Rhs> + Debug + Clone,
163    Rhs: Debug + Clone,
164{
165    type Output = Size<Rhs>;
166
167    fn mul(self, rhs: Rhs) -> Self::Output {
168        Size {
169            width: self.width * rhs.clone(),
170            height: self.height * rhs,
171        }
172    }
173}
174
175impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Size<T> {
176    fn mul_assign(&mut self, rhs: S) {
177        self.width = self.width.clone() * rhs.clone();
178        self.height = self.height.clone() * rhs;
179    }
180}
181
182impl<T: Eq + Debug + Clone> Eq for Size<T> {}
183
184impl From<Size<Option<Pixels>>> for Size<Option<f32>> {
185    fn from(val: Size<Option<Pixels>>) -> Self {
186        Size {
187            width: val.width.map(|p| p.0 as f32),
188            height: val.height.map(|p| p.0 as f32),
189        }
190    }
191}
192
193impl Size<Length> {
194    pub fn full() -> Self {
195        Self {
196            width: relative(1.).into(),
197            height: relative(1.).into(),
198        }
199    }
200}
201
202impl Size<DefiniteLength> {
203    pub fn zero() -> Self {
204        Self {
205            width: px(0.).into(),
206            height: px(0.).into(),
207        }
208    }
209}
210
211impl Size<Length> {
212    pub fn auto() -> Self {
213        Self {
214            width: Length::Auto,
215            height: Length::Auto,
216        }
217    }
218}
219
220#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
221#[refineable(debug)]
222#[repr(C)]
223pub struct Bounds<T: Clone + Debug> {
224    pub origin: Point<T>,
225    pub size: Size<T>,
226}
227
228impl<T, Rhs> Mul<Rhs> for Bounds<T>
229where
230    T: Mul<Rhs, Output = Rhs> + Clone + Debug,
231    Point<T>: Mul<Rhs, Output = Point<Rhs>>,
232    Rhs: Clone + Debug,
233{
234    type Output = Bounds<Rhs>;
235
236    fn mul(self, rhs: Rhs) -> Self::Output {
237        Bounds {
238            origin: self.origin * rhs.clone(),
239            size: self.size * rhs,
240        }
241    }
242}
243
244impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Bounds<T> {
245    fn mul_assign(&mut self, rhs: S) {
246        self.origin *= rhs.clone();
247        self.size *= rhs;
248    }
249}
250
251impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Bounds<T>
252where
253    Size<T>: Div<S, Output = Size<T>>,
254{
255    type Output = Self;
256
257    fn div(self, rhs: S) -> Self {
258        Self {
259            origin: self.origin / rhs.clone(),
260            size: self.size / rhs,
261        }
262    }
263}
264
265impl<T: Clone + Debug + Add<T, Output = T>> Bounds<T> {
266    pub fn upper_right(&self) -> Point<T> {
267        Point {
268            x: self.origin.x.clone() + self.size.width.clone(),
269            y: self.origin.y.clone(),
270        }
271    }
272
273    pub fn lower_right(&self) -> Point<T> {
274        Point {
275            x: self.origin.x.clone() + self.size.width.clone(),
276            y: self.origin.y.clone() + self.size.height.clone(),
277        }
278    }
279}
280
281impl<T: Clone + Debug + PartialOrd + Add<T, Output = T>> Bounds<T> {
282    pub fn contains_point(&self, point: Point<T>) -> bool {
283        point.x >= self.origin.x
284            && point.x <= self.origin.x.clone() + self.size.width.clone()
285            && point.y >= self.origin.y
286            && point.y <= self.origin.y.clone() + self.size.height.clone()
287    }
288
289    pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Bounds<U> {
290        Bounds {
291            origin: self.origin.map(&f),
292            size: self.size.map(f),
293        }
294    }
295}
296
297impl Bounds<Pixels> {
298    pub fn scale(&self, factor: f32) -> Bounds<ScaledPixels> {
299        Bounds {
300            origin: self.origin.scale(factor),
301            size: self.size.scale(factor),
302        }
303    }
304}
305
306impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}
307
308#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
309#[refineable(debug)]
310#[repr(C)]
311pub struct Edges<T: Clone + Debug> {
312    pub top: T,
313    pub right: T,
314    pub bottom: T,
315    pub left: T,
316}
317
318impl<T: Clone + Debug + Mul<Output = T>> Mul for Edges<T> {
319    type Output = Self;
320
321    fn mul(self, rhs: Self) -> Self::Output {
322        Self {
323            top: self.top.clone() * rhs.top,
324            right: self.right.clone() * rhs.right,
325            bottom: self.bottom.clone() * rhs.bottom,
326            left: self.left.clone() * rhs.left,
327        }
328    }
329}
330
331impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Edges<T> {
332    fn mul_assign(&mut self, rhs: S) {
333        self.top = self.top.clone() * rhs.clone();
334        self.right = self.right.clone() * rhs.clone();
335        self.bottom = self.bottom.clone() * rhs.clone();
336        self.left = self.left.clone() * rhs.clone();
337    }
338}
339
340impl<T: Clone + Debug + Copy> Copy for Edges<T> {}
341
342impl<T: Clone + Debug> Edges<T> {
343    pub fn map<U: Clone + Debug, F: Fn(&T) -> U>(&self, f: F) -> Edges<U> {
344        Edges {
345            top: f(&self.top),
346            right: f(&self.right),
347            bottom: f(&self.bottom),
348            left: f(&self.left),
349        }
350    }
351
352    pub fn any<F: Fn(&T) -> bool>(&self, predicate: F) -> bool {
353        predicate(&self.top)
354            || predicate(&self.right)
355            || predicate(&self.bottom)
356            || predicate(&self.left)
357    }
358}
359
360impl Edges<Length> {
361    pub fn auto() -> Self {
362        Self {
363            top: Length::Auto,
364            right: Length::Auto,
365            bottom: Length::Auto,
366            left: Length::Auto,
367        }
368    }
369
370    pub fn zero() -> Self {
371        Self {
372            top: px(0.).into(),
373            right: px(0.).into(),
374            bottom: px(0.).into(),
375            left: px(0.).into(),
376        }
377    }
378}
379
380impl Edges<DefiniteLength> {
381    pub fn zero() -> Self {
382        Self {
383            top: px(0.).into(),
384            right: px(0.).into(),
385            bottom: px(0.).into(),
386            left: px(0.).into(),
387        }
388    }
389}
390
391impl Edges<AbsoluteLength> {
392    pub fn zero() -> Self {
393        Self {
394            top: px(0.).into(),
395            right: px(0.).into(),
396            bottom: px(0.).into(),
397            left: px(0.).into(),
398        }
399    }
400
401    pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
402        Edges {
403            top: self.top.to_pixels(rem_size),
404            right: self.right.to_pixels(rem_size),
405            bottom: self.bottom.to_pixels(rem_size),
406            left: self.left.to_pixels(rem_size),
407        }
408    }
409}
410
411#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
412#[refineable(debug)]
413#[repr(C)]
414pub struct Corners<T: Clone + Debug> {
415    pub top_left: T,
416    pub top_right: T,
417    pub bottom_right: T,
418    pub bottom_left: T,
419}
420
421impl<T: Clone + Debug> Corners<T> {
422    pub fn map<U: Clone + Debug, F: Fn(&T) -> U>(&self, f: F) -> Corners<U> {
423        Corners {
424            top_left: f(&self.top_left),
425            top_right: f(&self.top_right),
426            bottom_right: f(&self.bottom_right),
427            bottom_left: f(&self.bottom_left),
428        }
429    }
430}
431
432impl<T: Clone + Debug + Mul<Output = T>> Mul for Corners<T> {
433    type Output = Self;
434
435    fn mul(self, rhs: Self) -> Self::Output {
436        Self {
437            top_left: self.top_left.clone() * rhs.top_left,
438            top_right: self.top_right.clone() * rhs.top_right,
439            bottom_right: self.bottom_right.clone() * rhs.bottom_right,
440            bottom_left: self.bottom_left.clone() * rhs.bottom_left,
441        }
442    }
443}
444
445impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Corners<T> {
446    fn mul_assign(&mut self, rhs: S) {
447        self.top_left = self.top_left.clone() * rhs.clone();
448        self.top_right = self.top_right.clone() * rhs.clone();
449        self.bottom_right = self.bottom_right.clone() * rhs.clone();
450        self.bottom_left = self.bottom_left.clone() * rhs;
451    }
452}
453
454impl<T: Clone + Debug + Copy> Copy for Corners<T> {}
455
456#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
457#[repr(transparent)]
458pub struct Pixels(pub(crate) f32);
459
460impl Mul<f32> for Pixels {
461    type Output = Pixels;
462
463    fn mul(self, other: f32) -> Pixels {
464        Pixels(self.0 * other)
465    }
466}
467
468impl Mul<Pixels> for f32 {
469    type Output = Pixels;
470
471    fn mul(self, rhs: Pixels) -> Self::Output {
472        Pixels(self * rhs.0)
473    }
474}
475
476impl MulAssign<f32> for Pixels {
477    fn mul_assign(&mut self, other: f32) {
478        self.0 *= other;
479    }
480}
481
482impl Pixels {
483    pub fn round(&self) -> Self {
484        Self(self.0.round())
485    }
486
487    pub fn floor(&self) -> Self {
488        Self(self.0.floor())
489    }
490
491    pub fn scale(&self, factor: f32) -> ScaledPixels {
492        ScaledPixels(self.0 * factor)
493    }
494}
495
496impl Mul<Pixels> for Pixels {
497    type Output = Pixels;
498
499    fn mul(self, rhs: Pixels) -> Self::Output {
500        Pixels(self.0 * rhs.0)
501    }
502}
503
504impl Eq for Pixels {}
505
506impl Ord for Pixels {
507    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
508        self.0.partial_cmp(&other.0).unwrap()
509    }
510}
511
512impl std::hash::Hash for Pixels {
513    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
514        self.0.to_bits().hash(state);
515    }
516}
517
518impl From<f64> for Pixels {
519    fn from(val: f64) -> Self {
520        Pixels(val as f32)
521    }
522}
523
524impl From<f32> for Pixels {
525    fn from(val: f32) -> Self {
526        Pixels(val)
527    }
528}
529
530impl Debug for Pixels {
531    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
532        write!(f, "{} px", self.0)
533    }
534}
535
536impl From<Pixels> for f32 {
537    fn from(pixels: Pixels) -> Self {
538        pixels.0
539    }
540}
541
542impl From<&Pixels> for f32 {
543    fn from(pixels: &Pixels) -> Self {
544        pixels.0
545    }
546}
547
548impl From<Pixels> for f64 {
549    fn from(pixels: Pixels) -> Self {
550        pixels.0 as f64
551    }
552}
553
554#[derive(
555    Add,
556    AddAssign,
557    Clone,
558    Copy,
559    Debug,
560    Default,
561    Div,
562    Eq,
563    Hash,
564    Ord,
565    PartialEq,
566    PartialOrd,
567    Sub,
568    SubAssign,
569)]
570#[repr(transparent)]
571pub struct DevicePixels(pub(crate) i32);
572
573impl DevicePixels {
574    pub fn to_bytes(&self, bytes_per_pixel: u8) -> u32 {
575        self.0 as u32 * bytes_per_pixel as u32
576    }
577}
578
579impl From<DevicePixels> for i32 {
580    fn from(device_pixels: DevicePixels) -> Self {
581        device_pixels.0
582    }
583}
584
585impl From<i32> for DevicePixels {
586    fn from(val: i32) -> Self {
587        DevicePixels(val)
588    }
589}
590
591impl From<DevicePixels> for u64 {
592    fn from(device_pixels: DevicePixels) -> Self {
593        device_pixels.0 as u64
594    }
595}
596
597impl From<u64> for DevicePixels {
598    fn from(val: u64) -> Self {
599        DevicePixels(val as i32)
600    }
601}
602
603#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
604#[repr(transparent)]
605pub struct ScaledPixels(pub(crate) f32);
606
607impl Eq for ScaledPixels {}
608
609impl Debug for ScaledPixels {
610    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
611        write!(f, "{} px (scaled)", self.0)
612    }
613}
614
615impl From<ScaledPixels> for DevicePixels {
616    fn from(scaled: ScaledPixels) -> Self {
617        DevicePixels(scaled.0.ceil() as i32)
618    }
619}
620
621impl From<DevicePixels> for ScaledPixels {
622    fn from(device: DevicePixels) -> Self {
623        ScaledPixels(device.0 as f32)
624    }
625}
626
627#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
628pub struct Rems(f32);
629
630impl Mul<Pixels> for Rems {
631    type Output = Pixels;
632
633    fn mul(self, other: Pixels) -> Pixels {
634        Pixels(self.0 * other.0)
635    }
636}
637
638impl Debug for Rems {
639    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
640        write!(f, "{} rem", self.0)
641    }
642}
643
644#[derive(Clone, Copy, Debug)]
645pub enum AbsoluteLength {
646    Pixels(Pixels),
647    Rems(Rems),
648}
649
650impl AbsoluteLength {
651    pub fn is_zero(&self) -> bool {
652        match self {
653            AbsoluteLength::Pixels(px) => px.0 == 0.,
654            AbsoluteLength::Rems(rems) => rems.0 == 0.,
655        }
656    }
657}
658
659impl From<Pixels> for AbsoluteLength {
660    fn from(pixels: Pixels) -> Self {
661        AbsoluteLength::Pixels(pixels)
662    }
663}
664
665impl From<Rems> for AbsoluteLength {
666    fn from(rems: Rems) -> Self {
667        AbsoluteLength::Rems(rems)
668    }
669}
670
671impl AbsoluteLength {
672    pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
673        match self {
674            AbsoluteLength::Pixels(pixels) => *pixels,
675            AbsoluteLength::Rems(rems) => *rems * rem_size,
676        }
677    }
678}
679
680impl Default for AbsoluteLength {
681    fn default() -> Self {
682        px(0.).into()
683    }
684}
685
686/// A non-auto length that can be defined in pixels, rems, or percent of parent.
687#[derive(Clone, Copy)]
688pub enum DefiniteLength {
689    Absolute(AbsoluteLength),
690    /// A fraction of the parent's size between 0 and 1.
691    Fraction(f32),
692}
693
694impl DefiniteLength {
695    pub fn to_pixels(&self, base_size: AbsoluteLength, rem_size: Pixels) -> Pixels {
696        match self {
697            DefiniteLength::Absolute(size) => size.to_pixels(rem_size),
698            DefiniteLength::Fraction(fraction) => match base_size {
699                AbsoluteLength::Pixels(px) => px * *fraction,
700                AbsoluteLength::Rems(rems) => rems * rem_size * *fraction,
701            },
702        }
703    }
704}
705
706impl Debug for DefiniteLength {
707    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
708        match self {
709            DefiniteLength::Absolute(length) => Debug::fmt(length, f),
710            DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
711        }
712    }
713}
714
715impl From<Pixels> for DefiniteLength {
716    fn from(pixels: Pixels) -> Self {
717        Self::Absolute(pixels.into())
718    }
719}
720
721impl From<Rems> for DefiniteLength {
722    fn from(rems: Rems) -> Self {
723        Self::Absolute(rems.into())
724    }
725}
726
727impl From<AbsoluteLength> for DefiniteLength {
728    fn from(length: AbsoluteLength) -> Self {
729        Self::Absolute(length)
730    }
731}
732
733impl Default for DefiniteLength {
734    fn default() -> Self {
735        Self::Absolute(AbsoluteLength::default())
736    }
737}
738
739/// A length that can be defined in pixels, rems, percent of parent, or auto.
740#[derive(Clone, Copy)]
741pub enum Length {
742    Definite(DefiniteLength),
743    Auto,
744}
745
746impl Debug for Length {
747    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
748        match self {
749            Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
750            Length::Auto => write!(f, "auto"),
751        }
752    }
753}
754
755pub fn relative(fraction: f32) -> DefiniteLength {
756    DefiniteLength::Fraction(fraction).into()
757}
758
759/// Returns the Golden Ratio, i.e. `~(1.0 + sqrt(5.0)) / 2.0`.
760pub fn phi() -> DefiniteLength {
761    relative(1.61803398875)
762}
763
764pub fn rems(rems: f32) -> Rems {
765    Rems(rems)
766}
767
768pub fn px(pixels: f32) -> Pixels {
769    Pixels(pixels)
770}
771
772pub fn auto() -> Length {
773    Length::Auto
774}
775
776impl From<Pixels> for Length {
777    fn from(pixels: Pixels) -> Self {
778        Self::Definite(pixels.into())
779    }
780}
781
782impl From<Rems> for Length {
783    fn from(rems: Rems) -> Self {
784        Self::Definite(rems.into())
785    }
786}
787
788impl From<DefiniteLength> for Length {
789    fn from(length: DefiniteLength) -> Self {
790        Self::Definite(length)
791    }
792}
793
794impl From<AbsoluteLength> for Length {
795    fn from(length: AbsoluteLength) -> Self {
796        Self::Definite(length.into())
797    }
798}
799
800impl Default for Length {
801    fn default() -> Self {
802        Self::Definite(DefiniteLength::default())
803    }
804}
805
806impl From<()> for Length {
807    fn from(_: ()) -> Self {
808        Self::Definite(DefiniteLength::default())
809    }
810}
811
812pub trait IsZero {
813    fn is_zero(&self) -> bool;
814}
815
816impl IsZero for DevicePixels {
817    fn is_zero(&self) -> bool {
818        self.0 == 0
819    }
820}
821
822impl IsZero for ScaledPixels {
823    fn is_zero(&self) -> bool {
824        self.0 == 0.
825    }
826}
827
828impl IsZero for Pixels {
829    fn is_zero(&self) -> bool {
830        self.0 == 0.
831    }
832}
833
834impl IsZero for Rems {
835    fn is_zero(&self) -> bool {
836        self.0 == 0.
837    }
838}
839
840impl IsZero for AbsoluteLength {
841    fn is_zero(&self) -> bool {
842        match self {
843            AbsoluteLength::Pixels(pixels) => pixels.is_zero(),
844            AbsoluteLength::Rems(rems) => rems.is_zero(),
845        }
846    }
847}
848
849impl IsZero for DefiniteLength {
850    fn is_zero(&self) -> bool {
851        match self {
852            DefiniteLength::Absolute(length) => length.is_zero(),
853            DefiniteLength::Fraction(fraction) => *fraction == 0.,
854        }
855    }
856}
857
858impl IsZero for Length {
859    fn is_zero(&self) -> bool {
860        match self {
861            Length::Definite(length) => length.is_zero(),
862            Length::Auto => false,
863        }
864    }
865}
866
867impl<T: IsZero + Debug + Clone> IsZero for Point<T> {
868    fn is_zero(&self) -> bool {
869        self.x.is_zero() && self.y.is_zero()
870    }
871}
872
873impl<T: IsZero + Debug + Clone> IsZero for Size<T> {
874    fn is_zero(&self) -> bool {
875        self.width.is_zero() && self.height.is_zero()
876    }
877}
878
879impl<T: IsZero + Debug + Clone> IsZero for Bounds<T> {
880    fn is_zero(&self) -> bool {
881        self.origin.is_zero() && self.size.is_zero()
882    }
883}