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

Consolidate Result's and Option's methods into fewer impl blocks #92444

Merged
merged 15 commits into from
Jan 3, 2022

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Dec 30, 2021

Result's and Option's methods have historically been separated up into impl blocks based on their trait bounds, with the bounds specified on type parameters of the impl block. I find this unhelpful because closely related methods, like unwrap_or and unwrap_or_default, end up disproportionately far apart in source code and rustdocs:

impl<T> Option<T> {
    pub fn unwrap_or(self, default: T) -> T {
        ...
    }

    one eternity later
}

impl<T: Default> Option<T> {
    pub fn unwrap_or_default(self) -> T {
        ...
    }
}

I'd prefer for method to be in as few impl blocks as possible, with the most logical grouping within each impl block. Any bounds needed can be written as where clauses on the method instead:

impl<T> Option<T> {
    pub fn unwrap_or(self, default: T) -> T {
        ...
    }

    pub fn unwrap_or_default(self) -> T
    where
        T: Default,
    {
        ...
    }
}

Warning: the end-to-end diff of this PR is computed confusingly by git / rendered confusingly by GitHub; it's practically impossible to review that way. I've broken the PR into commits that move small groups of methods for which git behaves better — these each should be easily individually reviewable.

@dtolnay dtolnay added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Dec 30, 2021
@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 30, 2021
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 30, 2021
@dtolnay dtolnay removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 30, 2021
@rust-lang rust-lang deleted a comment from alamb Dec 31, 2021
@joshtriplett
Copy link
Member

Looking at the UI test updates, I like the net effect of this on the error messages.

@bors r+

@bors
Copy link
Contributor

bors commented Jan 1, 2022

📌 Commit 5960f7a has been approved by joshtriplett

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 1, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 3, 2022
…askrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#90102 (Remove `NullOp::Box`)
 - rust-lang#92011 (Use field span in `rustc_macros` when emitting decode call)
 - rust-lang#92402 (Suggest while let x = y when encountering while x = y)
 - rust-lang#92409 (Couple of libtest cleanups)
 - rust-lang#92418 (Fix spacing in pretty printed PatKind::Struct with no fields)
 - rust-lang#92444 (Consolidate Result's and Option's methods into fewer impl blocks)

Failed merges:

 - rust-lang#92483 (Stabilize `result_cloned` and `result_copied`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 13e2840 into rust-lang:master Jan 3, 2022
@rustbot rustbot added this to the 1.59.0 milestone Jan 3, 2022
@dtolnay dtolnay deleted the coremethods branch January 3, 2022 18:41
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 4, 2022
Move `contains` method of Option and Result lower in docs

Follow-up to rust-lang#92444 trying to get the `Option` and `Result` rustdocs in better shape.

This addresses the request in rust-lang#62358 (comment). The `contains` methods are previously too high up in the docs on both `Option` and `Result` &mdash; stuff like `ok` and `map` and `and_then` should all be featured higher than `contains`. All of those are more ubiquitously useful than `contains`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants