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 or_insert_with_key to Entry of HashMap/BTreeMap #70996

Merged
merged 3 commits into from
Apr 12, 2020
Merged

Add or_insert_with_key to Entry of HashMap/BTreeMap #70996

merged 3 commits into from
Apr 12, 2020

Conversation

ChaiTRex
Copy link
Contributor

Going along with or_insert_with, or_insert_with_key provides the Entry's key to the lambda, avoiding the need to either clone the key or the need to reimplement this body of this method from scratch each time.

This is useful when the initial value for a map entry is derived from the key. For example, the introductory Rust book has an example Cacher struct that takes an expensive-to-compute lambda and then can, given an argument to the lambda, produce either the cached result or execute the lambda.


I'm fairly new to Rust, so any optimizations, corrections to types, better names, better documentation, or whatever else would be appreciated. I'd like to thank Arnavion on freenode for helping me to implement a very similar method when I found that or_insert_with_key was unavailable.

As a somewhat-related note, this implements rust-lang/rfcs#1202 from 2015, so if this pull request is accepted, that should be closed.

Going along with or_insert_with, or_insert_with_key provides the
Entry's key to the lambda, avoiding the need to either clone the
key or the need to reimplement this body of this method from
scratch each time.

This is useful when the initial value for a map entry is derived
from the key. For example, the introductory Rust book has an
example Cacher struct that takes an expensive-to-compute lambda and
then can, given an argument to the lambda, produce either the
cached result or execute the lambda.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @LukasKalbertodt (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 10, 2020
@joshtriplett joshtriplett added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 10, 2020
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-7 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.
2020-04-10T17:03:10.8399070Z ========================== Starting Command Output ===========================
2020-04-10T17:03:10.8402642Z [command]/bin/bash --noprofile --norc /home/vsts/work/_temp/80095d5c-57c8-4cae-a15f-2c375c289786.sh
2020-04-10T17:03:10.8402903Z 
2020-04-10T17:03:10.8409261Z ##[section]Finishing: Disable git automatic line ending conversion
2020-04-10T17:03:10.8429267Z ##[section]Starting: Checkout rust-lang/rust@refs/pull/70996/merge to s
2020-04-10T17:03:10.8432641Z Task         : Get sources
2020-04-10T17:03:10.8432900Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
2020-04-10T17:03:10.8433150Z Version      : 1.0.0
2020-04-10T17:03:10.8433318Z Author       : Microsoft
---
2020-04-10T17:03:11.8337094Z ##[command]git remote add origin https://github.com/rust-lang/rust
2020-04-10T17:03:11.8341552Z ##[command]git config gc.auto 0
2020-04-10T17:03:11.8344561Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2020-04-10T17:03:11.8347326Z ##[command]git config --get-all http.proxy
2020-04-10T17:03:11.8352386Z ##[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/70996/merge:refs/remotes/pull/70996/merge
---
2020-04-10T17:06:06.4597264Z Looks like docker image is the same as before, not uploading
2020-04-10T17:06:13.9501109Z [CI_JOB_NAME=x86_64-gnu-llvm-7]
2020-04-10T17:06:13.9806111Z [CI_JOB_NAME=x86_64-gnu-llvm-7]
2020-04-10T17:06:13.9836554Z == clock drift check ==
2020-04-10T17:06:13.9839755Z   local time: Fri Apr 10 17:06:13 UTC 2020
2020-04-10T17:06:14.1763414Z   network time: Fri, 10 Apr 2020 17:06:14 GMT
2020-04-10T17:06:14.1790263Z Starting sccache server...
2020-04-10T17:06:14.2627253Z configure: processing command line
2020-04-10T17:06:14.2627688Z configure: 
2020-04-10T17:06:14.2628529Z configure: rust.dist-src        := False
---
2020-04-10T17:11:45.8883026Z    Compiling rustc_feature v0.0.0 (/checkout/src/librustc_feature)
2020-04-10T17:11:47.3991843Z    Compiling fmt_macros v0.0.0 (/checkout/src/libfmt_macros)
2020-04-10T17:11:48.9702051Z    Compiling rustc_ast_pretty v0.0.0 (/checkout/src/librustc_ast_pretty)
2020-04-10T17:11:51.1584018Z    Compiling rustc_hir v0.0.0 (/checkout/src/librustc_hir)
2020-04-10T17:11:58.9524236Z    Compiling rustc_query_system v0.0.0 (/checkout/src/librustc_query_system)
2020-04-10T17:12:02.8326820Z    Compiling rustc_hir_pretty v0.0.0 (/checkout/src/librustc_hir_pretty)
2020-04-10T17:12:07.1521858Z    Compiling rustc_attr v0.0.0 (/checkout/src/librustc_attr)
2020-04-10T17:12:11.4532450Z    Compiling rustc_parse v0.0.0 (/checkout/src/librustc_parse)
2020-04-10T17:12:19.7770017Z    Compiling rustc_ast_lowering v0.0.0 (/checkout/src/librustc_ast_lowering)
---
2020-04-10T17:36:21.6717613Z    Compiling rustc_feature v0.0.0 (/checkout/src/librustc_feature)
2020-04-10T17:36:23.6979258Z    Compiling fmt_macros v0.0.0 (/checkout/src/libfmt_macros)
2020-04-10T17:36:25.9490365Z    Compiling rustc_ast_pretty v0.0.0 (/checkout/src/librustc_ast_pretty)
2020-04-10T17:36:28.7659966Z    Compiling rustc_hir v0.0.0 (/checkout/src/librustc_hir)
2020-04-10T17:36:39.6777904Z    Compiling rustc_query_system v0.0.0 (/checkout/src/librustc_query_system)
2020-04-10T17:36:44.2623462Z    Compiling rustc_hir_pretty v0.0.0 (/checkout/src/librustc_hir_pretty)
2020-04-10T17:36:50.0704390Z    Compiling rustc_attr v0.0.0 (/checkout/src/librustc_attr)
2020-04-10T17:36:56.0667234Z    Compiling rustc_parse v0.0.0 (/checkout/src/librustc_parse)
2020-04-10T17:37:07.4895362Z    Compiling rustc_ast_lowering v0.0.0 (/checkout/src/librustc_ast_lowering)
---
2020-04-10T18:03:03.7791228Z .................................................................................................... 1600/9883
2020-04-10T18:03:10.1119911Z .................................................................................................... 1700/9883
2020-04-10T18:03:14.4879479Z .................................................................................................... 1800/9883
2020-04-10T18:03:23.4807527Z .................................................................................................... 1900/9883
2020-04-10T18:03:32.5545189Z ..i................................................................................................. 2000/9883
2020-04-10T18:03:39.4011082Z ............................................................................................iiiii... 2100/9883
2020-04-10T18:04:01.7165688Z .................................................................................................... 2300/9883
2020-04-10T18:04:03.8249626Z .................................................................................................... 2400/9883
2020-04-10T18:04:05.9972896Z .................................................................................................... 2500/9883
2020-04-10T18:04:11.7601923Z .................................................................................................... 2600/9883
---
2020-04-10T18:07:25.0011250Z .................................................................................................... 5100/9883
2020-04-10T18:07:33.2688048Z .................................................................................................... 5200/9883
2020-04-10T18:07:38.6950159Z ...........i........................................................................................ 5300/9883
2020-04-10T18:07:49.7613847Z .................................................................................................... 5400/9883
2020-04-10T18:07:55.5989851Z ii.ii........i...i.................................................................................. 5500/9883
2020-04-10T18:08:04.0011017Z .............................................i...................................................... 5700/9883
2020-04-10T18:08:15.4203795Z .................................................................ii................................. 5800/9883
2020-04-10T18:08:23.6778327Z ....i............................................................................................... 5900/9883
2020-04-10T18:08:30.5510896Z .................................................................................................... 6000/9883
2020-04-10T18:08:30.5510896Z .................................................................................................... 6000/9883
2020-04-10T18:08:41.7731963Z ..................................................................................................ii 6100/9883
2020-04-10T18:08:54.8128387Z ...i..ii...........i................................................................................ 6200/9883
2020-04-10T18:09:13.7797989Z .................................................................................................... 6400/9883
2020-04-10T18:09:19.8463540Z .................................................................................................... 6500/9883
2020-04-10T18:09:19.8463540Z .................................................................................................... 6500/9883
2020-04-10T18:09:34.8596674Z ............................i..ii................................................................... 6600/9883
2020-04-10T18:09:57.9717583Z .................................................................................................... 6800/9883
2020-04-10T18:10:00.3109034Z ............................i....................................................................... 6900/9883
2020-04-10T18:10:02.5577027Z .................................................................................................... 7000/9883
2020-04-10T18:10:04.9441297Z ...................................................................i................................ 7100/9883
---
2020-04-10T18:11:53.2534886Z .................................................................................................... 7800/9883
2020-04-10T18:11:57.7251162Z .................................................................................................... 7900/9883
2020-04-10T18:12:05.2385295Z .................................................................................................... 8000/9883
2020-04-10T18:12:13.1307391Z ................................i................................................................... 8100/9883
2020-04-10T18:12:22.6947532Z ................................................................................iiiiii.iiii.i....... 8200/9883
2020-04-10T18:12:39.9365526Z .........................i......i................................................................... 8400/9883
2020-04-10T18:12:44.2650373Z .................................................................................................... 8500/9883
2020-04-10T18:12:56.5629224Z .................................................................................................... 8600/9883
2020-04-10T18:13:10.8623711Z .................................................................................................... 8700/9883
---
2020-04-10T18:15:50.8807105Z Suite("src/test/codegen") not skipped for "bootstrap::test::Codegen" -- not in ["src/tools/tidy"]
2020-04-10T18:15:50.9024421Z Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2020-04-10T18:15:51.1232467Z 
2020-04-10T18:15:51.1232945Z running 185 tests
2020-04-10T18:15:54.0816007Z iiii......i............ii.i..iiii....i....i...........i............i..i..................i....i..... 100/185
2020-04-10T18:15:57.0024210Z .......i.i.i...iii..iiiiiiiiiiiiiiii.......................iii...............ii......
2020-04-10T18:15:57.0027022Z 
2020-04-10T18:15:57.0032134Z  finished in 6.101
2020-04-10T18:15:57.0042574Z Suite("src/test/codegen-units") not skipped for "bootstrap::test::CodegenUnits" -- not in ["src/tools/tidy"]
2020-04-10T18:15:57.0269691Z Check compiletest suite=codegen-units mode=codegen-units (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
---
2020-04-10T18:15:59.3810788Z Suite("src/test/assembly") not skipped for "bootstrap::test::Assembly" -- not in ["src/tools/tidy"]
2020-04-10T18:15:59.4016882Z Check compiletest suite=assembly mode=assembly (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2020-04-10T18:15:59.5744179Z 
2020-04-10T18:15:59.5746913Z running 9 tests
2020-04-10T18:15:59.5748075Z iiiiiiiii
2020-04-10T18:15:59.5749082Z 
2020-04-10T18:15:59.5749237Z  finished in 0.172
2020-04-10T18:15:59.5758051Z Suite("src/test/incremental") not skipped for "bootstrap::test::Incremental" -- not in ["src/tools/tidy"]
2020-04-10T18:15:59.5968473Z Check compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
---
2020-04-10T18:16:21.8733056Z Suite("src/test/debuginfo") not skipped for "bootstrap::test::Debuginfo" -- not in ["src/tools/tidy"]
2020-04-10T18:16:21.9003560Z Check compiletest suite=debuginfo mode=debuginfo (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2020-04-10T18:16:22.1538774Z 
2020-04-10T18:16:22.1539136Z running 115 tests
2020-04-10T18:16:38.0483549Z iiiii..i.....i..i...i..i.i.i..i..i..ii....i.i....ii..........iiii.........i.....i..i.......ii.i.ii.. 100/115
2020-04-10T18:16:40.1412770Z ...iiii.....ii.
2020-04-10T18:16:40.1414375Z 
2020-04-10T18:16:40.1421818Z  finished in 18.242
2020-04-10T18:16:40.1431115Z Suite("src/test/ui-fulldeps") not skipped for "bootstrap::test::UiFullDeps" -- not in ["src/tools/tidy"]
2020-04-10T18:16:40.1433892Z Uplifting stage1 rustc (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
---
2020-04-10T18:29:47.4758300Z .................................................................................................... 400/490
2020-04-10T18:29:59.1522851Z ..........................................................................................
2020-04-10T18:29:59.1524002Z failures:
2020-04-10T18:29:59.1524274Z 
2020-04-10T18:29:59.1525518Z ---- collections/btree/map.rs - collections::btree::map::Entry::or_insert_with_key (line 2371) stdout ----
2020-04-10T18:29:59.1526559Z error[E0658]: use of unstable library feature 'or_insert_with_key'
2020-04-10T18:29:59.1527918Z  --> collections/btree/map.rs:2376:24
2020-04-10T18:29:59.1528120Z   |
2020-04-10T18:29:59.1528374Z 8 | map.entry("poneyland").or_insert_with_key(|key| key.chars().count());
2020-04-10T18:29:59.1529165Z   |
2020-04-10T18:29:59.1529165Z   |
2020-04-10T18:29:59.1529837Z   = note: see issue #70996 <***/issues/70996> for more information
2020-04-10T18:29:59.1530249Z   = help: add `#![feature(or_insert_with_key)]` to the crate attributes to enable
2020-04-10T18:29:59.1530872Z error: aborting due to previous error
2020-04-10T18:29:59.1531061Z 
2020-04-10T18:29:59.1531509Z For more information about this error, try `rustc --explain E0658`.
2020-04-10T18:29:59.1531953Z Couldn't compile the test.
---
2020-04-10T18:29:59.1594257Z 
2020-04-10T18:29:59.1594942Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test --exclude src/tools/tidy
2020-04-10T18:29:59.1595374Z Build completed unsuccessfully in 1:21:59
2020-04-10T18:29:59.1656615Z == clock drift check ==
2020-04-10T18:30:00.4971192Z   local time: Fri Apr 10 18:29:59 UTC 2020
2020-04-10T18:30:00.4980652Z   network time: Fri, 10 Apr 2020 18:29:59 GMT
2020-04-10T18:30:00.4995503Z 
2020-04-10T18:30:00.4995503Z 
2020-04-10T18:30:00.5031956Z ##[error]Bash exited with code '1'.
2020-04-10T18:30:00.5048300Z ##[section]Finishing: Run build
2020-04-10T18:30:00.5104696Z ##[section]Starting: Checkout rust-lang/rust@refs/pull/70996/merge to s
2020-04-10T18:30:00.5110779Z Task         : Get sources
2020-04-10T18:30:00.5111166Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
2020-04-10T18:30:00.5111503Z Version      : 1.0.0
2020-04-10T18:30:00.5111753Z Author       : Microsoft
2020-04-10T18:30:00.5111753Z Author       : Microsoft
2020-04-10T18:30:00.5112146Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkId=798199)
2020-04-10T18:30:00.5112583Z ==============================================================================
2020-04-10T18:30:00.9013497Z Cleaning any cached credential from repository: rust-lang/rust (GitHub)
2020-04-10T18:30:00.9018002Z ##[section]Finishing: Checkout rust-lang/rust@refs/pull/70996/merge to s
2020-04-10T18:30:00.9130297Z Cleaning up task key
2020-04-10T18:30:00.9131914Z Start cleaning up orphan processes.
2020-04-10T18:30:00.9352384Z Terminate orphan process: pid (3587) (python)
2020-04-10T18:30:00.9647688Z ##[section]Finishing: Finalize Job

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 @rust-lang/infra. (Feature Requests)

Copy link
Member

@Amanieu Amanieu left a comment

Choose a reason for hiding this comment

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

Thanks! Could you also submit a PR to hashbrown? I would like to keep the APIs in sync.

src/liballoc/collections/btree/map.rs Outdated Show resolved Hide resolved
@ChaiTRex
Copy link
Contributor Author

@Amanieu I've made a pull request to hashbrown at rust-lang/hashbrown#152.

bors added a commit to rust-lang/hashbrown that referenced this pull request Apr 11, 2020
Add or_insert_with_key to Entry of HashMap

Going along with `or_insert_with`, `or_insert_with_key` provides the `Entry`'s key to the lambda, avoiding the need to either clone the key or the need to reimplement this body of this method from scratch each time.

This is useful when the initial value for a map entry is derived from the key. For example, the introductory Rust book has an example Cacher struct that takes an expensive-to-compute lambda and then can, given an argument to the lambda, produce either the cached result or execute the lambda.

---

This is modified from rust-lang/rust#70996 and intended to sync the APIs of `std` and `hashbrown`.
@Amanieu
Copy link
Member

Amanieu commented Apr 11, 2020

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Apr 11, 2020

📌 Commit db0c39f has been approved by Amanieu

@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 Apr 11, 2020
bors added a commit to rust-lang/hashbrown that referenced this pull request Apr 11, 2020
Add or_insert_with_key to Entry of HashMap

Going along with `or_insert_with`, `or_insert_with_key` provides the `Entry`'s key to the lambda, avoiding the need to either clone the key or the need to reimplement this body of this method from scratch each time.

This is useful when the initial value for a map entry is derived from the key. For example, the introductory Rust book has an example Cacher struct that takes an expensive-to-compute lambda and then can, given an argument to the lambda, produce either the cached result or execute the lambda.

---

This is modified from rust-lang/rust#70996 and intended to sync the APIs of `std` and `hashbrown`.
Centril added a commit to Centril/rust that referenced this pull request Apr 11, 2020
Add or_insert_with_key to Entry of HashMap/BTreeMap

Going along with `or_insert_with`, `or_insert_with_key` provides the `Entry`'s key to the lambda, avoiding the need to either clone the key or the need to reimplement this body of this method from scratch each time.

This is useful when the initial value for a map entry is derived from the key. For example, the introductory Rust book has an example Cacher struct that takes an expensive-to-compute lambda and then can, given an argument to the lambda, produce either the cached result or execute the lambda.

---

I'm fairly new to Rust, so any optimizations, corrections to types, better names, better documentation, or whatever else would be appreciated. I'd like to thank Arnavion on freenode for helping me to implement a very similar method when I found that `or_insert_with_key` was unavailable.

As a somewhat-related note, this implements rust-lang/rfcs#1202 from 2015, so if this pull request is accepted, that should be closed.
bors added a commit to rust-lang/hashbrown that referenced this pull request Apr 11, 2020
Add or_insert_with_key to Entry of HashMap

Going along with `or_insert_with`, `or_insert_with_key` provides the `Entry`'s key to the lambda, avoiding the need to either clone the key or the need to reimplement this body of this method from scratch each time.

This is useful when the initial value for a map entry is derived from the key. For example, the introductory Rust book has an example Cacher struct that takes an expensive-to-compute lambda and then can, given an argument to the lambda, produce either the cached result or execute the lambda.

---

This is modified from rust-lang/rust#70996 and intended to sync the APIs of `std` and `hashbrown`.
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 11, 2020
Rollup of 5 pull requests

Successful merges:

 - rust-lang#70644 (Clean up `ModuleConfig` initialization)
 - rust-lang#70937 (Fix staticlib name for *-pc-windows-gnu targets)
 - rust-lang#70996 (Add or_insert_with_key to Entry of HashMap/BTreeMap)
 - rust-lang#71020 (Store UNICODE_VERSION as a tuple)
 - rust-lang#71021 (Use write!-style syntax for MIR assert terminator)

Failed merges:

r? @ghost
@bors bors merged commit 03a724b into rust-lang:master Apr 12, 2020
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-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants