From f3869040c72ecfb6b8cf75e2222221dbdc909324 Mon Sep 17 00:00:00 2001 From: lcnr/Bastian Kauschke Date: Tue, 5 Mar 2019 14:34:49 +0100 Subject: [PATCH 1/6] add to_zero/one to Zero/One --- src/identities.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/identities.rs b/src/identities.rs index 12b9fca..437fbc1 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -1,5 +1,6 @@ use core::num::Wrapping; use core::ops::{Add, Mul}; +use core::mem; /// Defines an additive identity element for `Self`. pub trait Zero: Sized + Add { @@ -20,6 +21,24 @@ pub trait Zero: Sized + Add { // This cannot be an associated constant, because of bignums. fn zero() -> Self; + /// Sets `self` to the additive identity element of `Self`, `0`. + /// This function may be faster than replacing self with `Zero::zero()`. + /// + /// # Laws + /// + /// ```{.text} + /// a + 0 = a ∀ a ∈ Self + /// 0 + a = a ∀ a ∈ Self + /// ``` + /// + /// # Purity + /// + /// This function may return different results depending on the previous state of `self`. + fn to_zero(&mut self) -> &mut Self { + mem::replace(self, Zero::zero()); + self + } + /// Returns `true` if `self` is equal to the additive identity. #[inline] fn is_zero(&self) -> bool; @@ -90,6 +109,23 @@ pub trait One: Sized + Mul { // This cannot be an associated constant, because of bignums. fn one() -> Self; + /// Sets `self` to the multiplicative identity element of `Self`, `1`. + /// + /// # Laws + /// + /// ```{.text} + /// a * 1 = a ∀ a ∈ Self + /// 1 * a = a ∀ a ∈ Self + /// ``` + /// + /// # Purity + /// + /// This function may return different results depending on the previous state of `self`. + fn to_zero(&mut self) -> &mut Self { + mem::replace(self, One::one()); + self + } + /// Returns `true` if `self` is equal to the multiplicative identity. /// /// For performance reasons, it's best to implement this manually. From 80052795ba18ec15889da400f9297185e4337cbf Mon Sep 17 00:00:00 2001 From: lcnr/Bastian Kauschke Date: Tue, 5 Mar 2019 14:45:54 +0100 Subject: [PATCH 2/6] fix --- src/identities.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/identities.rs b/src/identities.rs index 437fbc1..0b0e0e6 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -22,7 +22,7 @@ pub trait Zero: Sized + Add { fn zero() -> Self; /// Sets `self` to the additive identity element of `Self`, `0`. - /// This function may be faster than replacing self with `Zero::zero()`. + /// Returns `&mut self` to enable method chaining. /// /// # Laws /// @@ -30,10 +30,6 @@ pub trait Zero: Sized + Add { /// a + 0 = a ∀ a ∈ Self /// 0 + a = a ∀ a ∈ Self /// ``` - /// - /// # Purity - /// - /// This function may return different results depending on the previous state of `self`. fn to_zero(&mut self) -> &mut Self { mem::replace(self, Zero::zero()); self @@ -110,6 +106,7 @@ pub trait One: Sized + Mul { fn one() -> Self; /// Sets `self` to the multiplicative identity element of `Self`, `1`. + /// Returns `&mut self` to enable method chaining. /// /// # Laws /// @@ -117,11 +114,7 @@ pub trait One: Sized + Mul { /// a * 1 = a ∀ a ∈ Self /// 1 * a = a ∀ a ∈ Self /// ``` - /// - /// # Purity - /// - /// This function may return different results depending on the previous state of `self`. - fn to_zero(&mut self) -> &mut Self { + fn to_one(&mut self) -> &mut Self { mem::replace(self, One::one()); self } From f06893feb0d7b3bd65e1566a5f7ad6501a38c0e1 Mon Sep 17 00:00:00 2001 From: lcnr/Bastian Kauschke Date: Wed, 6 Mar 2019 12:40:05 +0100 Subject: [PATCH 3/6] remove mem::replace --- src/identities.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/identities.rs b/src/identities.rs index 0b0e0e6..346e59a 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -1,6 +1,5 @@ use core::num::Wrapping; use core::ops::{Add, Mul}; -use core::mem; /// Defines an additive identity element for `Self`. pub trait Zero: Sized + Add { @@ -31,7 +30,7 @@ pub trait Zero: Sized + Add { /// 0 + a = a ∀ a ∈ Self /// ``` fn to_zero(&mut self) -> &mut Self { - mem::replace(self, Zero::zero()); + *self = Zero::zero(); self } @@ -115,7 +114,7 @@ pub trait One: Sized + Mul { /// 1 * a = a ∀ a ∈ Self /// ``` fn to_one(&mut self) -> &mut Self { - mem::replace(self, One::one()); + *self = One::one(); self } From d2a1e035ad039d066d82d2865c40ca890e704d4e Mon Sep 17 00:00:00 2001 From: lcnr/Bastian Kauschke Date: Thu, 7 Mar 2019 17:07:07 +0100 Subject: [PATCH 4/6] 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()) } From 87d4dbc4188f4648ef0e9ac348667a84a5b88ccb Mon Sep 17 00:00:00 2001 From: lcnr/Bastian Kauschke Date: Tue, 26 Mar 2019 17:39:11 +0100 Subject: [PATCH 5/6] do not return &mut Self in set_one/zero --- src/identities.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/identities.rs b/src/identities.rs index 4a5a2b3..8deb01e 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -21,9 +21,8 @@ pub trait Zero: Sized + Add { /// Sets `self` to the additive identity element of `Self`, `0`. /// Returns `&mut self` to enable method chaining. - fn set_zero(&mut self) -> &mut Self { + fn set_zero(&mut self) { *self = Zero::zero(); - self } /// Returns `true` if `self` is equal to the additive identity. @@ -73,9 +72,8 @@ where self.0.is_zero() } - fn set_zero(&mut self) -> &mut Self { + fn set_zero(&mut self) { self.0.set_zero(); - self } fn zero() -> Self { @@ -103,10 +101,8 @@ pub trait One: Sized + Mul { fn one() -> Self; /// Sets `self` to the multiplicative identity element of `Self`, `1`. - /// Returns `&mut self` to enable method chaining. - fn set_one(&mut self) -> &mut Self { + fn set_one(&mut self) { *self = One::one(); - self } /// Returns `true` if `self` is equal to the multiplicative identity. @@ -161,9 +157,8 @@ impl One for Wrapping where Wrapping: Mul>, { - fn set_one(&mut self) -> &mut Self { + fn set_one(&mut self) { self.0.set_one(); - self } fn one() -> Self { From 09e27abaa05fabf465c8b7480933bab0ec77026b Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 28 Mar 2019 12:30:22 -0700 Subject: [PATCH 6/6] Remove a stale doc comment on `set_zero()` --- src/identities.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/identities.rs b/src/identities.rs index 8deb01e..eadd018 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -20,7 +20,6 @@ pub trait Zero: Sized + Add { fn zero() -> Self; /// Sets `self` to the additive identity element of `Self`, `0`. - /// Returns `&mut self` to enable method chaining. fn set_zero(&mut self) { *self = Zero::zero(); }