dashmap
dashmap copied to clipboard
Equivalence trait instead of Borrow
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).
Looked into this. It seems to be currently blocked by https://github.com/rust-lang/rust/issues/56167 as we use the std hashmap under the hood.
Now unblocked as we've switched to hashbrown.
@xacrimon This one's pretty straightforward. PR and merge?
Common Equivalent now lives in a separate crate by the way: https://docs.rs/equivalent/latest/equivalent/
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.
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.
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?
@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.