Skip to content

Add Index impls for IdHashMap and IdOrdMap - #310

Open
ChrisJr404 wants to merge 1 commit into
oxidecomputer:mainfrom
ChrisJr404:index-impls-idhashmap-idordmap
Open

Add Index impls for IdHashMap and IdOrdMap#310
ChrisJr404 wants to merge 1 commit into
oxidecomputer:mainfrom
ChrisJr404:index-impls-idhashmap-idordmap

Conversation

@ChrisJr404

Copy link
Copy Markdown

Hello! This picks up #9, adding Index impls for IdHashMap and IdOrdMap so you can look an item up with map[&key], the way std's HashMap and BTreeMap let you. Like those, indexing panics when the key is absent, and get is still there for the non-panicking version.

You mentioned trouble lining up the lifetimes, and I ran into the same wall at first. The issue is that get's query bound ties the key lifetime to the borrow of self, but Index::index doesn't let you name that borrow in the impl. Making the bound higher-ranked over the key lifetime (for<'k> Equivalent<T::Key<'k>>, and the Comparable equivalent for the ord map) gets it to compile and still covers the normal cases, like &str keys queried with str. There's a test for that borrowed-key case on both maps so it doesn't silently regress.

I kept this to the two single-key maps that the issue asks about. BiHashMap/TriHashMap have more than one key, so Index there is a bit more of a design question and I left it out.

Added tests and doc examples for both, plus a CHANGELOG entry.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant