2025-07-29 12:38:11 -04:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
// Sealed with Magistamp.
|
|
|
|
|
|
2026-02-12 19:21:53 -05:00
|
|
|
/// `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.
|
2025-03-10 12:17:37 -04:00
|
|
|
pub trait Bounded
|
|
|
|
|
{
|
|
|
|
|
/// The minimum value for this type.
|
2026-02-12 19:21:53 -05:00
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// use sigils::Bounded;
|
|
|
|
|
///
|
|
|
|
|
/// let min = <u8 as Bounded>::MIN;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(min, u8::MIN);
|
|
|
|
|
/// ```
|
2025-03-10 12:17:37 -04:00
|
|
|
const MIN: Self;
|
|
|
|
|
|
|
|
|
|
/// The maximum value for this type.
|
2026-02-12 19:21:53 -05:00
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// use sigils::Bounded;
|
|
|
|
|
///
|
|
|
|
|
/// let max = <f64 as Bounded>::MAX;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(max, f64::MAX);
|
|
|
|
|
/// ```
|
2025-03-10 12:17:37 -04:00
|
|
|
const MAX: Self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A macro for making implementation of
|
|
|
|
|
/// the Bounded trait easier.
|
|
|
|
|
macro_rules! bounded_trait_impl
|
|
|
|
|
{
|
2026-02-12 19:21:53 -05:00
|
|
|
($($T:ty),*) =>
|
2025-03-10 12:17:37 -04:00
|
|
|
{
|
2026-02-12 19:21:53 -05:00
|
|
|
$(
|
|
|
|
|
impl Bounded for $T
|
2025-03-10 12:17:37 -04:00
|
|
|
{
|
2026-02-12 19:21:53 -05:00
|
|
|
const MIN: $T = <$T>::MIN;
|
|
|
|
|
const MAX: $T = <$T>::MAX;
|
2025-03-10 12:17:37 -04:00
|
|
|
}
|
2026-02-12 19:21:53 -05:00
|
|
|
)*
|
2025-03-10 12:17:37 -04:00
|
|
|
}
|
2026-02-12 19:21:53 -05:00
|
|
|
}
|
2025-03-10 12:17:37 -04:00
|
|
|
|
|
|
|
|
|
2026-02-12 19:21:53 -05:00
|
|
|
// 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);
|