1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::cmp::Ordering;
use std::ops::Mul;

use crate::traits::IntoComponents;
use crate::utils::vec_ord;
use crate::Point;

/// A width and a height measurement.
#[derive(Default, Clone, Copy, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Size<Unit> {
    /// The width component
    pub width: Unit,
    /// The height component
    pub height: Unit,
}

impl<Unit> Size<Unit> {
    /// Returns a new size of the given `width` and `height`.
    pub const fn new(width: Unit, height: Unit) -> Self {
        Self { width, height }
    }

    /// Returns a new size using `dimension` for both width and height.
    pub fn squared(dimension: Unit) -> Self
    where
        Unit: Clone,
    {
        Self {
            width: dimension.clone(),
            height: dimension,
        }
    }

    /// Returns the area of the rectangle.
    pub fn area(&self) -> <Unit as Mul>::Output
    where
        Unit: Mul + Copy,
    {
        self.width * self.height
    }

    /// Converts the contents of this size to `NewUnit` using [`From`].
    pub fn cast<NewUnit>(self) -> Size<NewUnit>
    where
        NewUnit: From<Unit>,
    {
        Size {
            width: self.width.into(),
            height: self.height.into(),
        }
    }

    /// Maps each component to `map` and returns a new value with the mapped
    /// components.
    #[must_use]
    pub fn map<NewUnit>(self, mut map: impl FnMut(Unit) -> NewUnit) -> Size<NewUnit> {
        Size {
            width: map(self.width),
            height: map(self.height),
        }
    }

    /// Converts the contents of this size to `NewUnit` using [`TryFrom`].
    ///
    /// # Errors
    ///
    /// Returns `<NewUnit as TryFrom>::Error` when the inner type cannot be
    /// converted. For this crate's types, this genenerally will be
    pub fn try_cast<NewUnit>(self) -> Result<Size<NewUnit>, NewUnit::Error>
    where
        NewUnit: TryFrom<Unit>,
    {
        Ok(Size {
            width: self.width.try_into()?,
            height: self.height.try_into()?,
        })
    }
}

impl<Unit> Ord for Size<Unit>
where
    Unit: Ord + Mul<Output = Unit> + Copy,
{
    fn cmp(&self, other: &Self) -> Ordering {
        vec_ord::<Unit>((*self).into_components(), (*other).into_components())
    }

    fn max(self, other: Self) -> Self
    where
        Self: Sized,
    {
        Self {
            width: self.width.max(other.width),
            height: self.height.max(other.height),
        }
    }

    fn min(self, other: Self) -> Self
    where
        Self: Sized,
    {
        Self {
            width: self.width.min(other.width),
            height: self.height.min(other.height),
        }
    }

    fn clamp(self, min: Self, max: Self) -> Self
    where
        Self: Sized,
    {
        Self {
            width: self.width.clamp(min.width, max.width),
            height: self.height.clamp(min.height, max.height),
        }
    }
}

impl<Unit> PartialOrd for Size<Unit>
where
    Unit: Ord + Mul<Output = Unit> + Copy,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl_2d_math!(Size, width, height);

impl<Unit> From<Size<Unit>> for Point<Unit> {
    fn from(value: Size<Unit>) -> Self {
        value.to_vec()
    }
}

impl<Unit> From<Point<Unit>> for Size<Unit> {
    fn from(value: Point<Unit>) -> Self {
        value.to_vec()
    }
}

#[cfg(feature = "wgpu")]
impl From<Size<crate::units::UPx>> for wgpu::Extent3d {
    fn from(value: Size<crate::units::UPx>) -> Self {
        Self {
            width: value.width.into(),
            height: value.height.into(),
            depth_or_array_layers: 1,
        }
    }
}

#[cfg(feature = "winit")]
impl From<winit::dpi::PhysicalSize<u32>> for Size<crate::units::UPx> {
    fn from(value: winit::dpi::PhysicalSize<u32>) -> Self {
        Self {
            width: value.width.try_into().expect("width too large"),
            height: value.height.try_into().expect("height too large"),
        }
    }
}

#[cfg(feature = "winit")]
impl From<winit::dpi::PhysicalSize<i32>> for Size<crate::units::Px> {
    fn from(value: winit::dpi::PhysicalSize<i32>) -> Self {
        Self {
            width: value.width.try_into().expect("width too large"),
            height: value.height.try_into().expect("height too large"),
        }
    }
}

#[cfg(feature = "winit")]
impl From<Size<crate::units::UPx>> for winit::dpi::PhysicalSize<u32> {
    fn from(size: Size<crate::units::UPx>) -> Self {
        Self {
            width: size.width.into(),
            height: size.height.into(),
        }
    }
}

#[cfg(feature = "winit")]
impl From<Size<crate::units::Px>> for winit::dpi::PhysicalSize<i32> {
    fn from(size: Size<crate::units::Px>) -> Self {
        Self {
            width: size.width.into(),
            height: size.height.into(),
        }
    }
}