Given the following struct:
pub struct WebsocketConnection {
session_id: usize,
tx: UnboundedSender<Message>,
user: Option<User>,
// More fields
}
impl Hash for WebsocketConnection {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.session_id.hash(state);
}
}
Since the one field being used for creating the hash is the session_id
it would be nice if I can retrieve the WebsocketConnection
from a HashSet only by the session_id
. The current workaround if you only have the session_id
is to create a temporary WebsocketConnection
and then pass that to remove
/get
/contains
which seems kinda annoying. Or instead of using a HashSet one could use HashMap<session_id, WebsocketConnection>
C++ has a similar feature: std::unordered_set<Key,Hash,KeyEqual,Allocator>::find - cppreference.com
I am wondering if HashSet should be extended with function(s) that allow finding/removing values from a HashSet by its hash or maybe instead allow to find/remove by anything that implements the trait Hash. That way one could write a custom wrapper type for session_id
and only pass that to remove/find an element.
Best regards, Dario