7 stable releases
Uses old Rust 2015
1.3.0 | Apr 11, 2020 |
---|---|
1.2.0 | Jan 24, 2019 |
1.1.0 | Mar 26, 2017 |
1.0.3 | Aug 1, 2016 |
1.0.1 | Jul 31, 2016 |
#489 in Network programming
2,027 downloads per month
Used in 5 crates
(2 directly)
21KB
311 lines
Multi-Map
Like a std::collection::HashMap
, but allows you to use either of two different keys to retrieve items.
Sometimes, when developing software stacks, a layer will have some context object it needs to store. Assume, for example, we have an HTTP module, which sits below some sort of HTTP-using Application, and above a Socket module.
When a new connection is created, a message is received from the Socket module with the ID of the new connection. A new HTTP Connection object is created, and stored against the Socket ID, so that it can be easily retrieved when data arrives on the Socket. But to inform the layer above about the new HTTP Connection (as distinct from any other HTTP Connections that may be on going), we need to give the HTTP Connection itself a unique ID, and we need some mechanism of locating the HTTP Connection by this ID as well.
The trivial solution is to store the HTTP Connections in a list, and then iterate them, looking for a matching Socket ID or a matching HTTP Connection ID. But this is slow.
A HashMap seems like a good idea, but you can only key on either the HTTP ID or the Socket ID, not on both.
This module allows you to create a MultiMap - a map which you can look up either by the primary key (HttpID) or an alternative ID (SocketID). Internally, it uses two maps, one on (K1, (K2, V)) and another on (K2, K1).
Insert, removal and iteration is supported. Gradually, I might implement the rest of the std::collections::HashMap API, but I've found this to be sufficiently useful for now.