#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
pub struct BoundedValue<T> {
pub min: T,
pub max: T,
}
impl<T> BoundedValue<T> {
pub const fn new(min: T, max: T) -> Self {
Self { min, max }
}
#[inline]
pub fn contains<V>(&self, value: V) -> bool
where
V: PartialOrd<T>,
{
(value >= self.min) && (value <= self.max)
}
}
impl<T> Debug for BoundedValue<T>
where
T: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BoundedValue")
.field("min", &self.min)
.field("max", &self.max)
.finish()
}
}
impl<T> Default for BoundedValue<T>
where
T: Default,
{
fn default() -> Self {
Self {
min: Default::default(),
max: Default::default(),
}
}
}
impl<T> Clone for BoundedValue<T>
where
T: Clone,
{
fn clone(&self) -> Self {
Self {
min: self.min.clone(),
max: self.max.clone(),
}
}
}
impl<T> Copy for BoundedValue<T> where T: Copy {}
impl<T> PartialEq for BoundedValue<T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
(self.min == other.min) && (self.max == other.max)
}
}
impl<T> Eq for BoundedValue<T> where T: Eq {}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BoundedValueWithDefault<T> {
pub min: T,
pub max: T,
pub default: T,
}
impl<T> BoundedValueWithDefault<T> {
pub const fn new(min: T, max: T, default: T) -> Self {
Self { min, max, default }
}
#[inline]
pub fn contains<V>(&self, value: V) -> bool
where
V: PartialOrd<T>,
{
(value >= self.min) && (value <= self.max)
}
}
impl<T> Debug for BoundedValueWithDefault<T>
where
T: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BoundedValueWithDefault")
.field("min", &self.min)
.field("max", &self.max)
.field("default", &self.default)
.finish()
}
}
impl<T> Default for BoundedValueWithDefault<T>
where
T: Default,
{
fn default() -> Self {
Self {
min: Default::default(),
max: Default::default(),
default: Default::default(),
}
}
}
impl<T> Clone for BoundedValueWithDefault<T>
where
T: Clone,
{
fn clone(&self) -> Self {
Self {
min: self.min.clone(),
max: self.max.clone(),
default: self.default.clone(),
}
}
}
impl<T> Copy for BoundedValueWithDefault<T> where T: Copy {}
impl<T> PartialEq for BoundedValueWithDefault<T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
(self.min == other.min) && (self.max == other.max) && (self.default == other.default)
}
}
impl<T> Eq for BoundedValueWithDefault<T> where T: Eq {}