Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inplace methods to Zero and One #104

Merged
merged 6 commits into from
Mar 28, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ pub trait Zero: Sized + Add<Self, Output = Self> {
// This cannot be an associated constant, because of bignums.
fn zero() -> Self;

/// Sets `self` to the additive identity element of `Self`, `0`.
/// Returns `&mut self` to enable method chaining.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Returns `&mut self` to enable method chaining.

///
/// # Laws
lcnr marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```{.text}
/// a + 0 = a ∀ a ∈ Self
/// 0 + a = a ∀ a ∈ Self
/// ```
fn to_zero(&mut self) -> &mut Self {
*self = Zero::zero();
self
}

/// Returns `true` if `self` is equal to the additive identity.
#[inline]
fn is_zero(&self) -> bool;
Expand Down Expand Up @@ -90,6 +104,20 @@ pub trait One: Sized + Mul<Self, Output = Self> {
// This cannot be an associated constant, because of bignums.
fn one() -> Self;

/// 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 {
*self = One::one();
self
}

/// Returns `true` if `self` is equal to the multiplicative identity.
///
/// For performance reasons, it's best to implement this manually.
Expand Down