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
use tinyvec::ArrayVec;
use toad_common::Cursor;
use super::MessageParseError;
use crate::from_bytes::TryConsumeBytes;
use crate::Message;
#[derive(Copy, Clone, Hash, PartialEq, PartialOrd, Debug, Eq, Ord)]
pub struct Id(pub u16);
impl Id {
pub fn from_be_bytes(bs: [u8; 2]) -> Self {
Self(u16::from_be_bytes(bs))
}
}
impl<Bytes: AsRef<[u8]>> TryConsumeBytes<Bytes> for Id {
type Error = MessageParseError;
fn try_consume_bytes(bytes: &mut Cursor<Bytes>) -> Result<Self, Self::Error> {
match bytes.take_exact(2) {
| Some(&[a, b]) => Ok(Id::from_be_bytes([a, b])),
| _ => Err(MessageParseError::eof()),
}
}
}