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

Implement AsMut<String> for String #69704

Closed
wants to merge 1 commit into from
Closed

Conversation

nwtnni
Copy link

@nwtnni nwtnni commented Mar 4, 2020

Related to #68741 and #68742.

There's a parallel implementation of AsMut<[T]> for Vec<T> and AsMut<Vec<T>> for Vec<T>, so I thought we might want to do the same for String?

EDIT: actually, would it make more sense to just write the following global implementations?

impl<T: ?Sized> AsRef<T> for T {
    fn as_ref(&self) -> &T {
        self
    }
}

impl<T: ?Sized> AsMut<T> for T {
    fn as_mut(&mut self) -> &mut T {
        self
    }
}

Similarly to what the Borrow and BorrowMut traits do? This is my first time contributing, so I'm not sure if this would require an issue or RFC--please let me know if there's a more appropriate place to suggest this.

@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 @shepmaster (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 Mar 4, 2020
@shepmaster
Copy link
Member

There's a parallel implementation of AsMut<[T]> for Vec<T> and AsMut<Vec<T>> for Vec<T>,

Because we already have the equivalent for Vec<T>, this change makes sense to me. It would be good to review how deliberate that existing implementation was, ensuring that it wasn't an accident we want to not propagate.

However, this modifies the public API, so I'm going to kick it to the libs team...

r? @LukasKalbertodt

would it make more sense to just write the following global implementations

Perhaps, but the lack of a blanket implementation seems deliberate:

AsRef has the same signature as Borrow, but Borrow is different in few aspects:

Unlike AsRef, Borrow has a blanket impl for any T, and can be used to accept either a reference or a value.

@nwtnni
Copy link
Author

nwtnni commented Mar 5, 2020

Perhaps, but the lack of a blanket implementation seems deliberate:

AsRef has the same signature as Borrow, but Borrow is different in few aspects:

Unlike AsRef, Borrow has a blanket impl for any T, and can be used to accept either a reference or a value.

You're right, I do see that in the AsRef docs. The implementations of AsRef<Vec<T>> and AsMut<Vec<T>> for Vec<T> seem to be the odd ones out, looking at the other implementations, so I don't mind closing this PR.

@JohnCSimon JohnCSimon 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 Mar 11, 2020
@JohnCSimon
Copy link
Member

Ping from triage:
@nwtnni if no more work is going to happen with this PR can you please close it? Thanks.

@nwtnni
Copy link
Author

nwtnni commented Mar 11, 2020

Ping from triage:
@nwtnni if no more work is going to happen with this PR can you please close it? Thanks.

@LukasKalbertodt, can I get a quick confirmation before I close?

@LukasKalbertodt
Copy link
Member

@nwtnni I wanted to think about it a bit more, but had no time for it yet. Sorry! Will do in the coming day and then comment here. You can keep this open until then.

@LukasKalbertodt
Copy link
Member

I still haven't completely understood the design decisions behind Borrow and AsRef, but yes: the existing Vec impl seems to be a special case. So let's close this for now, as already suggested.

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.

5 participants