mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
/// A width/height pair in (logical or physical) pixels.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, mln_wire::Wire)]
pub struct Size {
    pub width: u32,
    pub height: u32,
}

impl Size {
    #[must_use]
    pub const fn new(width: u32, height: u32) -> Self {
        Size { width, height }
    }

    /// The area, in square pixels.
    #[must_use]
    pub const fn area(&self) -> u64 {
        self.width as u64 * self.height as u64
    }

    #[must_use]
    pub fn aspect_ratio(&self) -> f32 {
        self.width as f32 / self.height as f32
    }

    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.width == 0 || self.height == 0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basics() {
        let s = Size::new(512, 256);
        assert_eq!(s.area(), 131_072);
        assert_eq!(s.aspect_ratio(), 2.0);
        assert!(!s.is_empty());
        assert!(Size::default().is_empty());
        assert!(Size::new(0, 5).is_empty());
        assert_eq!(Size::new(1, 2), Size::new(1, 2));
        assert_ne!(Size::new(1, 2), Size::new(2, 1));
    }
}