From d2a1e035ad039d066d82d2865c40ca890e704d4e Mon Sep 17 00:00:00 2001 From: lcnr/Bastian Kauschke Date: Thu, 7 Mar 2019 17:07:07 +0100 Subject: [PATCH] update --- src/identities.rs | 62 +++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/identities.rs b/src/identities.rs index 346e59a..4a5a2b3 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -2,16 +2,15 @@ use core::num::Wrapping; use core::ops::{Add, Mul}; /// Defines an additive identity element for `Self`. +/// +/// # Laws +/// +/// ```{.text} +/// a + 0 = a ∀ a ∈ Self +/// 0 + a = a ∀ a ∈ Self +/// ``` pub trait Zero: Sized + Add { /// Returns the additive identity element of `Self`, `0`. - /// - /// # Laws - /// - /// ```{.text} - /// a + 0 = a ∀ a ∈ Self - /// 0 + a = a ∀ a ∈ Self - /// ``` - /// /// # Purity /// /// This function should return the same result at all times regardless of @@ -22,14 +21,7 @@ pub trait Zero: Sized + Add { /// Sets `self` to the additive identity element of `Self`, `0`. /// Returns `&mut self` to enable method chaining. - /// - /// # Laws - /// - /// ```{.text} - /// a + 0 = a ∀ a ∈ Self - /// 0 + a = a ∀ a ∈ Self - /// ``` - fn to_zero(&mut self) -> &mut Self { + fn set_zero(&mut self) -> &mut Self { *self = Zero::zero(); self } @@ -80,22 +72,28 @@ where fn is_zero(&self) -> bool { self.0.is_zero() } + + fn set_zero(&mut self) -> &mut Self { + self.0.set_zero(); + self + } + fn zero() -> Self { Wrapping(T::zero()) } } /// Defines a multiplicative identity element for `Self`. +/// +/// # Laws +/// +/// ```{.text} +/// a * 1 = a ∀ a ∈ Self +/// 1 * a = a ∀ a ∈ Self +/// ``` pub trait One: Sized + Mul { /// 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 @@ -106,14 +104,7 @@ pub trait One: Sized + Mul { /// Sets `self` to the multiplicative identity element of `Self`, `1`. /// Returns `&mut self` to enable method chaining. - /// - /// # Laws - /// - /// ```{.text} - /// a * 1 = a ∀ a ∈ Self - /// 1 * a = a ∀ a ∈ Self - /// ``` - fn to_one(&mut self) -> &mut Self { + fn set_one(&mut self) -> &mut Self { *self = One::one(); self } @@ -139,6 +130,10 @@ macro_rules! one_impl { fn one() -> $t { $v } + #[inline] + fn is_one(&self) -> bool { + *self == $v + } } }; } @@ -166,6 +161,11 @@ impl One for Wrapping where Wrapping: Mul>, { + fn set_one(&mut self) -> &mut Self { + self.0.set_one(); + self + } + fn one() -> Self { Wrapping(T::one()) }