-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Span: Ergonomics Improvements #40
Comments
For One approach that can be done today is to feed the output of Now, going from I'm definitely fine with adding |
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
See: #21 (comment) |
First, let me say that I love the library. It just makes sense and the APIs are great to work with. Here are my thoughts after playing with the
Span
type:Easy conversion into the
Duration
type:It's a very common for service to get a span by subtracting timestamps. For example, by parsing retry-after header and applying sleep operation. Usually, sleep operation works with
Duration
and ti would be nice to have a built-in support for convertingSpan
intoDuration
:Check whether the Span is positive
Currently, there is
is_zero
andis_negative
APIs on the Span, I miss theis_positive
method that tells me theSpan
contains som non-zero positive value. Usage would be:I was also missing
saturating_add
andsaturating_sub
, but that's covered by #10.The text was updated successfully, but these errors were encountered: