I have switched to using the `core::` calling convention instead of using the `std::` by means of `extern crate core as std`. This makes it so that the library is easier to reason. The `Number` trait has been broken up now into `Number` and `Natural`. This was done to make it so that the pure mathematical types were separate from the Rust implementation types. I went and added better comments, including examples. The macros were tightened up so that the implementations took up less space. The FromNumber and ToNumber stuff will need to be reimplemented yet. Some of it seemed redundant with From and TryFrom traits now. It will be something to come back to and implement at the end.
62 lines
1.5 KiB
Rust
62 lines
1.5 KiB
Rust
// 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);
|