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

Feature request: IndexKeys and IndexKeysMut traits #382

Open
Pr0methean opened this issue Mar 15, 2025 · 1 comment
Open

Feature request: IndexKeys and IndexKeysMut traits #382

Pr0methean opened this issue Mar 15, 2025 · 1 comment

Comments

@Pr0methean
Copy link

It would be handy if IndexMap<K,V> and IndexSet<K> had a common trait that would let us iterate the keys of either and obtain their indices, like the keySet() method of Java maps. It might also sometimes be useful to be able to create a mutable set view of an IndexMap<K,V> by specifying a function that would produce values for new keys. Why not call these traits IndexKeys<K> and IndexKeysMut<K> respectively, and implement keys_as_set(&self) and keys_as_set_mut(&mut self, new_value_provider: Fn(&K) -> V) methods to provide the set views of a map? (IndexSet<K> would just implement them itself.)

@cuviper
Copy link
Member

cuviper commented Mar 16, 2025

We have separate iterators, IndexMap::keys and regular IndexSet::iter, and you can enumerate() either of those to get their indices too, but what be the gain in having a common trait? If you have generic code that wants the keys, I would just have that take the input directly as an iterator.

Mutability is touchier, because we don't generally allow that, although there's some opt-in available by importing map::MutableKeys (which has iter_mut2) or set::MutableValues (no iterator yet).

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

No branches or pull requests

2 participants