Added the Zero and One traits to sigils.
It looks like these may be removed from the standard library soon.
This commit is contained in:
54
src/one.rs
Normal file
54
src/one.rs
Normal file
@ -0,0 +1,54 @@
|
||||
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);
|
Reference in New Issue
Block a user