1use std::fmt;
4
5#[repr(C)]
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub struct CGSize {
21 pub width: f64,
22 pub height: f64,
23}
24
25impl std::hash::Hash for CGSize {
26 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
27 self.width.to_bits().hash(state);
28 self.height.to_bits().hash(state);
29 }
30}
31
32impl Eq for CGSize {}
33
34impl CGSize {
35 #[must_use]
46 pub const fn new(width: f64, height: f64) -> Self {
47 Self { width, height }
48 }
49
50 #[must_use]
61 pub const fn zero() -> Self {
62 Self::new(0.0, 0.0)
63 }
64
65 #[must_use]
67 pub const fn area(&self) -> f64 {
68 self.width * self.height
69 }
70
71 #[must_use]
73 pub fn aspect_ratio(&self) -> f64 {
74 if self.height == 0.0 {
75 0.0
76 } else {
77 self.width / self.height
78 }
79 }
80
81 #[allow(clippy::float_cmp)]
84 #[must_use]
85 pub const fn is_square(&self) -> bool {
86 self.width == self.height
87 }
88
89 #[must_use]
90 pub fn is_empty(&self) -> bool {
91 self.width <= 0.0 || self.height <= 0.0
92 }
93
94 #[must_use]
96 pub const fn is_null(&self) -> bool {
97 self.width == 0.0 && self.height == 0.0
98 }
99}
100
101impl Default for CGSize {
102 fn default() -> Self {
103 Self::zero()
104 }
105}
106
107impl fmt::Display for CGSize {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 write!(f, "{}x{}", self.width, self.height)
110 }
111}