-
Notifications
You must be signed in to change notification settings - Fork 13.3k
make Option::cloned generic over Deref, make cloned's tests actually run, add Cloned iterator adaptor #19060
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
Conversation
r? @aturon |
impl<'a, T: Clone> Option<&'a T> { | ||
/// Maps an Option<&T> to an Option<T> by cloning the contents of the Option<&T>. | ||
impl<'a, T: Clone, D: Deref<T>> Option<D> { | ||
/// Maps an Option<Deref<T>> to an Option<T> by cloning the contents of the Option. |
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.
Remember that with DST, Deref<T>
is actually a type (and in general Foo<Deref<T>>
is a thing). This comment probably should be the more verbose "Maps Option<T>
to Option<U>
by derefencing and cloning".
Slick. r=me with minor nit addressed. |
1 similar comment
Slick. r=me with minor nit addressed. |
} | ||
|
||
impl<A: Clone, D: Deref<A>, I: Iterator<D>> Iterator<A> for Cloned<I> { | ||
fn next(&mut self) -> Option<A> { |
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.
Add #[inline]
to this and other one-line functions?
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.
It's generic, so it's already a candidate for cross-crate inlining. Otherwise I'd rather avoid adding more #[inline]s to std without any performance information.
@aturon Updated description of Option::cloned, look good to you? |
Edit: whoops, didn't mean to hit post. Anyway, this is something I tried to do when I first implemented cloned, but couldn't figure out. Somewhere between then and the PR actually landing, we got Deref of references, so now this works! 🎉 Also turns out the test for the functionality was never marked as a #[test]. Oops! Also added a Cloned iterator adaptor. If this isn't desirable, it can be taken out of the PR (seperate commits).
So, I was talking to @alexcrichton about this PR, and realized that I probably approved it a little too hastily; before we stabilize this API, I'd like to know what the concrete use cases are that need to cut through |
This was primarily motivated by @SimonSapin's frustration with the ergonomics of getting a by-value iterator over a [u8]. They proposed an explicit by-value iterator, but I suggested this design as more useful and composable. So now you can do I honestly expected this to be discussed more before being merged! (hence my note of "If this isn't desirable, it can be taken out of the PR") |
@gankro Yeah, it was my bad for rubber-stamping it; that's what I get for reviewing in a hurry. The idea of having |
Yeah I didn't mark it as unstable or anything, so it's experimental. The primary motivation for Deref is that it captures &T and &mut T. Everything else is just gravy. |
Oh durr I could have done this with IntoOwned, I guess. Which is basically Super Deref+Clone. err wait, has that landed? |
For posterity: we need |
Edit: whoops, didn't mean to hit post.
Anyway, this is something I tried to do when I first implemented cloned, but couldn't figure out. Somewhere between then and the PR actually landing, we got Deref of references, so now this works! 🎉
Also turns out the test for the functionality was never marked as a #[test]. Oops!
Also added a Cloned iterator adaptor. If this isn't desirable, it can be taken out of the PR (seperate commits).