Skip to main content

firmament/
size.rs

1use std::f32;
2
3/// An amount of space in 2 dimensions.
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct Size<T = f32> {
6    /// The width.
7    pub width: T,
8    /// The height.
9    pub height: T,
10}
11
12impl<T> Size<T> {
13    /// Creates a new  [`Size`] with the given width and height.
14    ///
15    /// [`Size`]: struct.Size.html
16    pub const fn new(width: T, height: T) -> Self {
17        Self { width, height }
18    }
19}
20
21impl Size {
22    /// A [`Size`] with zero width and height.
23    ///
24    /// [`Size`]: struct.Size.html
25    pub const ZERO: Self = Self::new(0., 0.);
26
27    /// A [`Size`] with a width and height of 1 unit.
28    ///
29    /// [`Size`]: struct.Size.html
30    pub const UNIT: Self = Self::new(1., 1.);
31
32    /// A [`Size`] with infinite width and height.
33    ///
34    /// [`Size`]: struct.Size.html
35    pub const INFINITY: Self = Self::new(f32::INFINITY, f32::INFINITY);
36
37    /// Increments the [`Size`] to account for the given padding.
38    ///
39    /// [`Size`]: struct.Size.html
40    pub fn pad(&self, padding: f32) -> Self {
41        Self {
42            width: self.width + padding * 2.0,
43            height: self.height + padding * 2.0,
44        }
45    }
46}
47
48impl From<[f32; 2]> for Size {
49    fn from([width, height]: [f32; 2]) -> Self {
50        Self { width, height }
51    }
52}
53
54impl From<[u16; 2]> for Size {
55    fn from([width, height]: [u16; 2]) -> Self {
56        Self::new(width.into(), height.into())
57    }
58}