1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// Abstraction that tells if a local protocol instance is a client or a server
pub trait Role {
/// See [`RoleTy`].
const TY: RoleTy;
}
/// Local instance is a client, i.e., opens connections.
#[derive(Clone, Copy, Debug)]
pub struct Client;
impl Role for Client {
const TY: RoleTy = RoleTy::Client;
}
/// Local instance is a server, i.e., listens for connections.
#[derive(Clone, Copy, Debug)]
pub struct Server;
impl Role for Server {
const TY: RoleTy = RoleTy::Server;
}
/// Represents the type of role a local protocol instance fulfills.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RoleTy {
/// The local instance is a client that opens connections.
Client,
/// The local instance is a server that listens for connections.
Server,
}
impl RoleTy {
/// Returns `true` if this instance is [`RoleTy::Client`].
#[inline]
#[must_use]
pub const fn is_client(&self) -> bool {
matches!(self, Self::Client)
}
/// Returns `true` if this instance is [`RoleTy::Server`].
#[inline]
#[must_use]
pub const fn is_server(&self) -> bool {
matches!(self, Self::Server)
}
}