#hash-map #key #http #collection #sockets #items #different

multi-map

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

#1245 in Network programming

Download history 949/week @ 2023-12-12 617/week @ 2023-12-19 215/week @ 2023-12-26 706/week @ 2024-01-02 1000/week @ 2024-01-09 665/week @ 2024-01-16 732/week @ 2024-01-23 609/week @ 2024-01-30 782/week @ 2024-02-06 1763/week @ 2024-02-13 871/week @ 2024-02-20 756/week @ 2024-02-27 676/week @ 2024-03-05 190/week @ 2024-03-12 508/week @ 2024-03-19 465/week @ 2024-03-26

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

MIT license

21KB
311 lines

Multi-Map

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