-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
12a83c7
to
26b6aec
Compare
@@ -42,7 +42,7 @@ __pycache__/ | |||
/src/libcore/unicode/SpecialCasing.txt | |||
/src/libcore/unicode/UnicodeData.txt | |||
/src/libcore/unicode/downloaded | |||
/target/ | |||
target/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intentional?
There was a problem hiding this comment.
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.
The job Click to expand the log.
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 |
Ping from triage. Thank you! |
This is libs territory, sorry, thought I'd already done this :) r? @dtolnay |
There was a problem hiding this 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.
This is somewhat related to #58957.
When writing some code I noticed the disparity between
CharIndices<'_>
andEnumerate<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
forFilter
orStepBy
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 addingas_ref
to that wouldn't be to weird.Ultimately, this fills in a few holes that seemed reasonable to fill, namely:
str::Bytes
has anas_bytes
method now and implementsAsRef<[u8]>
,str::Chars
andstr::CharIndices
now implementAsRef<str>
andAsRef<[u8]>
. (unsure ifOsStr
andPath
should be included, or[u8]
should be excluded)iter::{Copied, Cloned, Enumerate}
will have anas_slice
method when wrapped around aslice::Iter
orslice::IterMut
, and anAsRef<[T]>
implementation when wrappingslice::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.