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

Equivalence trait instead of Borrow #183

Closed
stepancheg opened this issue Feb 10, 2022 · 11 comments · Fixed by #330
Closed

Equivalence trait instead of Borrow #183

stepancheg opened this issue Feb 10, 2022 · 11 comments · Fixed by #330
Labels
enhancement New feature or request help wanted Extra attention is needed p-low Low priority issue

Comments

@stepancheg
Copy link
Contributor

See how IndexMap is doing it: https://docs.rs/indexmap/latest/indexmap/map/struct.IndexMap.html#method.get

Equivalent trait is implemented automatically for Borrow, but additionally it can be implemented for complex types. Consider this example:

struct Key {
  k0: String,
  k1: String,
}

let map: DashMap<Key, u32> = ...

struct KeyRef<'a> {
  k0: &'a str,
  k1: &'a str,
}

let key_ref: KeyRef = ...

map.get(???)

With indexmap it is possible to implement KeyRef: Equivalent<Key> and query by KeyRef.

It is not possible to implement reasonable Borrow for Key (without allocating).

@xacrimon xacrimon added enhancement New feature or request help wanted Extra attention is needed p-high High priority issue labels Feb 21, 2022
@xacrimon
Copy link
Owner

Looked into this. It seems to be currently blocked by rust-lang/rust#56167 as we use the std hashmap under the hood.

@xacrimon
Copy link
Owner

Now unblocked as we've switched to hashbrown.

@xacrimon xacrimon added p-low Low priority issue and removed p-high High priority issue labels Apr 27, 2022
@tomkarw
Copy link
Contributor

tomkarw commented Aug 9, 2023

@xacrimon This one's pretty straightforward. PR and merge?

@stepancheg
Copy link
Contributor Author

Common Equivalent now lives in a separate crate by the way: https://docs.rs/equivalent/latest/equivalent/

@tomkarw
Copy link
Contributor

tomkarw commented Aug 10, 2023

Common Equivalent now lives in a separate crate by the way: https://docs.rs/equivalent/latest/equivalent/

That's true, but we care about whatever Equivalent trait hashbrown uses. So I think it's correct that the reexport chain is equivalent -> hashbrown -> dashmap.

@stepancheg
Copy link
Contributor Author

we care about whatever Equivalent trait hashbrown uses

Users of indexmap may prefer to implement Equivalent for their types from equivalent trait once for all possible map implementations using equivalent, without dependency on hashbrown, or indexmap, or dashmap.

@tomkarw
Copy link
Contributor

tomkarw commented Aug 10, 2023

Users of indexmap may prefer to implement Equivalent for their types from equivalent trait once for all possible map implementations using equivalent, without dependency on hashbrown, or indexmap, or dashmap.

That's true, however, they are free to do that. Even if we reexport the trait as dashmap::Equivalent for convenience, they can depend on equivalent::Equivalent, implement that and have it work in dashmap as well as other implementations.

Just to be clear, is your preferred solution different from this?

@stepancheg
Copy link
Contributor Author

@tomkarw well, this crate exports hashbrown HashMap in public API, then you are correct, it should be hashbrown's Equivalent. I just learned it now.

@tfreiberg-fastly
Copy link
Contributor

I need this feature and updated #274 in https://github.com/tfreiberg-fastly/dashmap/tree/tfreiberg/rebase-replace-borrow-requirement-on-key-with-hashbrown-equivalent-trait. (For now I also made entry take a borrowed key, which would be a breaking change, so I'm not opening a PR yet)

Is there anything that blocked the previous PR?
Also, I need something like hashbrown's entry_ref API, would that be welcome?

@xacrimon
Copy link
Owner

xacrimon commented Feb 13, 2025

Hi @tfreiberg-fastly! master currently has breaking changes
from v6 and I'm planning to publish a 7.0.0-rc1 later this week. Will probably look at a real v7 after a month or so without reported issues. entry_ref API would be welcome and merged ASAP since I now have renewed time to spend on this crate (last 2-ish years have been too busy). I'll try to get an equivalent API change in this weekend for the release candidate.

@TimoFreiberg
Copy link

alright! i'll try to open a PR tomorrow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed p-low Low priority issue
Projects
None yet
5 participants