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 Result::map_or #66292

Merged
merged 1 commit into from
Nov 13, 2019
Merged

add Result::map_or #66292

merged 1 commit into from
Nov 13, 2019

Conversation

tesuji
Copy link
Contributor

@tesuji tesuji commented Nov 11, 2019

This PR adds this API to make it consistent with Option::map_or.

impl<T, E> Result<T, E> {
    pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
        match self {
            Ok(t) => f(t),
            Err(_) => default,
        }
    }
}

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed (pretty log, raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2019-11-11T13:26:06.0486490Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-11-11T13:26:06.8287724Z ##[command]git config gc.auto 0
2019-11-11T13:26:06.8290143Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-11-11T13:26:06.8292392Z ##[command]git config --get-all http.proxy
2019-11-11T13:26:06.8296169Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/66292/merge:refs/remotes/pull/66292/merge
---
2019-11-11T14:27:10.7614063Z .................................................................................................... 1400/9228
2019-11-11T14:27:17.0662777Z .................................................................................................... 1500/9228
2019-11-11T14:27:23.2103167Z .................................................................................................... 1600/9228
2019-11-11T14:27:32.4106929Z .................................................................................................... 1700/9228
2019-11-11T14:27:40.8672124Z ..i................................................................................................. 1800/9228
2019-11-11T14:27:47.6168828Z ......................................................................................iiiii......... 1900/9228
2019-11-11T14:28:08.9748473Z .................................................................................................... 2100/9228
2019-11-11T14:28:11.3606468Z .................................................................................................... 2200/9228
2019-11-11T14:28:13.8639781Z .................................................................................................... 2300/9228
2019-11-11T14:28:23.4935318Z .................................................................................................... 2400/9228
---
2019-11-11T14:31:15.1942993Z ..................................................................................i...............i. 4700/9228
2019-11-11T14:31:22.3021801Z .................................................................................................... 4800/9228
2019-11-11T14:31:31.3430284Z .................................................................................................... 4900/9228
2019-11-11T14:31:36.5905527Z .................................................................................................... 5000/9228
2019-11-11T14:31:47.8853852Z .....................................................................................ii.ii.......... 5100/9228
2019-11-11T14:31:51.6197305Z .i.................................................................................................. 5200/9228
2019-11-11T14:32:05.8555148Z .................................................................................................... 5400/9228
2019-11-11T14:32:12.8714516Z ...................................................................i................................ 5500/9228
2019-11-11T14:32:20.2609625Z .................................................................................................... 5600/9228
2019-11-11T14:32:28.2065577Z .................................................................................................... 5700/9228
2019-11-11T14:32:28.2065577Z .................................................................................................... 5700/9228
2019-11-11T14:32:37.1021655Z ....................................................ii...i..ii...........i.......................... 5800/9228
2019-11-11T14:32:59.2088563Z .................................................................................................... 6000/9228
2019-11-11T14:33:07.4202365Z .................................................................................................... 6100/9228
2019-11-11T14:33:07.4202365Z .................................................................................................... 6100/9228
2019-11-11T14:33:13.9942172Z .......................................................................i..ii........................ 6200/9228
2019-11-11T14:33:42.6331487Z .................................................................................................... 6400/9228
2019-11-11T14:33:44.7482787Z .......................................i............................................................ 6500/9228
2019-11-11T14:33:46.9352563Z .................................................................................................... 6600/9228
2019-11-11T14:33:49.2351513Z .......................i............................................................................ 6700/9228
---
2019-11-11T14:38:59.6654190Z  finished in 5.620
2019-11-11T14:38:59.6826665Z Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T14:38:59.8670064Z 
2019-11-11T14:38:59.8670397Z running 156 tests
2019-11-11T14:39:02.7079993Z iiii....iii......iii..iiii...i.............................i..i..................i....i...........ii 100/156
2019-11-11T14:39:04.5827060Z .i.i..iiii..............i.........iii.i.........ii......
2019-11-11T14:39:04.5830073Z 
2019-11-11T14:39:04.5832066Z  finished in 4.900
2019-11-11T14:39:04.5993714Z Check compiletest suite=codegen-units mode=codegen-units (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T14:39:04.7715413Z 
---
2019-11-11T14:39:06.6831858Z  finished in 2.083
2019-11-11T14:39:06.7007892Z Check compiletest suite=assembly mode=assembly (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T14:39:07.5782415Z 
2019-11-11T14:39:07.5787546Z running 9 tests
2019-11-11T14:39:07.5790159Z iiiiiiiii
2019-11-11T14:39:07.5795467Z 
2019-11-11T14:39:07.5796229Z  finished in 0.160
2019-11-11T14:39:07.5796746Z Check compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T14:39:07.5796916Z 
---
2019-11-11T14:39:25.9253034Z  finished in 19.046
2019-11-11T14:39:25.9503674Z Check compiletest suite=debuginfo mode=debuginfo-gdb+lldb (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T14:39:26.1300292Z 
2019-11-11T14:39:26.1300514Z running 123 tests
2019-11-11T14:39:49.5192893Z .iiiii...i.....i..i...i..i.i.i..i.ii..i.i.....i..i....ii..........iiii..........i...ii...i.......ii. 100/123
2019-11-11T14:39:54.0018324Z i.i.i......iii.i.....ii
2019-11-11T14:39:54.0020596Z 
2019-11-11T14:39:54.0020879Z  finished in 28.051
2019-11-11T14:39:54.0029992Z Uplifting stage1 rustc (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-11-11T14:39:54.0030496Z Copying stage2 rustc from stage1 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu / x86_64-unknown-linux-gnu)
---
2019-11-11T14:51:47.6421602Z 
2019-11-11T14:51:47.6422357Z    Doc-tests core
2019-11-11T14:51:52.5791683Z 
2019-11-11T14:51:52.5792413Z running 2419 tests
2019-11-11T14:52:03.0035409Z ......iiiii......................................................................................... 100/2419
2019-11-11T14:52:13.3788564Z ................................................................................ii.................. 200/2419
2019-11-11T14:52:37.3059305Z ..i................................................................................................. 400/2419
2019-11-11T14:52:37.3059305Z ..i................................................................................................. 400/2419
2019-11-11T14:52:47.1922239Z ..................................................i..i.................iiii......................... 500/2419
2019-11-11T14:53:05.7476180Z .................................................................................................... 700/2419
2019-11-11T14:53:15.5902112Z .................................................................................................... 800/2419
2019-11-11T14:53:25.4517676Z .................................................................................................... 900/2419
2019-11-11T14:53:35.2564883Z .................................................................................................... 1000/2419
---
2019-11-11T14:56:01.4768157Z ---- result.rs - result::Result<T, E>::map_or (line 522) stdout ----
2019-11-11T14:56:01.4768699Z error[E0599]: no method named `len` found for type `char` in the current scope
2019-11-11T14:56:01.4769308Z  --> result.rs:528:31
2019-11-11T14:56:01.4769790Z   |
2019-11-11T14:56:01.4769952Z 9 | assert_eq!(x.map_or(42, |v| v.len()), 42);
2019-11-11T14:56:01.4773776Z 
2019-11-11T14:56:01.4773826Z error: aborting due to previous error
2019-11-11T14:56:01.4775213Z 
2019-11-11T14:56:01.4775961Z For more information about this error, try `rustc --explain E0599`.
---
2019-11-11T14:56:01.4899595Z   local time: Mon Nov 11 14:56:01 UTC 2019
2019-11-11T14:56:01.5828733Z   network time: Mon, 11 Nov 2019 14:56:01 GMT
2019-11-11T14:56:01.5833060Z == end clock drift check ==
2019-11-11T14:56:02.1528875Z 
2019-11-11T14:56:02.1622311Z ##[error]Bash exited with code '1'.
2019-11-11T14:56:02.1664628Z ##[section]Starting: Checkout
2019-11-11T14:56:02.1666192Z ==============================================================================
2019-11-11T14:56:02.1666240Z Task         : Get sources
2019-11-11T14:56:02.1666295Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

src/libcore/result.rs Outdated Show resolved Hide resolved
@tesuji tesuji changed the title add Result::map_or add Result::map_or and stabilize it Nov 11, 2019
@tesuji
Copy link
Contributor Author

tesuji commented Nov 12, 2019

r? @SimonSapin

@rust-highfive rust-highfive assigned SimonSapin and unassigned dtolnay Nov 12, 2019
@SimonSapin
Copy link
Contributor

I don’t think we have precedent for adding instantly-stable APIs, except in cases where constrained by limitation of the stability tracking implementation. (Specifically: trait impls do not have stability of their own, so a new impl of an existing stable trait for an existing stable type is insta-stable by necessity.)

That said, in this case this is very similar to the existing Option::map_or so I wouldn’t mind proposing FCP soon after landing an unstable implementation.

I see you’ve already opened a tracking issue #66293, please change the attribute to unstable in this PR.

@SimonSapin SimonSapin added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 12, 2019
@tesuji
Copy link
Contributor Author

tesuji commented Nov 12, 2019

Done.

@SimonSapin
Copy link
Contributor

Looks good, thanks!

@bors r+

@bors
Copy link
Contributor

bors commented Nov 12, 2019

📌 Commit e8f3a9f has been approved by SimonSapin

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 12, 2019
@tesuji tesuji changed the title add Result::map_or and stabilize it add Result::map_or Nov 12, 2019
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Nov 13, 2019
add Result::map_or

This PR adds this API to make it consistent with `Option::map_or`.

```rust
impl<T, E> Result<T, E> {
    pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
        match self {
            Ok(t) => f(t),
            Err(_) => default,
        }
    }
}
```

This API is very small. We already has a similar API for `Option::map_or`.
bors added a commit that referenced this pull request Nov 13, 2019
Rollup of 13 pull requests

Successful merges:

 - #65932 (download .tar.xz if python3 is used)
 - #66074 ([mir-opt] Turn on the `ConstProp` pass by default)
 - #66094 (Fix documentation for `Iterator::count()`.)
 - #66166 (rename cfg(rustdoc) into cfg(doc))
 - #66227 (docs: Fix link to BufWriter::flush)
 - #66292 (add Result::map_or)
 - #66297 (Add a callback that allows compiler consumers to override queries.)
 - #66317 (Use a relative bindir for rustdoc to find rustc)
 - #66330 (Improve non-exhaustiveness handling in usefulness checking)
 - #66331 (Add some tests for fixed ICEs)
 - #66334 (Move Session fields to CrateStore)
 - #66335 (Move self-profile infrastructure to data structures)
 - #66337 (Remove dead code for encoding/decoding lint IDs)

Failed merges:

r? @ghost
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Nov 13, 2019
add Result::map_or

This PR adds this API to make it consistent with `Option::map_or`.

```rust
impl<T, E> Result<T, E> {
    pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
        match self {
            Ok(t) => f(t),
            Err(_) => default,
        }
    }
}
```

This API is very small. We already has a similar API for `Option::map_or`.
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Nov 13, 2019
add Result::map_or

This PR adds this API to make it consistent with `Option::map_or`.

```rust
impl<T, E> Result<T, E> {
    pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
        match self {
            Ok(t) => f(t),
            Err(_) => default,
        }
    }
}
```

This API is very small. We already has a similar API for `Option::map_or`.
bors added a commit that referenced this pull request Nov 13, 2019
Rollup of 14 pull requests

Successful merges:

 - #65932 (download .tar.xz if python3 is used)
 - #66094 (Fix documentation for `Iterator::count()`.)
 - #66166 (rename cfg(rustdoc) into cfg(doc))
 - #66186 (Add long error explanation for E0623)
 - #66227 (docs: Fix link to BufWriter::flush)
 - #66248 (add raw ptr variant of UnsafeCell::get)
 - #66292 (add Result::map_or)
 - #66297 (Add a callback that allows compiler consumers to override queries.)
 - #66317 (Use a relative bindir for rustdoc to find rustc)
 - #66330 (Improve non-exhaustiveness handling in usefulness checking)
 - #66331 (Add some tests for fixed ICEs)
 - #66334 (Move Session fields to CrateStore)
 - #66335 (Move self-profile infrastructure to data structures)
 - #66337 (Remove dead code for encoding/decoding lint IDs)

Failed merges:

r? @ghost
@bors bors merged commit e8f3a9f into rust-lang:master Nov 13, 2019
@bors
Copy link
Contributor

bors commented Nov 13, 2019

☔ The latest upstream changes (presumably #66366) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 13, 2019
@tesuji tesuji deleted the result-map_or branch November 13, 2019 23:44
@cdbattags
Copy link

Looks like this is out in the wild now with 1.41 but why is default first?

@mathstuf
Copy link
Contributor

It matches Option::map_or's argument ordering. That ship sailed long ago. https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or

I believe the rationale was to allow closures to run off the end of the argument list and not be stuck with an awkward formatting decision of what to do after its closing brace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants