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
#![cfg_attr(feature = "nightly", feature(array_try_map))]
#![doc = include_str!("../README.md")]
mod bytes;
mod cursor;
pub mod lencoder;
mod record;
mod types;
use core::convert::TryInto;
use core::mem::{size_of, MaybeUninit};
use core::{fmt, ptr};
use ErrorKind::*;
pub use bytes::*;
pub use cursor::*;
pub use derive::*;
pub use lencoder::Lencoder;
pub use record::*;
pub type Result<T> = core::result::Result<T, ErrorKind>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorKind {
InsufficientBytes,
InvalidLength,
InvalidInput,
LenOverflow,
Unsupported,
InvalidType,
InvalidData,
InvalidChar,
InvalidUtf8,
Other,
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self, f)
}
}
impl std::error::Error for ErrorKind {}
pub trait DataType<'de>: Sized {
const SIZE: usize = size_of::<Self>();
const IS_DYNAMIC: bool = true;
#[inline]
fn size_hint(&self) -> usize {
Self::SIZE
}
fn serialize(self, _: &mut Cursor<impl Bytes>);
fn deserialize(_: &mut Cursor<&'de [u8]>) -> Result<Self>;
#[inline]
fn encode(self) -> Vec<u8> {
let mut vec = Vec::with_capacity(self.size_hint());
self.serialize(&mut (&mut vec).into());
vec
}
#[inline]
fn decode(data: &'de [u8]) -> Result<Self> {
Self::deserialize(&mut Cursor::from(data))
}
}