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 AsRef, as_slice, as_str to more things #65901

Closed
wants to merge 2 commits into from

Conversation

clarfonthey
Copy link
Contributor

This is somewhat related to #58957.

When writing some code I noticed the disparity between CharIndices<'_> and Enumerate<Iter<'_, T>>; while you can get the original slice from the former, you can't from the latter.

Essentially, I chose a relatively conservative list of adapters that would reasonably add this functionality where this would never ultimately lead to unexpected behaviour. For example, having as_ref for Filter or StepBy would likely lead to items being shown that aren't part of the iterator, which would be confusing. However, Cloned can safely assumed to iterate its items in order, and adding as_ref to that wouldn't be to weird.

Ultimately, this fills in a few holes that seemed reasonable to fill, namely:

  • str::Bytes has an as_bytes method now and implements AsRef<[u8]>,
  • str::Chars and str::CharIndices now implement AsRef<str> and AsRef<[u8]>. (unsure if OsStr and Path should be included, or [u8] should be excluded)
  • iter::{Copied, Cloned, Enumerate} will have an as_slice method when wrapped around a slice::Iter or slice::IterMut, and an AsRef<[T]> implementation when wrapping slice::Iter.

This honestly seems like an ever-deepening rabbit hole and I'm not sure where it should stop. That said, I feel that adding these methods to the above is shallow enough it shouldn't be too controversial.

Curious to see what people's thoughts are.

@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(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 Oct 28, 2019
@jonas-schievink jonas-schievink added relnotes Marks issues that should be documented in the release notes of the next release. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 28, 2019
@jonas-schievink jonas-schievink added this to the 1.40 milestone Oct 28, 2019
@@ -42,7 +42,7 @@ __pycache__/
/src/libcore/unicode/SpecialCasing.txt
/src/libcore/unicode/UnicodeData.txt
/src/libcore/unicode/downloaded
/target/
target/
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes; I should have put it in a separate commit. RLS puts the target/ directory in src/libcore for me.

@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-10-28T19:08:14.6192181Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-10-28T19:08:14.6872131Z ##[command]git config gc.auto 0
2019-10-28T19:08:14.6954191Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-10-28T19:08:14.7011076Z ##[command]git config --get-all http.proxy
2019-10-28T19:08:14.7170442Z ##[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/65901/merge:refs/remotes/pull/65901/merge
---
2019-10-28T20:07:20.2028443Z .................................................................................................... 1600/9259
2019-10-28T20:07:25.8026555Z .................................................................................................... 1700/9259
2019-10-28T20:07:37.8204855Z ..........................................................i...............i......................... 1800/9259
2019-10-28T20:07:45.2162759Z .................................................................................................... 1900/9259
2019-10-28T20:07:59.7670163Z ................................................iiiii............................................... 2000/9259
2019-10-28T20:08:10.2468043Z .................................................................................................... 2200/9259
2019-10-28T20:08:12.7577474Z .................................................................................................... 2300/9259
2019-10-28T20:08:16.4124950Z .................................................................................................... 2400/9259
2019-10-28T20:08:39.3319709Z .................................................................................................... 2500/9259
---
2019-10-28T20:11:26.4772015Z .................................................i...............i.................................. 4800/9259
2019-10-28T20:11:35.1399868Z .................................................................................................... 4900/9259
2019-10-28T20:11:43.6459867Z .................................................................................................... 5000/9259
2019-10-28T20:11:49.7876691Z .................................................................................................... 5100/9259
2019-10-28T20:11:59.9100065Z ..................................................ii.ii...........i................................. 5200/9259
2019-10-28T20:12:09.3644842Z .................................................................................................... 5400/9259
2019-10-28T20:12:18.3647028Z .................................................................................................... 5500/9259
2019-10-28T20:12:26.0085117Z ....................i............................................................................... 5600/9259
2019-10-28T20:12:31.8238989Z .................................................................................................... 5700/9259
2019-10-28T20:12:31.8238989Z .................................................................................................... 5700/9259
2019-10-28T20:12:43.2950221Z .................................................................................................... 5800/9259
2019-10-28T20:12:55.2801727Z .....ii...i..ii...........i......................................................................... 5900/9259
2019-10-28T20:13:16.3098142Z .................................................................................................... 6100/9259
2019-10-28T20:13:22.6564662Z .................................................................................................... 6200/9259
2019-10-28T20:13:22.6564662Z .................................................................................................... 6200/9259
2019-10-28T20:13:36.4147979Z ........................i..ii....................................................................... 6300/9259
2019-10-28T20:13:56.1341423Z ..........................................................................................i......... 6500/9259
2019-10-28T20:13:58.3539537Z .................................................................................................... 6600/9259
2019-10-28T20:14:00.6136665Z .................................................................i.................................. 6700/9259
2019-10-28T20:14:03.5352123Z .................................................................................................... 6800/9259
---
2019-10-28T20:18:27.1992489Z  finished in 5.487
2019-10-28T20:18:27.2173803Z Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-10-28T20:18:27.3762706Z 
2019-10-28T20:18:27.3763614Z running 153 tests
2019-10-28T20:18:30.4662612Z i....iii......iii..iiii...i.............................i..i..................i....i...........ii.i. 100/153
2019-10-28T20:18:32.3761335Z i..iiii..............i.........iii.i.........ii......
2019-10-28T20:18:32.3761936Z 
2019-10-28T20:18:32.3767925Z  finished in 5.159
2019-10-28T20:18:32.3954925Z Check compiletest suite=codegen-units mode=codegen-units (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-10-28T20:18:32.5572117Z 
---
2019-10-28T20:18:34.4820129Z  finished in 2.086
2019-10-28T20:18:34.5017088Z Check compiletest suite=assembly mode=assembly (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-10-28T20:18:34.6555263Z 
2019-10-28T20:18:34.6555507Z running 9 tests
2019-10-28T20:18:34.6556272Z iiiiiiiii
2019-10-28T20:18:34.6556613Z 
2019-10-28T20:18:34.6556657Z  finished in 0.153
2019-10-28T20:18:34.6734154Z Check compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-10-28T20:18:34.8317190Z 
---
2019-10-28T20:18:52.4653803Z  finished in 17.792
2019-10-28T20:18:52.4860697Z Check compiletest suite=debuginfo mode=debuginfo-gdb+lldb (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-10-28T20:18:52.6436978Z 
2019-10-28T20:18:52.6437505Z running 123 tests
2019-10-28T20:19:16.1741688Z .iiiii...i.....i..i...i..i.i.i..i.ii..i.i.....i..i....ii..........iiii..........i...ii...i.......ii. 100/123
2019-10-28T20:19:20.7198600Z i.i.i......iii.i.....ii
2019-10-28T20:19:20.7200495Z 
2019-10-28T20:19:20.7204267Z  finished in 28.234
2019-10-28T20:19:20.7213935Z Uplifting stage1 rustc (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-10-28T20:19:20.7214491Z Copying stage2 rustc from stage1 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu / x86_64-unknown-linux-gnu)
---
2019-10-28T20:31:35.1415604Z 
2019-10-28T20:31:35.1416634Z    Doc-tests core
2019-10-28T20:31:40.1027180Z 
2019-10-28T20:31:40.1027982Z running 2409 tests
2019-10-28T20:31:50.7127808Z ......iiiii......................................................................................... 100/2409
2019-10-28T20:32:01.1500562Z ................................................................................ii.................. 200/2409
2019-10-28T20:32:25.6467350Z ..i................................................................................................. 400/2409
2019-10-28T20:32:25.6467350Z ..i................................................................................................. 400/2409
2019-10-28T20:32:35.7458507Z .................................................i..i.................iiii.......................... 500/2409
2019-10-28T20:32:55.2728351Z .................................................................................................... 700/2409
2019-10-28T20:33:05.1972057Z .................................................................................................... 800/2409
2019-10-28T20:33:15.1872226Z .................................................................................................... 900/2409
2019-10-28T20:33:25.0324752Z .................................................................................................... 1000/2409
---
2019-10-28T20:37:29.3547319Z 
2019-10-28T20:37:29.3547540Z running 999 tests
2019-10-28T20:37:50.2257025Z i................................................................................................... 100/999
2019-10-28T20:38:01.9293223Z .................................................................................................... 200/999
2019-10-28T20:38:10.0792072Z ...................iii......i......i...i......i..................................................... 300/999
2019-10-28T20:38:15.5096556Z .................................................................................................... 400/999
2019-10-28T20:38:23.0745022Z ..........................................i..i.................................ii................... 500/999
2019-10-28T20:38:37.4570090Z .................................................................................................... 700/999
2019-10-28T20:38:37.4570090Z .................................................................................................... 700/999
2019-10-28T20:38:45.7673016Z .........................iiii....................................................................... 800/999
2019-10-28T20:39:00.4730031Z .................................................................................................... 900/999
2019-10-28T20:39:08.6317225Z ...............................................iiii................................................
2019-10-28T20:39:08.6319851Z 
2019-10-28T20:39:08.6424333Z  finished in 192.165
2019-10-28T20:39:08.6441100Z Testing term stage1 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-10-28T20:39:08.8420317Z    Compiling term v0.0.0 (/checkout/src/libterm)
---
2019-10-28T20:52:47.9819940Z     Checking compiler_builtins v0.1.18
2019-10-28T20:52:48.4356022Z  Documenting alloc v0.0.0 (/checkout/src/liballoc)
2019-10-28T20:52:51.6908008Z     Finished release [optimized] target(s) in 24.02s
2019-10-28T20:52:52.0226411Z  Documenting core v0.0.0 (/checkout/src/libcore)
2019-10-28T20:53:04.1871689Z error: `[slice::iter::as_slice]` cannot be resolved, ignoring it...
2019-10-28T20:53:04.1873852Z    --> src/libcore/iter/adapters/mod.rs:246:28
2019-10-28T20:53:04.1874432Z     |
2019-10-28T20:53:04.1875351Z 246 |     /// [`Iter::as_slice`][slice::iter::as_slice].
2019-10-28T20:53:04.1876333Z     |
2019-10-28T20:53:04.1876760Z note: lint level defined here
2019-10-28T20:53:04.1877173Z    --> src/libcore/lib.rs:64:9
2019-10-28T20:53:04.1877599Z     |
2019-10-28T20:53:04.1877599Z     |
2019-10-28T20:53:04.1878238Z 64  | #![deny(intra_doc_link_resolution_failure)] // rustdoc is run without -D warnings
2019-10-28T20:53:04.1879266Z     = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`
2019-10-28T20:53:04.1879430Z 
2019-10-28T20:53:04.1879430Z 
2019-10-28T20:53:04.1879953Z error: `[slice::iter::as_slice]` cannot be resolved, ignoring it...
2019-10-28T20:53:04.1880401Z    --> src/libcore/iter/adapters/mod.rs:246:28
2019-10-28T20:53:04.1880776Z     |
2019-10-28T20:53:04.1881233Z 246 |     /// [`Iter::as_slice`][slice::iter::as_slice].
2019-10-28T20:53:04.1882457Z     |
2019-10-28T20:53:04.1882962Z     = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`
2019-10-28T20:53:04.1883128Z 
2019-10-28T20:53:04.1883128Z 
2019-10-28T20:53:04.1883534Z error: `[slice::iter::as_slice]` cannot be resolved, ignoring it...
2019-10-28T20:53:04.1883981Z    --> src/libcore/iter/adapters/mod.rs:254:28
2019-10-28T20:53:04.1884357Z     |
2019-10-28T20:53:04.1884965Z 254 |     /// [`Iter::as_slice`][slice::iter::as_slice].
2019-10-28T20:53:04.1886080Z     |
2019-10-28T20:53:04.1886581Z     = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`
2019-10-28T20:53:04.1886747Z 
2019-10-28T20:53:04.1886747Z 
2019-10-28T20:53:04.1887150Z error: `[slice::iter::as_slice]` cannot be resolved, ignoring it...
2019-10-28T20:53:04.1887641Z    --> src/libcore/iter/adapters/mod.rs:392:28
2019-10-28T20:53:04.1888139Z     |
2019-10-28T20:53:04.1888589Z 392 |     /// [`Iter::as_slice`][slice::iter::as_slice].
2019-10-28T20:53:04.1889497Z     |
2019-10-28T20:53:04.1889974Z     = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`
2019-10-28T20:53:04.1890241Z 
2019-10-28T20:53:04.1890241Z 
2019-10-28T20:53:04.1890620Z error: `[slice::iter::as_slice]` cannot be resolved, ignoring it...
2019-10-28T20:53:04.1891036Z    --> src/libcore/iter/adapters/mod.rs:400:28
2019-10-28T20:53:04.1891387Z     |
2019-10-28T20:53:04.1891831Z 400 |     /// [`Iter::as_slice`][slice::iter::as_slice].
2019-10-28T20:53:04.1892669Z     |
2019-10-28T20:53:04.1893154Z     = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`
2019-10-28T20:53:04.1893311Z 
2019-10-28T20:53:04.1893311Z 
2019-10-28T20:53:04.1893687Z error: `[slice::iter::as_slice]` cannot be resolved, ignoring it...
2019-10-28T20:53:04.1894107Z     --> src/libcore/iter/adapters/mod.rs:1280:28
2019-10-28T20:53:04.1894823Z      |
2019-10-28T20:53:04.1895426Z 1280 |     /// [`Iter::as_slice`][slice::iter::as_slice].
2019-10-28T20:53:04.1896345Z      |
2019-10-28T20:53:04.1897523Z      = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`
2019-10-28T20:53:04.1897585Z 
2019-10-28T20:53:04.1897585Z 
2019-10-28T20:53:04.1897976Z error: `[slice::iter::as_slice]` cannot be resolved, ignoring it...
2019-10-28T20:53:04.1898411Z     --> src/libcore/iter/adapters/mod.rs:1288:28
2019-10-28T20:53:04.1898609Z      |
2019-10-28T20:53:04.1898876Z 1288 |     /// [`Iter::as_slice`][slice::iter::as_slice].
2019-10-28T20:53:04.1899417Z      |
2019-10-28T20:53:04.1899704Z      = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`
2019-10-28T20:53:04.1899757Z 
2019-10-28T20:53:04.5987564Z error: aborting due to 7 previous errors
2019-10-28T20:53:04.5987564Z error: aborting due to 7 previous errors
2019-10-28T20:53:04.5987718Z 
2019-10-28T20:53:04.7027757Z error: Could not document `core`.
2019-10-28T20:53:04.7027915Z 
2019-10-28T20:53:04.7027968Z Caused by:
2019-10-28T20:53:04.7029030Z   process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustdoc --edition=2018 --crate-type lib --crate-name core src/libcore/lib.rs --target x86_64-unknown-linux-gnu -o /checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/doc --color always --markdown-css rust.css --markdown-no-toc --generate-redirect-pages --resource-suffix 1.40.0 --index-page /checkout/src/doc/index.md -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/release/deps -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/release/deps` (exit code: 1)
2019-10-28T20:53:04.7045117Z 
2019-10-28T20:53:04.7045117Z 
2019-10-28T20:53:04.7046112Z command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "rustdoc" "-Zconfig-profile" "--target" "x86_64-unknown-linux-gnu" "-Zbinary-dep-depinfo" "-j" "2" "--release" "--locked" "--color" "always" "--features" "panic-unwind backtrace compiler-builtins-c" "--manifest-path" "/checkout/src/libtest/Cargo.toml" "-Z" "unstable-options" "-p" "core" "--" "--markdown-css" "rust.css" "--markdown-no-toc" "--generate-redirect-pages" "--resource-suffix" "1.40.0" "--index-page" "/checkout/src/doc/index.md"
2019-10-28T20:53:04.7046257Z 
2019-10-28T20:53:04.7046305Z 
2019-10-28T20:53:04.7052252Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
2019-10-28T20:53:04.7052324Z Build completed unsuccessfully in 1:38:01
2019-10-28T20:53:04.7052324Z Build completed unsuccessfully in 1:38:01
2019-10-28T20:53:04.7105842Z == clock drift check ==
2019-10-28T20:53:04.7122399Z   local time: Mon Oct 28 20:53:04 UTC 2019
2019-10-28T20:53:04.8560078Z   network time: Mon, 28 Oct 2019 20:53:04 GMT
2019-10-28T20:53:04.8560267Z == end clock drift check ==
2019-10-28T20:53:07.2292602Z 
2019-10-28T20:53:07.2389532Z ##[error]Bash exited with code '1'.
2019-10-28T20:53:07.2440687Z ##[section]Starting: Checkout
2019-10-28T20:53:07.2442374Z ==============================================================================
2019-10-28T20:53:07.2442425Z Task         : Get sources
2019-10-28T20:53:07.2442466Z 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)

@JohnCSimon
Copy link
Member

Ping from triage.
@Mark-Simulacrum Can you please review this PR?
cc: @clarfon @hellow554

Thank you!

@Mark-Simulacrum
Copy link
Member

This is libs territory, sorry, thought I'd already done this :)

r? @dtolnay

@Centril Centril modified the milestones: 1.40, 1.41 Nov 7, 2019
src/libcore/iter/adapters/mod.rs Outdated Show resolved Hide resolved
src/libcore/iter/adapters/mod.rs Outdated Show resolved Hide resolved
src/libcore/iter/adapters/mod.rs Outdated Show resolved Hide resolved
src/libcore/str/mod.rs Outdated Show resolved Hide resolved
src/libcore/str/mod.rs Outdated Show resolved Hide resolved
src/libcore/str/mod.rs Outdated Show resolved Hide resolved
src/libcore/str/mod.rs Outdated Show resolved Hide resolved
src/libcore/str/mod.rs Outdated Show resolved Hide resolved
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
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.

Thanks for the PR! I am going to close because I don't feel that any of the methods or impls added here are sufficiently motivated.

I agree with you that adding associated methods to permutations of iterator adaptors is a rabbit hole.

Regarding the various AsRef impls, I guess I don't understand the use case. It's pretty uncommon for callers to need the genericness of AsRef in writing clear code. Do we really need someone to be able to make a CharIndices and pass it to fs::write in order to express what they mean in the clearest way? Not that permitting it would be bad, it's just not a need that people have and so it's not something that is worth designing for.

In my view the bar for adding surface area to the standard library isn't whether the behavior would be unambiguous to a reasonable observer ("never ultimately lead to unexpected behaviour" in your words), but whether people can get done what they need to get done with an effort that is proportional to how common their individual use case is. Instead of adding the most amount of surface area that remains unambiguous, we need to add the least amount of surface area that makes a usable library.

@dtolnay dtolnay closed this Nov 10, 2019
@Centril Centril removed the relnotes Marks issues that should be documented in the release notes of the next release. label Nov 13, 2019
@Centril Centril removed this from the 1.41 milestone Nov 13, 2019
@clarfonthey clarfonthey deleted the as_ref branch January 29, 2022 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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.

8 participants