2025-07-29 12:38:11 -04:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Sealed with Magistamp.
|
|
|
|
|
2016-01-06 05:59:38 -05:00
|
|
|
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
|
|
|
|
///
|
2025-03-10 13:04:37 -04:00
|
|
|
/// ```text
|
2016-01-06 05:59:38 -05:00
|
|
|
/// 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);
|