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
use speedy::{Context, Readable, Reader, Writable, Writer};
use crate::messages::validity_trait::Validity;
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq)]
pub struct ProtocolId {
protocol_id: [char; 4],
}
impl ProtocolId {
pub const PROTOCOL_RTPS: ProtocolId = ProtocolId {
protocol_id: ['R', 'T', 'P', 'S'],
};
}
impl Default for ProtocolId {
fn default() -> Self {
ProtocolId::PROTOCOL_RTPS
}
}
impl Validity for ProtocolId {
fn valid(&self) -> bool {
*self == ProtocolId::PROTOCOL_RTPS
}
}
impl<'a, C: Context> Readable<'a, C> for ProtocolId {
#[inline]
fn read_from<R: Reader<'a, C>>(reader: &mut R) -> Result<Self, C::Error> {
let mut protocol_id = ProtocolId::default();
for i in 0..protocol_id.protocol_id.len() {
protocol_id.protocol_id[i] = reader.read_u8()? as char;
}
Ok(protocol_id)
}
#[inline]
fn minimum_bytes_needed() -> usize {
4
}
}
impl<C: Context> Writable<C> for ProtocolId {
#[inline]
fn write_to<T: ?Sized + Writer<C>>(&self, writer: &mut T) -> Result<(), C::Error> {
for elem in &self.protocol_id {
writer.write_u8(*elem as u8)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use speedy::Endianness;
use super::*;
#[test]
fn validity() {
let protocol_id = ProtocolId::PROTOCOL_RTPS;
assert!(protocol_id.valid());
let protocol_id = ProtocolId {
protocol_id: ['S', 'P', 'T', 'R'],
};
assert!(!protocol_id.valid());
}
#[test]
fn minimum_bytes_needed() {
assert_eq!(
4,
<ProtocolId as Readable<Endianness>>::minimum_bytes_needed()
);
}
serialization_test!( type = ProtocolId,
{
protocol_rtps,
ProtocolId::PROTOCOL_RTPS,
le = [0x52, 0x54, 0x50, 0x53],
be = [0x52, 0x54, 0x50, 0x53]
});
}