Files
sigils/src/bounded.rs

62 lines
1.5 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: Apache-2.0
// Sealed with Magistamp.
/// `Bounded` provides the inclusive minimum and maximum range limits
/// for a numeric type.
///
/// Rust types have actual upper and lower representation limits that don't
/// follow the mathematical definitions. For example a Natural number goes
/// from one to infinity but a u8 can only really represent a number between
/// zero and two hundred and fifty five. This let's use set the minimum and
/// maximum values a Rust type can represent so we can use them in code easily.
pub trait Bounded
{
/// The minimum value for this type.
///
/// # Examples
/// ```rust
/// use sigils::Bounded;
///
/// let min = <u8 as Bounded>::MIN;
///
/// assert_eq!(min, u8::MIN);
/// ```
const MIN: Self;
/// The maximum value for this type.
///
/// # Examples
/// ```rust
/// use sigils::Bounded;
///
/// let max = <f64 as Bounded>::MAX;
///
/// assert_eq!(max, f64::MAX);
/// ```
const MAX: Self;
}
/// A macro for making implementation of
/// the Bounded trait easier.
macro_rules! bounded_trait_impl
{
($($T:ty),*) =>
{
$(
impl Bounded for $T
{
const MIN: $T = <$T>::MIN;
const MAX: $T = <$T>::MAX;
}
)*
}
}
// Implement the Bounded for all the primitive types.
bounded_trait_impl!(u8, u16, u32, u64, u128, usize);
bounded_trait_impl!(i8, i16, i32, i64, i128, isize);
bounded_trait_impl!(f32, f64);