1mod collection;
5mod string;
6
7use crate::*;
8use std::{
9 convert::{TryFrom, TryInto},
10 fmt,
11 iter::FromIterator,
12 marker::PhantomData,
13 ops::{Deref, DerefMut},
14};
15
16#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
32pub struct Record<Len: LenType, T> {
33 _marker: PhantomData<Len>,
34 pub data: T,
35}
36
37impl<Len: LenType, T> Record<Len, T> {
38 #[inline]
39 pub const fn new(data: T) -> Self {
40 Self {
41 data,
42 _marker: PhantomData,
43 }
44 }
45}
46
47impl<Len: LenType, T> From<T> for Record<Len, T> {
48 #[inline]
49 fn from(data: T) -> Self {
50 Self::new(data)
51 }
52}
53
54impl<Len: LenType, T, V: FromIterator<T>> FromIterator<T> for Record<Len, V> {
55 #[inline]
56 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
57 Record::new(V::from_iter(iter))
58 }
59}
60
61impl<Len: LenType, T: fmt::Debug> fmt::Debug for Record<Len, T> {
62 #[inline]
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 self.data.fmt(f)
65 }
66}
67impl<Len: LenType, T> Deref for Record<Len, T> {
68 type Target = T;
69 #[inline]
70 fn deref(&self) -> &Self::Target {
71 &self.data
72 }
73}
74impl<Len: LenType, T> DerefMut for Record<Len, T> {
75 #[inline]
76 fn deref_mut(&mut self) -> &mut Self::Target {
77 &mut self.data
78 }
79}
80
81pub trait LenType:
83 fmt::Display + TryFrom<usize> + TryInto<usize> + Encoder + for<'de> Decoder<'de>
84{
85 fn max() -> Self;
86 fn bits() -> u32;
87 fn ty_str() -> &'static str;
88}
89
90macro_rules! impl_len_ty {
91 [$($ty:ty),*] => {$(
92 impl LenType for $ty {
93 #[inline] fn max() -> Self { <$ty>::MAX }
94 #[inline] fn bits() -> u32 { <$ty>::BITS }
95 #[inline] fn ty_str() -> &'static str { stringify!($ty) }
96 }
97 )*};
98}
99impl_len_ty!(u8, u16, u32, u64, usize, L2, L3);
100macro_rules! encode_len {
101 [$data:expr, $c: expr] => {
102 let len = $data.len();
103 Len::try_from(len).map_err(|_| invalid_input(format!("Max payload length is {} ({}), But got {len}", Len::max(), Len::ty_str())))?.encoder($c)?;
104 };
105}
106macro_rules! decode_len {
107 [$c: expr] => ({
108 let len: usize = Len::decoder($c)?.try_into().map_err(|_| DynErr::from("Invalid length"))?;
109 len
110 });
111}
112pub(crate) use decode_len;
113pub(crate) use encode_len;