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

subsec_{milli,micro,nano}seconds may have over-inclusive documented ranges #669

Closed
samcowger opened this issue Mar 7, 2024 · 1 comment · Fixed by #670
Closed

subsec_{milli,micro,nano}seconds may have over-inclusive documented ranges #669

samcowger opened this issue Mar 7, 2024 · 1 comment · Fixed by #670
Labels
A-docs Area: documentation

Comments

@samcowger
Copy link
Contributor

Documentation of these functions promises that they will produce values in the ranges -1_000..1_000, -1_000_000..1_000_000, and -1_000_000_000..1_000_000_000, respectively, but I don't see how they could produce the lowest value in their stated bounds.

In terms of code, I'll use subsec_nanoseconds as an example:

time/time/src/duration.rs

Lines 912 to 923 in 7a6a800

/// Get the number of nanoseconds past the number of whole seconds.
///
/// The returned value will always be in the range `-1_000_000_000..1_000_000_000`.
///
/// ```rust
/// # use time::ext::NumericalDuration;
/// assert_eq!(1.000_000_400.seconds().subsec_nanoseconds(), 400);
/// assert_eq!((-1.000_000_400).seconds().subsec_nanoseconds(), -400);
/// ```
pub const fn subsec_nanoseconds(self) -> i32 {
self.nanoseconds.get()
}

self.nanoseconds is a Nanoseconds, which is a RangedI32 - in particular, a RangedI32<{ -(Nanosecond::per(Second) as i32 - 1) }, { Nanosecond::per(Second) as i32 - 1 }>. per seems to be defined appropriately, so this gives us RangedI32<-999_999_999, 999_999_999>. Given that a RangedI32<MIN, MAX> promises to contain "[a]n i32 that is known to be in the range MIN..=MAX", it's impossible for -1_000_000_000 to inhabit Nanoseconds, and so to be produced by subsec_nanoseconds.

I believe the same logic can be extended to subsec_microseconds and subsec_milliseconds, since they defer to subsec_nanoseconds.

@jhpratt
Copy link
Member

jhpratt commented Mar 8, 2024

Yeah, this is a mistake. It should be changed to -999_999_999..=999_999_999, using the same number for both bounds for symmetry. Likewise for other similar values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants