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

Add HashMapEntry and BTreeMapEntry aliases to std::collections #340

Closed
GrigorenkoPV opened this issue Feb 21, 2024 · 2 comments
Closed

Add HashMapEntry and BTreeMapEntry aliases to std::collections #340

GrigorenkoPV opened this issue Feb 21, 2024 · 2 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@GrigorenkoPV
Copy link

GrigorenkoPV commented Feb 21, 2024

Proposal: in std::collections add {Hash,BTree}MapEntry as an alias for {hash,btree}_map::Entry

Problem statement

Has this happened to you?

use std::collections::hash_map::Entry as HashMapEntry;

Motivating examples or use cases

It surely has happened to:

Not to mention that most of the contents in std::colections::{hash,btree}_map are "internal" things like iterator implementations, which you rarely ever have to refer to directly. Unlike with Entry, which is a surprisingly versatility API that lends itself especially well with Rust's pattern matching. But then you have to name the type, and generally you have the following options:

  1. Fully qualified name (long, not pretty).
  2. use std::collections::hash_map; and then refer to it as hash_map::Entry. But importing an entire module just to refer to a single type inside doesn't sit right with me.
  3. use std::collections::hash_map::Entry;. Suboptimal, the name is too vague and might collide.
  4. use std::collections::hash_map::Entry as HashMapEntry;

Solution sketch

Add these two lines (plus the neccessary attributes) to alloc/collections/mod.rs & srd/collections/mod.rs:

pub use btree_map::Entry as BTreeMapEntry;
pub use hash_map::Entry as HashMapEntry;

Sadly, this is a breaking change because of the glob imports. But I guess this can be done across an edition boundary?

Alternatives

  1. Do not do anything
  2. type HashMapEntry<'a, K, V, A = Global> = hash_map::Entry<'a, K, V, A>; (sounds even worse).
@GrigorenkoPV GrigorenkoPV added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Feb 21, 2024
@Amanieu
Copy link
Member

Amanieu commented Mar 5, 2024

We discussed this in the libs-api meeting. The overall feeling was against adding these aliases. As you've mentioned, it's already possible to do this yourself with the following code:

use std::collections::hash_map::Entry as HashMapEntry

With that said, there was interest in adding a type alias on the HashMap type itself, which would allow the Entry type to be named with HashMap::Entry. However this is blocked on inherent associated types (rust-lang/rust#8995) which are still unstable and in an incomplete state due to compiler limitations.

@Amanieu Amanieu closed this as completed Mar 5, 2024
@GrigorenkoPV
Copy link
Author

I agree, an associated alias sounds better.

And since this is a rather unimportant change, it can easily wait.

Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

2 participants