use core::num::NonZeroU32;
use mynd_math::{checked_mul_u64, checked_u64_to_usize};
use crate::{GeometryError, GeometryResult};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Dimensions {
width: NonZeroU32,
height: NonZeroU32,
pixel_count: u64,
}
impl Dimensions {
pub const fn new(width: u32, height: u32) -> GeometryResult<Self> {
let width = match NonZeroU32::new(width) {
Some(width) => width,
None => return Err(GeometryError::ZeroWidth),
};
let height = match NonZeroU32::new(height) {
Some(height) => height,
None => return Err(GeometryError::ZeroHeight),
};
let pixel_count = match checked_mul_u64(width.get() as u64, height.get() as u64) {
Ok(pixel_count) => pixel_count,
Err(error) => return Err(GeometryError::Arithmetic(error)),
};
Ok(Self {
width,
height,
pixel_count,
})
}
#[must_use]
pub const fn width(self) -> u32 {
self.width.get()
}
#[must_use]
pub const fn height(self) -> u32 {
self.height.get()
}
#[must_use]
pub const fn pixel_count(self) -> u64 {
self.pixel_count
}
pub fn pixel_count_usize(self) -> GeometryResult<usize> {
checked_u64_to_usize(self.pixel_count).map_err(GeometryError::Arithmetic)
}
}