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
use super::{SessionError, SessionErrorKind::ProtocolError};
pub const MAX_SESSION_ID: SessionId = SessionId(16);
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct SessionId(u8);
impl SessionId {
pub fn new(id: u8) -> Result<Self, SessionError> {
if id > MAX_SESSION_ID.0 {
fail!(
ProtocolError,
"session ID exceeds the maximum allowed: {} (max {})",
id,
MAX_SESSION_ID.0
);
}
Ok(SessionId(id))
}
pub fn succ(self) -> Result<Self, SessionError> {
Self::new(self.0 + 1)
}
pub fn to_u8(self) -> u8 {
self.0
}
}