domain/utils/config.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#![allow(dead_code)] // Currently only used with certain features.
use core::cmp;
//------------ DefMinMax -----------------------------------------------------
/// The default, minimum, and maximum values for a config variable.
#[derive(Clone, Copy)]
pub struct DefMinMax<T> {
/// The default value,
def: T,
/// The minimum value,
min: T,
/// The maximum value,
max: T,
}
impl<T> DefMinMax<T> {
/// Creates a new value.
pub const fn new(def: T, min: T, max: T) -> Self {
Self { def, min, max }
}
/// Returns the default value.
pub fn default(self) -> T {
self.def
}
/// Trims the given value to fit into the minimum/maximum range, inclusive.
pub fn limit(self, value: T) -> T
where
T: Ord,
{
cmp::max(self.min, cmp::min(self.max, value))
}
}