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

Commits on Oct 21, 2016

  1. 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).
    SimonSapin committed Oct 21, 2016
    Configuration menu
    Copy the full SHA
    7e603d4 View commit details
    Browse the repository at this point in the history