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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
mod collection;
mod string;
use crate::*;
use std::{
convert::{TryFrom, TryInto},
fmt,
iter::FromIterator,
marker::PhantomData,
ops::{Deref, DerefMut},
};
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Record<Len: LenType, T> {
_marker: PhantomData<Len>,
pub data: T,
}
impl<Len: LenType, T> Record<Len, T> {
#[inline]
pub const fn new(data: T) -> Self {
Self {
data,
_marker: PhantomData,
}
}
}
impl<Len: LenType, T> From<T> for Record<Len, T> {
#[inline]
fn from(data: T) -> Self {
Self::new(data)
}
}
impl<Len: LenType, T, V: FromIterator<T>> FromIterator<T> for Record<Len, V> {
#[inline]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Record::new(V::from_iter(iter))
}
}
impl<Len: LenType, T: fmt::Debug> fmt::Debug for Record<Len, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.data.fmt(f)
}
}
impl<Len: LenType, T> Deref for Record<Len, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<Len: LenType, T> DerefMut for Record<Len, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
pub trait LenType:
fmt::Display + TryFrom<usize> + TryInto<usize> + Encoder + for<'de> Decoder<'de>
{
fn max() -> Self;
fn bits() -> u32;
fn ty_str() -> &'static str;
}
macro_rules! impl_len_ty {
[$($ty:ty),*] => {$(
impl LenType for $ty {
#[inline] fn max() -> Self { <$ty>::MAX }
#[inline] fn bits() -> u32 { <$ty>::BITS }
#[inline] fn ty_str() -> &'static str { stringify!($ty) }
}
)*};
}
impl_len_ty!(u8, u16, u32, u64, usize, L2, L3);
macro_rules! encode_len {
[$data:expr, $c: expr] => {
let len = $data.len();
Len::try_from(len).map_err(|_| invalid_input(format!("Max payload length is {} ({}), But got {len}", Len::max(), Len::ty_str())))?.encoder($c)?;
};
}
macro_rules! decode_len {
[$c: expr] => ({
let len: usize = Len::decoder($c)?.try_into().map_err(|_| DynErr::from("Invalid length"))?;
len
});
}
pub(crate) use decode_len;
pub(crate) use encode_len;