proportion/src/proportion.rs

61 lines
1.4 KiB
Rust

use crate::Internal;
use std::{cmp::*, ops::*};
#[derive(Copy, Clone)]
pub struct Proportion<T: Internal> {
pub(crate) value: T,
}
impl<T: Internal> Proportion<T> {
pub fn new(value: T) -> Self {
Self { value }
}
pub fn set(&mut self, new_value: T) {
self.value = new_value;
}
pub fn is_zero(&self) -> bool {
self.value == T::MIN
}
pub fn is_one(&self) -> bool {
self.value == T::MAX }
}
impl<T: Internal> AsRef<T> for Proportion<T> {
fn as_ref(&self) -> &T {
&self.value
}
}
impl<T: Internal> Deref for Proportion<T> {
type Target = T;
fn deref(&self) -> &T {
&self.value
}
}
impl<T: Internal> PartialOrd for Proportion<T> {
fn partial_cmp(&self, other: &Proportion<T>) -> Option<Ordering> {
self.value.partial_cmp(&other.value)
}
}
impl<T: Internal> Ord for Proportion<T> {
fn cmp(&self, other: &Proportion<T>) -> Ordering {
self.value.cmp(&other.value)
}
}
impl<T: Internal> PartialEq for Proportion<T> {
fn eq(&self, other: &Proportion<T>) -> bool {
self.value.eq(&other.value)
}
}
impl<T: Internal> Eq for Proportion<T> {}
impl<T: Internal> std::fmt::Debug for Proportion<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "Proportion {{ {:?} / {:?} }}", self.value, Self::MAX)
}
}