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
use super::*;
use core::fmt;
use core::mem;
#[repr(packed)]
pub struct PairULE<A, B>(pub A, pub B);
unsafe impl<A: ULE, B: ULE> ULE for PairULE<A, B> {
type Error = PairULEError<A::Error, B::Error>;
fn validate_byte_slice(bytes: &[u8]) -> Result<(), Self::Error> {
let a_len = mem::size_of::<A>();
let b_len = mem::size_of::<B>();
if bytes.len() != a_len + b_len {
return Err(PairULEError::IncorrectLength(a_len + b_len, bytes.len()));
}
A::validate_byte_slice(&bytes[..a_len]).map_err(PairULEError::First)?;
B::validate_byte_slice(&bytes[a_len..]).map_err(PairULEError::Second)?;
Ok(())
}
}
impl<A: AsULE, B: AsULE> AsULE for (A, B) {
type ULE = PairULE<A::ULE, B::ULE>;
#[inline]
fn as_unaligned(self) -> Self::ULE {
PairULE(self.0.as_unaligned(), self.1.as_unaligned())
}
#[inline]
fn from_unaligned(unaligned: Self::ULE) -> Self {
(
A::from_unaligned(unaligned.0),
B::from_unaligned(unaligned.1),
)
}
}
#[derive(Clone, Debug)]
pub enum PairULEError<E, F> {
First(E),
Second(F),
IncorrectLength( usize, usize),
}
impl<E: fmt::Display, F: fmt::Display> fmt::Display for PairULEError<E, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match *self {
PairULEError::First(ref e) => e.fmt(f),
PairULEError::Second(ref e) => e.fmt(f),
PairULEError::IncorrectLength(expected, found) => write!(
f,
"Incorrect length for PairULE: expected {} found {}",
expected, found
),
}
}
}
impl<A: ULE, B: ULE> Clone for PairULE<A, B> {
fn clone(&self) -> Self {
let zero = self.0;
let one = self.1;
PairULE(zero, one)
}
}
impl<A: ULE, B: ULE> Copy for PairULE<A, B> {}