-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add map and try_map methods to BoundedBTreeMap #11869
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would a trait make sense, or should it just be a method on the struct directly?
What do you think @KiChjang ? If we want to implement it for other Bounded*
containers as well, probably a good idea, or?
I don't foresee anything wrong with implementing it on bounded types. Perhaps your hesitation comes from the fact that we might change the size of the elements, and hence the bounded size of the overall map? This wouldn't be an issue in my opinion, as the function returns a brand new |
Just wanted to hear your opinion, thanks 😄 |
Wait, I just realized... the So unfortunately no, I don't think this is actually the right approach to the problem. |
Added iter_mut, let me know what you think |
So |
Correct, I don't see the need for the |
bot merge |
Waiting for commit status. |
One issue with |
* Add map and try_map methods to BoundedBTreeMap * Undo changes to basic_authorship.rs * Remove unwrap and use unchecked_from instead * Add iter_mut() method * Remove map functions and add docs to iter_mut * fmt Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
See #11866 for relevant discussion.
I debated on whether to have the signature of the functions passed to
map
/try_map
passK
through, but I realized that this could cause the number of items in the map to decrease due to collisions (i.e.b.map(|(_, v)| (1, v))
). While this wouldn't violate any of the guarantees of the datatype, it is odd and most likely unexpected behavior; use.retain().map()
if that behavior is desired.In implementing this, I was wondering if
BoundedVec
would benefit from this functionality as well. Would a trait make sense, or should it just be a method on the struct directly? I'm not sure if this operation would make sense on aBoundedBTreeSet
for the same reason I kept the key as a reference inBoundedBTreeMap::{map/try_map}
.