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,8 +2,6 @@ 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`.
pub trait Zero: Sized + Add<Self, Output = Self> {
/// Returns the additive identity element of `Self`, `0`.
/// ///
/// # Laws /// # Laws
/// ///
@ -11,7 +9,8 @@ pub trait Zero: Sized + Add<Self, Output = Self> {
/// a + 0 = a ∀ a ∈ Self /// a + 0 = a ∀ a ∈ Self
/// 0 + a = a ∀ a ∈ Self /// 0 + a = a ∀ a ∈ Self
/// ``` /// ```
/// pub trait Zero: Sized + Add<Self, Output = Self> {
/// Returns the additive identity element of `Self`, `0`.
/// # 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,14 +72,18 @@ 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`.
pub trait One: Sized + Mul<Self, Output = Self> {
/// Returns the multiplicative identity element of `Self`, `1`.
/// ///
/// # Laws /// # Laws
/// ///
@ -95,6 +91,8 @@ pub trait One: Sized + Mul<Self, Output = Self> {
/// a * 1 = a ∀ a ∈ Self /// a * 1 = a ∀ a ∈ Self
/// 1 * a = a ∀ a ∈ Self /// 1 * a = a ∀ a ∈ Self
/// ``` /// ```
pub trait One: Sized + Mul<Self, Output = Self> {
/// Returns the multiplicative identity element of `Self`, `1`.
/// ///
/// # Purity /// # Purity
/// ///
@ -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())
} }