use std::ops::{Div, Mul};
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(transparent)]
pub struct Volume(i32);
impl Volume {
pub fn approx_from_ratio(ratio: f32) -> Self {
Self::from((128. * ratio) as i32)
}
pub const fn as_value(&self) -> i32 {
self.0
}
pub fn as_ratio(&self) -> f32 {
(self.0 as f32) / 128.
}
}
impl From<i32> for Volume {
fn from(value: i32) -> Self {
Self(value)
}
}
impl Default for Volume {
fn default() -> Self {
Self::from(128)
}
}
impl Mul<f32> for Volume {
type Output = Self;
fn mul(self, rhs: f32) -> Self::Output {
Self::approx_from_ratio(self.as_ratio() * rhs)
}
}
impl Div<f32> for Volume {
type Output = Self;
fn div(self, rhs: f32) -> Self::Output {
Self::approx_from_ratio(self.as_ratio() / rhs)
}
}