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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/// The state of a connection between two parties.
///
/// ```txt
/// +---> ReadClosed ---+
/// | |
/// +----> Draining ----+
/// | |
/// Open --+-------------------+-> ClosedAbruptly/ClosedGracefully
/// | |
/// +---> WriteClosed --+
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ConnectionState {
/// Is closed for both reads and writes. The connection was abruptly closed.
ClosedAbruptly,
/// Is closed for both reads and writes. The connection has gracefully closed.
ClosedGracefully,
/// Signals the intention to transit to closed, however, some internal state still
/// needs to be managed through a few more receipts and sends.
Draining,
/// Is open for both reads and writes.
Open,
/// Is closed for reading.
///
/// Happens when the desire to end an connection was initialized by the remote peer. You
/// shouldn't set this state based on local actions.
///
/// In sequential code this state will never occur because any remaining state will be
/// immediately flushed. In other words, [`Self::Open`] will jump directly to closed.
ReadClosed,
/// Is closed for writing.
///
/// Happens when the desire to end an connection was initiated locally. You shouldn't set
/// this state based on remote actions.
WriteClosed,
}
impl ConnectionState {
/// Returns `true` if the connection is no longer readable.
#[inline]
pub const fn cannot_read(self) -> bool {
matches!(self, Self::ClosedAbruptly | Self::ClosedGracefully | Self::ReadClosed)
}
/// Returns `true` if the connection is no longer readable or writable.
#[inline]
pub const fn cannot_read_or_write(self) -> bool {
matches!(
self,
Self::ClosedAbruptly | Self::ClosedGracefully | Self::ReadClosed | Self::WriteClosed
)
}
/// Returns `true` if the connection is no longer writable.
#[inline]
pub const fn cannot_write(self) -> bool {
matches!(self, Self::ClosedAbruptly | Self::ClosedGracefully | Self::WriteClosed)
}
/// Shortcut for [`ConnectionState::ClosedAbruptly`] | [`ConnectionState::ClosedGracefully`].
#[inline]
pub const fn is_closed(self) -> bool {
matches!(self, Self::ClosedAbruptly | Self::ClosedGracefully)
}
/// Shortcut for [`ConnectionState::Draining`].
#[inline]
pub const fn is_draining(self) -> bool {
matches!(self, Self::Draining)
}
/// Returns `true` if the state is [`ConnectionState::ClosedAbruptly`],
/// [`ConnectionState::ClosedGracefully`] or [`ConnectionState::Draining`].
#[inline]
pub const fn is_full_close(self) -> bool {
matches!(self, Self::ClosedAbruptly | Self::ClosedGracefully | Self::Draining)
}
/// Shortcut for [`ConnectionState::Open`].
#[inline]
pub const fn is_open(self) -> bool {
matches!(self, Self::Open)
}
/// Shortcut for [`ConnectionState::ReadClosed`].
#[inline]
pub const fn is_read_closed(&self) -> bool {
matches!(self, Self::ReadClosed)
}
/// Shortcut for [`ConnectionState::WriteClosed`].
#[inline]
pub const fn is_write_closed(&self) -> bool {
matches!(self, Self::WriteClosed)
}
}
impl From<u8> for ConnectionState {
#[inline]
fn from(from: u8) -> Self {
match from {
0 => ConnectionState::ClosedAbruptly,
1 => ConnectionState::ClosedGracefully,
2 => ConnectionState::Draining,
3 => ConnectionState::Open,
4 => ConnectionState::ReadClosed,
_ => ConnectionState::WriteClosed,
}
}
}
impl From<ConnectionState> for u8 {
#[inline]
fn from(from: ConnectionState) -> Self {
match from {
ConnectionState::ClosedAbruptly => 0,
ConnectionState::ClosedGracefully => 1,
ConnectionState::Draining => 2,
ConnectionState::Open => 3,
ConnectionState::ReadClosed => 4,
ConnectionState::WriteClosed => 5,
}
}
}