Files
sigils/src/one.rs

55 lines
1011 B
Rust
Raw Normal View History

use std::ops::Mul;
/// Defines a multiplicative identity element for `Self`.
pub trait One: Sized + Mul<Self, Output=Self>
{
/// Returns the multiplicative identity element of `Self`, `1`.
///
/// # Laws
///
/// ```{.text}
/// a * 1 = a ∀ a ∈ Self
/// 1 * a = a ∀ a ∈ Self
/// ```
///
/// # Purity
///
/// This function should return the same result at all times regardless of
/// external mutable state, for example values stored in TLS or in
/// `static mut`s.
fn one() -> Self;
}
macro_rules! one_impl
{
($varType: ty, $val: expr) =>
{
impl One for $varType
{
fn one() -> $varType
{
$val
}
}
}
}
one_impl!(u8, 1u8);
one_impl!(u16, 1u16);
one_impl!(u32, 1u32);
one_impl!(u64, 1u64);
one_impl!(usize, 1usize);
one_impl!(i8, 1i8);
one_impl!(i16, 1i16);
one_impl!(i32, 1i32);
one_impl!(i64, 1i64);
one_impl!(isize, 1isize);
one_impl!(f32, 1.0f32);
one_impl!(f64, 1.0f64);