#map #multi #dictionary #hashmap #connection


Like a std::collection::HashMap, but allows you to use either of two different keys to retrieve items

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

#640 in Network programming

Download history 632/week @ 2023-02-07 815/week @ 2023-02-14 545/week @ 2023-02-21 614/week @ 2023-02-28 662/week @ 2023-03-07 837/week @ 2023-03-14 452/week @ 2023-03-21 505/week @ 2023-03-28 471/week @ 2023-04-04 549/week @ 2023-04-11 477/week @ 2023-04-18 624/week @ 2023-04-25 556/week @ 2023-05-02 625/week @ 2023-05-09 465/week @ 2023-05-16 589/week @ 2023-05-23

2,295 downloads per month
Used in 5 crates (2 directly)

MIT license

311 lines


Build Status Crate version Documentation

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.

No runtime deps