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

Add inplace methods to Zero and One #104

merged 6 commits into from
Mar 28, 2019

Conversation

lcnr
Copy link
Contributor

@lcnr lcnr commented Mar 5, 2019

Adds the following default implemented methods to Zero and One:

fn set_zero(&mut self) {
    *self = Zero::zero();
}
fn set_one(&mut self) {
    *self = One::one();
}

This allows for reuse of BigNums.

Copy link
Member

@cuviper cuviper left a comment

Choose a reason for hiding this comment

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

I like the idea, but not the name. Usually to_X() would be an immutable &self method returning a derived value. Since you've documented these as "Sets self to ...", I think set_zero and set_one would be better names. Another option with Rust precedent is make_zero and make_one.

src/identities.rs Outdated Show resolved Hide resolved
@lcnr
Copy link
Contributor Author

lcnr commented Mar 7, 2019

@cuviper I looked at the guidelines and think that set or make should both be fine even if they sound somewhat odd. Changed it to set? 🤔

@lcnr
Copy link
Contributor Author

lcnr commented Mar 26, 2019

I also changed the functions to not return &mut Self. I now believe doing so is a somewhat bad practice as it adds uncertainty about what exactly this method is doing.

Copy link
Member

@cuviper cuviper left a comment

Choose a reason for hiding this comment

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

Looks good, just going to remove one stale comment.

@@ -20,6 +19,12 @@ 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.

@cuviper
Copy link
Member

cuviper commented Mar 28, 2019

bors r+

bors bot added a commit that referenced this pull request Mar 28, 2019
104: Add inplace methods to `Zero` and `One` r=cuviper a=lcnr

Adds the following default implemented methods to `Zero` and `One`:

```rust
fn set_zero(&mut self) {
    *self = Zero::zero();
}
```

```rust
fn set_one(&mut self) {
    *self = One::one();
}
```

This allows for reuse of BigNums.


Co-authored-by: lcnr/Bastian Kauschke <bastian_kauschke@hotmail.de>
Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors
Copy link
Contributor

bors bot commented Mar 28, 2019

Build succeeded

@bors bors bot merged commit 09e27ab into rust-num:master Mar 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants