Skip to content

std::slice::SliceConcatExt impls should rely on AsRef rather than Borrow #26780

Closed

Description

@SimonSapin

HashMap assumes that Borrow implies compatible hashing, which may not be desirable: servo/string-cache#89

Activity

  1. frewsxcv commented on Jul 5, 2015

    @frewsxcv
    Contributor
  2. bluss commented on Jul 5, 2015

    @bluss
    Contributor

    See discussion on #25162 why it was changed to Borrow

  3. steveklabnik commented on Jul 6, 2015

    @steveklabnik
    Contributor

    Yes, @aturon @alexcrichton , is this actually intended? Seems so

  4. steveklabnik commented on Jul 6, 2015

    @steveklabnik
    Contributor

    /cc @rust-lang/libs

  5. Gankra commented on Jul 6, 2015

    @Gankra
    Contributor

    To be clear: Almost the entire reason for Borrow to exist is to have "AsRef where Hash/Eq/Ord are expected to hold". (the rest of the reason is differing blanket impls in a world without specialization)

    (not clear on what @SimonSapin meant with "may not be desirable")

  6. alexcrichton commented on Jul 6, 2015

    @alexcrichton
    Member

    Yes this was switched to Borrow from AsRef as @bluss pointed out due to coherence issues. Whether or not this is desirable we cannot switch to AsRef today, so I'm going to close this as working as intended.

  7. SimonSapin commented on Jul 6, 2015

    @SimonSapin
    ContributorAuthor

    In string-cache we have:

    #[derive(PartialEq, Eq, Hash)]
    struct Atom { magic: u64 }
    impl Deref for Atom { Target = str; ... }

    The whole point of this library is to make Atom::eq fast. While we’re at it, we make Atom::hash fast in the same way. (That is, we only compare/hash the u64 value, not the str’s bytes.) The rest of the library ensures that *atom_1 == *atom_2 if and only if atom_1.magic == atom_2.magic.

    To make make Atom::hash fast, we make it not consistent with str::hash. Therefore, Atom should not implement Borrow<str>.

    SliceConcatExt however does not rely on hashing. Being able to use it on atoms would be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions