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 checked operation methods to Duration #36463

Merged
merged 5 commits into from
Sep 15, 2016
Merged

Add checked operation methods to Duration #36463

merged 5 commits into from
Sep 15, 2016

Conversation

eugene-bulkin
Copy link

Addresses #35774.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@sfackler
Copy link
Member

Can we update the Add, Sub etc impls to call into these methods to avoid code duplication?

}
}

/// Checked integer multiplication. Computes `self * other`, returning
Copy link
Member

Choose a reason for hiding this comment

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

Checked duration multiplication

Copy link
Author

Choose a reason for hiding this comment

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

Fixed, thanks!

@eugene-bulkin
Copy link
Author

eugene-bulkin commented Sep 14, 2016

@sfackler What's the best way to do that? Only catch I can see is checked_op can fail in a variety of different ways but returns None every time, so there's no way to know exactly why it failed, while the trait impls will panic with the actual error reason.

Edit: To clarify, my question is specifically: do we care about the expect messages we have in there currently? Since a test explicitly checks that overflowing subtraction panics, it appears that the API does want panics for failures.

@sfackler
Copy link
Member

It looks like they only fail due to overflow, so you should just be able to do something like

fn add(self, other: Duration) -> Duration {
    self.checked_add(other).expect("overflow when adding durations")
}

@eugene-bulkin
Copy link
Author

So how about this:

  • add has the expect message "overflow when adding durations"
  • sub has the expect message "overflow when subtracting durations"
  • mul has the expect message "overflow when multiplying duration by scalar"
  • div has the expect message "divide by zero error when dividing duration by scalar"

Are those reasonable? Or are the last two too wordy?

@sfackler
Copy link
Member

Those all seem reasonable. I wouldn't worry too much about the wordyness - people hopefully shouldn't be seeing them very often :)

@eugene-bulkin
Copy link
Author

Looks like doc-tests are failing. I'll get those fixed ASAP.

@alexcrichton
Copy link
Member

Looks good to me! r=me when the tests are passing

@eugene-bulkin
Copy link
Author

Doc-tests failing now because I marked the functions as unstable.

Should I leave the marked as unstable and add the #![feature(duration_checked_ops)] part in the attributes? Or should I mark them as stable?

@sfackler
Copy link
Member

You should add the attributes in the examples.

@alexcrichton
Copy link
Member

@bors: r+

Thanks!

@bors
Copy link
Contributor

bors commented Sep 15, 2016

📌 Commit b6321bd has been approved by alexcrichton

Manishearth added a commit to Manishearth/rust that referenced this pull request Sep 15, 2016
… r=alexcrichton

Add checked operation methods to Duration

Addresses rust-lang#35774.
bors added a commit that referenced this pull request Sep 15, 2016
Rollup of 9 pull requests

- Successful merges: #36384, #36405, #36425, #36429, #36438, #36454, #36459, #36461, #36463
- Failed merges: #36444
@bors bors merged commit b6321bd into rust-lang:master Sep 15, 2016
@eugene-bulkin eugene-bulkin deleted the duration-checked-ops branch September 30, 2016 00:22
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.

6 participants