This commit is contained in:
lcnr/Bastian Kauschke 2019-03-07 17:07:07 +01:00
parent f06893feb0
commit d2a1e035ad
1 changed files with 31 additions and 31 deletions

View File

@ -2,16 +2,15 @@ use core::num::Wrapping;
use core::ops::{Add, Mul}; use core::ops::{Add, Mul};
/// Defines an additive identity element for `Self`. /// Defines an additive identity element for `Self`.
///
/// # Laws
///
/// ```{.text}
/// a + 0 = a ∀ a ∈ Self
/// 0 + a = a ∀ a ∈ Self
/// ```
pub trait Zero: Sized + Add<Self, Output = Self> { pub trait Zero: Sized + Add<Self, Output = Self> {
/// Returns the additive identity element of `Self`, `0`. /// Returns the additive identity element of `Self`, `0`.
///
/// # Laws
///
/// ```{.text}
/// a + 0 = a ∀ a ∈ Self
/// 0 + a = a ∀ a ∈ Self
/// ```
///
/// # Purity /// # Purity
/// ///
/// This function should return the same result at all times regardless of /// This function should return the same result at all times regardless of
@ -22,14 +21,7 @@ pub trait Zero: Sized + Add<Self, Output = Self> {
/// Sets `self` to the additive identity element of `Self`, `0`. /// Sets `self` to the additive identity element of `Self`, `0`.
/// Returns `&mut self` to enable method chaining. /// Returns `&mut self` to enable method chaining.
/// fn set_zero(&mut self) -> &mut Self {
/// # Laws
///
/// ```{.text}
/// a + 0 = a ∀ a ∈ Self
/// 0 + a = a ∀ a ∈ Self
/// ```
fn to_zero(&mut self) -> &mut Self {
*self = Zero::zero(); *self = Zero::zero();
self self
} }
@ -80,22 +72,28 @@ where
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
self.0.is_zero() self.0.is_zero()
} }
fn set_zero(&mut self) -> &mut Self {
self.0.set_zero();
self
}
fn zero() -> Self { fn zero() -> Self {
Wrapping(T::zero()) Wrapping(T::zero())
} }
} }
/// Defines a multiplicative identity element for `Self`. /// Defines a multiplicative identity element for `Self`.
///
/// # Laws
///
/// ```{.text}
/// a * 1 = a ∀ a ∈ Self
/// 1 * a = a ∀ a ∈ Self
/// ```
pub trait One: Sized + Mul<Self, Output = Self> { pub trait One: Sized + Mul<Self, Output = Self> {
/// Returns the multiplicative identity element of `Self`, `1`. /// Returns the multiplicative identity element of `Self`, `1`.
/// ///
/// # Laws
///
/// ```{.text}
/// a * 1 = a ∀ a ∈ Self
/// 1 * a = a ∀ a ∈ Self
/// ```
///
/// # Purity /// # Purity
/// ///
/// This function should return the same result at all times regardless of /// This function should return the same result at all times regardless of
@ -106,14 +104,7 @@ pub trait One: Sized + Mul<Self, Output = Self> {
/// Sets `self` to the multiplicative identity element of `Self`, `1`. /// Sets `self` to the multiplicative identity element of `Self`, `1`.
/// Returns `&mut self` to enable method chaining. /// Returns `&mut self` to enable method chaining.
/// fn set_one(&mut self) -> &mut Self {
/// # Laws
///
/// ```{.text}
/// a * 1 = a ∀ a ∈ Self
/// 1 * a = a ∀ a ∈ Self
/// ```
fn to_one(&mut self) -> &mut Self {
*self = One::one(); *self = One::one();
self self
} }
@ -139,6 +130,10 @@ macro_rules! one_impl {
fn one() -> $t { fn one() -> $t {
$v $v
} }
#[inline]
fn is_one(&self) -> bool {
*self == $v
}
} }
}; };
} }
@ -166,6 +161,11 @@ impl<T: One> One for Wrapping<T>
where where
Wrapping<T>: Mul<Output = Wrapping<T>>, Wrapping<T>: Mul<Output = Wrapping<T>>,
{ {
fn set_one(&mut self) -> &mut Self {
self.0.set_one();
self
}
fn one() -> Self { fn one() -> Self {
Wrapping(T::one()) Wrapping(T::one())
} }