In my application there are different IDs, e.g.
remote_tunnel_id . their types are
And I have HashMap to store them by key.
local_tunnels : HashMap<u32 /* local tunnel id */, Arc<Tunnel>>, remote_tunnels : HashMap<u32 /* remote tunnel id */, Arc<Tunnel>>,
The problem is that -- it is easy to make mistake. e.g. passing a local tunnel id to a method where remote tunnel id is expected. This error cannot be detected by complier because their type are the same.
Hence, I am thinking about defining dedicated type per each kind of ID in order to prevent this error in compliation time.
#[derive(PartialEq, Eq, Hash, Clone, Copy)] pub struct LocalTunnelID(pub u32); #[derive(PartialEq, Eq, Hash, Clone, Copy)] pub struct RemoteTunnelID(pub u32);
My question: can
RemoteTunnelID be defined as HashMap's key below?
local_tunnels : HashMap<LocalTunnelID, Arc<Tunnel>>, remote_tunnels : HashMap<RemoteTunnelID, Arc<Tunnel>>,
Hash trait will work in the same way as