distant_net/manager/data/
list.rs

1use std::collections::HashMap;
2use std::ops::{Deref, DerefMut, Index, IndexMut};
3
4use derive_more::IntoIterator;
5use serde::{Deserialize, Serialize};
6
7use crate::common::{ConnectionId, Destination};
8
9/// Represents a list of information about active connections
10#[derive(Clone, Debug, PartialEq, Eq, IntoIterator, Serialize, Deserialize)]
11pub struct ConnectionList(pub(crate) HashMap<ConnectionId, Destination>);
12
13impl ConnectionList {
14    pub fn new() -> Self {
15        Self(HashMap::new())
16    }
17
18    /// Returns a reference to the destination associated with an active connection
19    pub fn connection_destination(&self, id: ConnectionId) -> Option<&Destination> {
20        self.0.get(&id)
21    }
22}
23
24impl Default for ConnectionList {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30impl Deref for ConnectionList {
31    type Target = HashMap<ConnectionId, Destination>;
32
33    fn deref(&self) -> &Self::Target {
34        &self.0
35    }
36}
37
38impl DerefMut for ConnectionList {
39    fn deref_mut(&mut self) -> &mut Self::Target {
40        &mut self.0
41    }
42}
43
44impl Index<ConnectionId> for ConnectionList {
45    type Output = Destination;
46
47    fn index(&self, connection_id: ConnectionId) -> &Self::Output {
48        &self.0[&connection_id]
49    }
50}
51
52impl IndexMut<ConnectionId> for ConnectionList {
53    fn index_mut(&mut self, connection_id: ConnectionId) -> &mut Self::Output {
54        self.0
55            .get_mut(&connection_id)
56            .expect("No connection with id")
57    }
58}