Skip to content

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

Closed
@SimonSapin

Description

@SimonSapin
Contributor

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

Activity

bluss

bluss commented on Jul 5, 2015

@bluss
Member

See discussion on #25162 why it was changed to Borrow

steveklabnik

steveklabnik commented on Jul 6, 2015

@steveklabnik
Member

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

steveklabnik

steveklabnik commented on Jul 6, 2015

@steveklabnik
Member

/cc @rust-lang/libs

Gankra

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")

alexcrichton

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.

SimonSapin

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

        Participants

        @steveklabnik@alexcrichton@SimonSapin@frewsxcv@Gankra

        Issue actions

          std::slice::SliceConcatExt impls should rely on AsRef rather than Borrow · Issue #26780 · rust-lang/rust