hyperium / http

Rust HTTP types
Apache License 2.0
1.15k stars 285 forks source link

provide entry or entry-like methods for Extensions #655

Closed GlenDC closed 9 months ago

GlenDC commented 10 months ago

Currently it is not possible to use the https://doc.rust-lang.org/stable/std/collections/hash_map/enum.Entry.html#method.or_insert_with for the Extensions, despite it being a hashmap.

Granted it's an edge case and also maybe the entire Entry API is a bit over kill. But at the very least would be nice if one could do get_or_else(..fn) and get_mut_or_else(..fn).

At the moment that would require first using a get() and then an insert() etc... Awkward and can give also awkward lifetime situations.

GlenDC commented 10 months ago

If there's interest for this I'm willing to contribute this, would just need to know what the preferred API would be in that case...

seanmonstar commented 10 months ago

Hm, I'm sympathetic to the usage pain, but I'm apprehensive about making the Extensions type much more like HashMap, such as with an Entry type. Possibly get_or_... methods would be sufficient?

GlenDC commented 10 months ago

Yes. That’s indeed what I had in mind. How would you name these methods exactly? So i can open a PR or would this need more discussion first?

GlenDC commented 10 months ago

There’s no rush btw. I scratched the original use cases for this, as it seems to lead to worse design. So far I haven’t found a legit new use case for it where it would unblock something or change things for the better.

so unless there are others that want this or you yourself think this is useful I’m going to not implement this until also voted/requested by you or others.

sfackler commented 9 months ago

I'd appreciate get_or_... methods as well. I can make a PR if you're interested @seanmonstar

seanmonstar commented 9 months ago

Sure!