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 a generic From<&Borrow> impl for Cow #48191

Closed
wants to merge 7 commits into from

Conversation

burtonageo
Copy link
Contributor

@burtonageo burtonageo commented Feb 13, 2018

This commit removes the ad-hoc impls of From<&Borrow> for Cow, and replaces them with a generic From impl across all &Borrow<T>. I am unsure of how the stability attributes should work, so I will need some guidance to get this in good shape to submit.

The practical effects of this commit are:

  • There are now From<&Borrow> impls derived for Cow<CStr> and Cow<OsStr>, as well as any other ecosystem types that can be borrowed (e.g. ascii types)
  • From<&Owned> is now auto implemented. This fixes some issues with using Into<Cow<T>> in generic situations, as shown in this playground.

Some other issues:

  • I attempted to add an impl<T: ToOwned> From<T::Owned> for Cow<T>, however it conflicted with the reflexive From impl in core. Thus, those impls will still have to be implemented in an ad-hoc way until maybe specialization could solve it.

@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 @alexcrichton (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.

@burtonageo burtonageo changed the title Add a generic From<Borrow> impl for Cow Add a generic From<&Borrow> impl for Cow Feb 13, 2018
@pietroalbini pietroalbini added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 13, 2018
@egilburg
Copy link

Does this replace a stable function with an unstable one?

@burtonageo
Copy link
Contributor Author

@egilburg I believe that trait impl blocks aren't checked for stability, so I think it could be made stable. Furthermore, this impl should cover strictly more cases than the original impls do, so there should be no source breakage with this change.

@egilburg
Copy link

@burtonageo Fair enough, it's just that currently the code is marked "unstable", so is the marker even needed if it's not actually taking effect, and especially since it logically needs to be stable to preserve existing behavior?

@burtonageo
Copy link
Contributor Author

@egilburg It was just a way to mark it as WIP, but I guess there's not much point marking it as unstable, so I'll update it now

@shepmaster
Copy link
Member

Ping from triage, @alexcrichton — will you be able to give this a review sometime soon?

@Mark-Simulacrum
Copy link
Member

I believe @alexcrichton is mostly absent for a few weeks, so r? @BurntSushi

@BurntSushi
Copy link
Member

r? @aturon

@rust-highfive rust-highfive assigned aturon and unassigned BurntSushi Feb 17, 2018
@pietroalbini
Copy link
Member

@aturon, or someone else from @rust-lang/libs, can we get a review on this PR?

@alexcrichton
Copy link
Member

Thanks for the PR @burtonageo! I think technically this is a breaking change, right? As such, I think we may want to get a crater run to assess breakage, but would want to confirm first you agree that it's a breaking change.

@alexcrichton alexcrichton assigned alexcrichton and unassigned aturon Feb 26, 2018
@burtonageo
Copy link
Contributor Author

@alexcrichton Thanks for the review! According to the rfc on breaking changes (under the section "generalising to generics"), I believe this counts as a minor semver change. However, that section does state that this change could create type inference failures, and thus I agree that this is a breaking change.

@withoutboats
Copy link
Contributor

I believe this is a breaking change (not one we usually allow). It would be incoherent with implementations like this, which are allowed:

#[derive(Clone)]
struct X;

impl<'a> From<&'a X> for Cow<'a, X> {
    fn from(x: &'a X) -> Self {
         Cow::Borrowed(x)
    }
}

@alexcrichton
Copy link
Member

@bors: try

@rust-lang/infra could a crate run be scheduled for this PR?

@bors
Copy link
Contributor

bors commented Feb 27, 2018

⌛ Trying commit 75bf333 with merge 1864114421fdd4165e02ca3ad67dfc6e42174ba3...

@kennytm kennytm added the S-waiting-on-crater Status: Waiting on a crater run to be completed. label Feb 27, 2018
@kennytm kennytm removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 27, 2018
@bors
Copy link
Contributor

bors commented Feb 27, 2018

☀️ Test successful - status-travis
State: approved= try=True

@Mark-Simulacrum
Copy link
Member

Crater started

@Mark-Simulacrum
Copy link
Member

Crater results: http://cargobomb-reports.s3.amazonaws.com/pr-48191/index.html. 'Blacklisted' crates (spurious failures etc) can be found
here.
If you see any spurious failures not on the list, please make a PR against that file.

@pietroalbini pietroalbini added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Mar 10, 2018
@alexcrichton
Copy link
Member

Thanks for the crater run! I glanced at all the git-related logs at the bottom and they're all breaking due to this change, so unfortunately looks like we won't be able to do this.

kennytm added a commit to kennytm/rust that referenced this pull request May 16, 2018
…chton

Implement From for more types on Cow

This is basically rust-lang#48191, except that it should be implemented in a way that doesn't break third party crates.
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.