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

span: add Span::to_duration and TryFrom impls for Duration #47

Merged
merged 1 commit into from
Jul 26, 2024

Conversation

BurntSushi
Copy link
Owner

This PR dips our toes into the water of integration with
std::time::Duration. I do somewhat expect there to be more integration
points, particularly in the datetime arithmetic APIs, but this at least
paves a path for folks to correctly convert between a Duration and a
Span. The error cases for the new APIs will ensure you can never
misuse these APIs.

This adds a few new APIs:

  • Span::is_positive, since we already have Span::is_negative and
    Span::is_zero.
  • Span::signum, since it's signed and it's sometimes convenient.
  • TryFrom<std::time::Duration> for Span.
  • TryFrom<Span> for std::time::Duration that returns an error if
    the span is negative or has non-zero units bigger than days.
  • Span::to_duration that requires a relative date and can convert any
    positive span into a Duration (modulo overflow).

Partially addresses #21

Fixes #40

This PR dips our toes into the water of integration with
`std::time::Duration`. I do somewhat expect there to be more integration
points, particularly in the datetime arithmetic APIs, but this at least
paves a path for folks to _correctly_ convert between a `Duration` and a
`Span`. The error cases for the new APIs will ensure you can never
misuse these APIs.

This adds a few new APIs:

* `Span::is_positive`, since we already have `Span::is_negative` and
  `Span::is_zero`.
* `Span::signum`, since it's signed and it's sometimes convenient.
* `TryFrom<std::time::Duration> for Span`.
* `TryFrom<Span> for std::time::Duration` that returns an error if
  the span is negative or has non-zero units bigger than days.
* `Span::to_duration` that requires a relative date and can convert any
  positive span into a `Duration` (modulo overflow).

Partially addresses #21

Fixes #40
@BurntSushi BurntSushi merged commit 93f66f0 into master Jul 26, 2024
14 checks passed
@BurntSushi BurntSushi deleted the ag/i21-initial-duration-integration branch July 26, 2024 00:21
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.

Span: Ergonomics Improvements
1 participant