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
use crate::auto::AsByteSequence;
use core::mem;
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ClientMessageData {
data: [u32; LONG_LEN],
}
pub const LONG_LEN: usize = 5;
pub const SHORT_LEN: usize = LONG_LEN * SHORT_INTERVAL;
pub const SHORT_INTERVAL: usize = mem::size_of::<u32>() / mem::size_of::<u16>();
pub const BYTE_LEN: usize = LONG_LEN * BYTE_INTERVAL;
pub const BYTE_INTERVAL: usize = mem::size_of::<u32>() / mem::size_of::<u8>();
impl ClientMessageData {
#[inline]
#[must_use]
pub fn bytes(&self) -> &[u8] {
bytemuck::cast_slice::<u32, u8>(&self.data)
}
#[inline]
#[must_use]
pub fn shorts(&self) -> &[u16] {
bytemuck::cast_slice::<u32, u16>(&self.data)
}
#[inline]
#[must_use]
pub fn longs(&self) -> &[u32] {
&self.data
}
#[inline]
#[must_use]
pub fn bytes_mut(&mut self) -> &mut [u8] {
bytemuck::cast_slice_mut::<u32, u8>(&mut self.data)
}
#[inline]
#[must_use]
pub fn shorts_mut(&mut self) -> &mut [u16] {
bytemuck::cast_slice_mut::<u32, u16>(&mut self.data)
}
#[inline]
#[must_use]
pub fn longs_mut(&mut self) -> &mut [u32] {
&mut self.data
}
}
impl AsByteSequence for ClientMessageData {
#[inline]
fn size(&self) -> usize {
BYTE_LEN
}
#[inline]
fn as_bytes(&self, bytes: &mut [u8]) -> usize {
bytes.copy_from_slice(bytemuck::cast_slice(&self.data));
BYTE_LEN
}
#[inline]
fn from_bytes(bytes: &[u8]) -> Option<(Self, usize)> {
if bytes.len() < BYTE_LEN {
None
} else {
let mut res: [u32; LONG_LEN] = [0; LONG_LEN];
res.copy_from_slice(bytemuck::cast_slice(bytes));
Some((ClientMessageData { data: res }, BYTE_LEN))
}
}
}