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

ACP: Duration::abs_diff #291

Closed
elomatreb opened this issue Nov 5, 2023 · 1 comment
Closed

ACP: Duration::abs_diff #291

elomatreb opened this issue Nov 5, 2023 · 1 comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@elomatreb
Copy link

Proposal

Problem statement

I would like to be able to get the absolute difference between two std::time::Duration values, without going through a manual "compare and subtract" dance.

Motivating examples or use cases

In a personal project I have some code that compares durations of music albums, and considers them close enough if they are within a certain threshold (because of sub-second rounding differences).

Solution sketch

I would like to propose adding a Duration::abs_diff method mirroring the existing abs_diff methods on primitive integers.

impl Duration {
    pub fn abs_diff(self, other: Duration) -> Duration {
        // ...
    }
}

Alternatives

It's perfectly possible to do this sort of thing using existing APIs, but it's verbose and potentially error-prone (e.g. the order of operations might be the wrong way around leading to a potential runtime panic). The manual implementation would be the following:

let abs_diff = if a < b {
    b - a
} else {
    a - b
};

Links and related work

n/a

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@elomatreb elomatreb added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Nov 5, 2023
@dtolnay
Copy link
Member

dtolnay commented Nov 5, 2023

Seconded; please send a standard library PR! Thanks.

@dtolnay dtolnay added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Nov 5, 2023
@dtolnay dtolnay closed this as completed Nov 5, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 21, 2023
…homcc

Add `Duration::abs_diff`

This adds a `Duration::abs_diff` method analogous to the existing one on the primitive integers.

ACP: rust-lang/libs-team#291
Tracking Issue: rust-lang#117618
bors added a commit to rust-lang/miri that referenced this issue Nov 21, 2023
Add `Duration::abs_diff`

This adds a `Duration::abs_diff` method analogous to the existing one on the primitive integers.

ACP: rust-lang/libs-team#291
Tracking Issue: rust-lang/rust#117618
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Apr 7, 2024
Add `Duration::abs_diff`

This adds a `Duration::abs_diff` method analogous to the existing one on the primitive integers.

ACP: rust-lang/libs-team#291
Tracking Issue: rust-lang/rust#117618
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
Add `Duration::abs_diff`

This adds a `Duration::abs_diff` method analogous to the existing one on the primitive integers.

ACP: rust-lang/libs-team#291
Tracking Issue: rust-lang/rust#117618
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

2 participants