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

Add Option::inspect and Result::{inspect, inspect_err} #91346

Merged
merged 1 commit into from
Dec 1, 2021

Conversation

ibraheemdev
Copy link
Member

@ibraheemdev ibraheemdev commented Nov 29, 2021

// core::result

impl Result<T, E> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
    pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self;
}

// core::option

impl Option<T> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
}

@rust-highfive
Copy link
Collaborator

r? @dtolnay

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

@LoganDark
Copy link

Would you be willing to consider using the tap names instead? i.e. tap, tap_ok, tap_err etc.

@ibraheemdev
Copy link
Member Author

I think inspect makes more sense because it 1. is consistent with existing APIs (Iterator::inspect) and 2. better describes what you're doing (inspecting the inner value through a reference). I did mention adding std::tap::Tap as an unresolved question in the tracking issue.

@LoganDark
Copy link

If you're using functional methods on the Option/Result like this, it's likely part of a method call chain (like a pipeline). In that case I think tap is appropriate, since you are not just inspecting the value, you are keeping it going through the pipeline as you do so. Just tapping it, not taking it out to inspect.

@OptimisticPeach
Copy link
Contributor

@LoganDark
Copy link

Iterator::inspect takes the item by reference.

Of course, I didn't mean to say otherwise. :)

@jhpratt
Copy link
Member

jhpratt commented Nov 29, 2021

fwiw I'd prefer they be named inspect for symmetry with iterators

@camsteffen
Copy link
Contributor

I'm a little surprised the signature isn't

fn inspect<F: FnOnce(&T)>(&self, f: F);

though I understand the benefit of method chaining. How useful would method chaining actually be in practice?

Sometimes you have a &mut Option<_> and take-by-value would be unusable for that case. But this is relatively uncommon.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Thanks for including reasonably representative example code which conveys the character of where you might find these functions used in real code, i.e. as opposed to something minimal-effort / contrived like let result = Ok(1); result.inspect(|x| foo(x));.

@dtolnay
Copy link
Member

dtolnay commented Dec 1, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Dec 1, 2021

📌 Commit 2e8358e has been approved by dtolnay

@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 Dec 1, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 1, 2021
…askrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#87160 (When recovering from a `:` in a pattern, use adequate AST pattern)
 - rust-lang#90985 (Use `get_diagnostic_name` more)
 - rust-lang#91087 (Remove all migrate.nll.stderr files)
 - rust-lang#91207 (Add support for LLVM coverage mapping format versions 5 and 6)
 - rust-lang#91298 (Improve error message for `E0659` if the source is not available)
 - rust-lang#91346 (Add `Option::inspect` and `Result::{inspect, inspect_err}`)
 - rust-lang#91404 (Fix bad `NodeId` limit checking.)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ce197e2 into rust-lang:master Dec 1, 2021
@rustbot rustbot added this to the 1.59.0 milestone Dec 1, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 13, 2023
…Sushi

Stabilize `result_option_inspect`

This PR stabilizes `result_option_inspect`:

```rust
// core::option

impl Option<T> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
}

// core::result

impl Result<T, E> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
    pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self;
}
```

<br>

Tracking issue: rust-lang#91345.
Implementation PR: rust-lang#91346.

Closes rust-lang#91345.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Nov 15, 2023
Stabilize `result_option_inspect`

This PR stabilizes `result_option_inspect`:

```rust
// core::option

impl Option<T> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
}

// core::result

impl Result<T, E> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
    pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self;
}
```

<br>

Tracking issue: rust-lang/rust#91345.
Implementation PR: rust-lang/rust#91346.

Closes rust-lang/rust#91345.
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Apr 7, 2024
Stabilize `result_option_inspect`

This PR stabilizes `result_option_inspect`:

```rust
// core::option

impl Option<T> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
}

// core::result

impl Result<T, E> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
    pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self;
}
```

<br>

Tracking issue: rust-lang/rust#91345.
Implementation PR: rust-lang/rust#91346.

Closes rust-lang/rust#91345.
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 27, 2024
Stabilize `result_option_inspect`

This PR stabilizes `result_option_inspect`:

```rust
// core::option

impl Option<T> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
}

// core::result

impl Result<T, E> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
    pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self;
}
```

<br>

Tracking issue: rust-lang/rust#91345.
Implementation PR: rust-lang/rust#91346.

Closes rust-lang/rust#91345.
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants