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

Revise Months API #752

Merged
merged 1 commit into from Aug 4, 2022
Merged

Revise Months API #752

merged 1 commit into from Aug 4, 2022

Conversation

djc
Copy link
Contributor

@djc djc commented Aug 3, 2022

  • Provide checked_add_months() and checked_sub_months() for
    callers that would like to avoid panics
  • Document panic potential in Add and Sub implementations
  • Implement additional traits for Months per API guidelines
  • Hide inner type for Months and add constructor
  • Use lower-level APIs to clamp day

Since I didn't get another chance to review #731 before it was merged (and the suggestion to make panicking explicit doesn't seem to have been followed up), here are some proposed improvements.

cc @avantgardnerio

@djc djc requested a review from esheppa August 3, 2022 09:50
* Provide `checked_add_months()` and `checked_sub_months()` for
  callers that would like to avoid panics
* Document panic potential in `Add` and `Sub` implementations
* Implement additional traits for `Months` per API guidelines
* Hide inner type for `Months` and add constructor
* Use lower-level APIs to clamp day
Copy link
Collaborator

@esheppa esheppa left a comment

Choose a reason for hiding this comment

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

This looks very clean and consistent with other chrono APIs with the checked_add_* functions. I've set up #753 with a potential change moving the adding months to a month logic to overflowing_add/sub functions in the month module, however feel free to ignore that if you think it doesn't make sense to have the overflowing_add/sub functions.

return Some(self);
}

match months.0 <= core::i32::MAX as u32 {
Copy link
Collaborator

@esheppa esheppa Aug 3, 2022

Choose a reason for hiding this comment

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

Could we avoid this by having new_opt and new constructors on the Months which only accept valid u32 values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that core::i32::MAX and -core::i32::MIN are not the same value... in other words, the boundary here depends on the context in which it is used (in this case add vs sub). I'm inclined to not make this any more complex than it already is.

Copy link

@avantgardnerio avantgardnerio left a comment

Choose a reason for hiding this comment

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

Very nice, thank you!

// Clamp original day in case new month is shorter

let flags = YearFlags::from_year(year);
let feb_days = if flags.ndays() == 366 { 29 } else { 28 };

Choose a reason for hiding this comment

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

This is a much better implementation, thank you! Had I known about ndays() I'd have done things differently. Either way, very happy to have support for adding Months in chrono!

}

// Copy `i32::MIN` here so we don't have to do a complicated cast
match months.0 <= 2_147_483_648 {

Choose a reason for hiding this comment

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

👍

Copy link
Collaborator

@esheppa esheppa 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!

@djc djc merged commit bd3b48d into main Aug 4, 2022
@djc djc deleted the checked-add-sub-months branch August 4, 2022 14:05
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

3 participants