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 From<Cow<str>> for String and From<Cow<[T]>> for Vec<T>. #37326

Merged
merged 1 commit into from
Oct 23, 2016

Conversation

SimonSapin
Copy link
Contributor

Motivation: the selectors crate is generic over a string type, in order to support all of String, string_cache::Atom, and gecko_string_cache::Atom. Multiple trait bounds are used for the various operations done with these strings. One of these operations is creating a string (as efficiently as possible, re-using an existing memory allocation if possible) from Cow<str>.

The std::convert::From trait seems natural for this, but the relevant implementation was missing before this PR. To work around this I’ve added a FromCowStr trait in selectors, but with trait coherence that means one of selectors or string_cache needs to depend on the other to implement this trait. Using a trait from std would solve this.

The Vec<T> implementation is just added for consistency. I also tried a more general impl<'a, O, B: ?Sized + ToOwned<Owned=O>> From<Cow<'a, B>> for O, but (the compiler thinks?) it conflicts with From<T> for T the impl (after moving all of collections::borrow into core::borrow to work around trait coherence).

Motivation: the `selectors` crate is generic over a string type,
in order to support all of `String`, `string_cache::Atom`, and
`gecko_string_cache::Atom`. Multiple trait bounds are used
for the various operations done with these strings.
One of these operations is creating a string (as efficiently as possible,
re-using an existing memory allocation if possible) from `Cow<str>`.

The `std::convert::From` trait seems natural for this, but
the relevant implementation was missing before this PR.
To work around this I’ve added a `FromCowStr` trait in `selectors`,
but with trait coherence that means one of `selectors` or `string_cache`
needs to depend on the other to implement this trait.
Using a trait from `std` would solve this.

The `Vec<T>` implementation is just added for consistency.
I also tried a more general
`impl<'a, O, B: ?Sized + ToOwned<Owned=O>> From<Cow<'a, B>> for O`,
but (the compiler thinks?) it conflicts with `From<T> for T` the impl
(after moving all of `collections::borrow` into `core::borrow`
to work around trait coherence).
@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 @aturon (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.

@alexcrichton
Copy link
Member

@bors: r+

cc @rust-lang/libs, new From impls!

In general we're pretty much always on board with adding various From impls afaik.

@bors
Copy link
Contributor

bors commented Oct 21, 2016

📌 Commit 7e603d4 has been approved by alexcrichton

@alexcrichton alexcrichton added the relnotes Marks issues that should be documented in the release notes of the next release. label Oct 21, 2016
@bluss
Copy link
Member

bluss commented Oct 21, 2016

@alexcrichton Wait, last time we regretted it, see #36685.

@alexcrichton
Copy link
Member

@bors: r-

Indeed! Let's get a crater run this time.

@alexcrichton alexcrichton added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Oct 21, 2016
@eddyb
Copy link
Member

eddyb commented Oct 22, 2016

Crater report looks clean.

@bors r=alexcrichton

@bors
Copy link
Contributor

bors commented Oct 22, 2016

📌 Commit 7e603d4 has been approved by alexcrichton

@bluss bluss removed the S-waiting-on-crater Status: Waiting on a crater run to be completed. label Oct 22, 2016
Manishearth added a commit to Manishearth/rust that referenced this pull request Oct 22, 2016
Implement `From<Cow<str>> for String` and `From<Cow<[T]>> for Vec<T>`.

Motivation: the `selectors` crate is generic over a string type, in order to support all of `String`,  `string_cache::Atom`, and `gecko_string_cache::Atom`. Multiple trait bounds are used for the various operations done with these strings. One of these operations is creating a string (as efficiently as possible, re-using an existing memory allocation if possible) from `Cow<str>`.

The `std::convert::From` trait seems natural for this, but the relevant implementation was missing before this PR. To work around this I’ve added a `FromCowStr` trait in `selectors`, but with trait coherence that means one of `selectors` or `string_cache` needs to depend on the other to implement this trait. Using a trait from `std` would solve this.

The `Vec<T>` implementation is just added for consistency. I also tried a more general `impl<'a, O, B: ?Sized + ToOwned<Owned=O>> From<Cow<'a, B>> for O`, but (the compiler thinks?) it conflicts with `From<T> for T` the impl (after moving all of `collections::borrow` into `core::borrow` to work around trait coherence).
@cristicbz
Copy link
Contributor

cristicbz commented Oct 22, 2016

Are we generally OK with allocating From conversions? I was wondering if something like impl<'a, B: ?Sized + ToOwned> From<&'a B> for B::Owned { /* ... */ } would be theoretically acceptable (ignoring coherence issues etc. for now) in the context of the Entry API extension RFC.

@SimonSapin
Copy link
Contributor Author

From<&'a str> for String exists and definitely allocates, for what it’s worth.

@cristicbz
Copy link
Contributor

@SimonSapin Good point, thanks!

@bors
Copy link
Contributor

bors commented Oct 23, 2016

⌛ Testing commit 7e603d4 with merge febfe76...

bors added a commit that referenced this pull request Oct 23, 2016
Implement `From<Cow<str>> for String` and `From<Cow<[T]>> for Vec<T>`.

Motivation: the `selectors` crate is generic over a string type, in order to support all of `String`,  `string_cache::Atom`, and `gecko_string_cache::Atom`. Multiple trait bounds are used for the various operations done with these strings. One of these operations is creating a string (as efficiently as possible, re-using an existing memory allocation if possible) from `Cow<str>`.

The `std::convert::From` trait seems natural for this, but the relevant implementation was missing before this PR. To work around this I’ve added a `FromCowStr` trait in `selectors`, but with trait coherence that means one of `selectors` or `string_cache` needs to depend on the other to implement this trait. Using a trait from `std` would solve this.

The `Vec<T>` implementation is just added for consistency. I also tried a more general `impl<'a, O, B: ?Sized + ToOwned<Owned=O>> From<Cow<'a, B>> for O`, but (the compiler thinks?) it conflicts with `From<T> for T` the impl (after moving all of `collections::borrow` into `core::borrow` to work around trait coherence).
@bors bors merged commit 7e603d4 into rust-lang:master Oct 23, 2016
@SimonSapin SimonSapin deleted the from-cow branch October 26, 2016 18:27
@SimonSapin
Copy link
Contributor Author

SimonSapin commented Oct 26, 2016

I’ve just realized that I don’t strictly need a From<Cow<str>> bound. I can require From<String> + From<&str> and match on cows: servo/rust-selectors@a87416f

(I still think this PR is a good addition.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants