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 crate::{
io::{Read, Write},
Decode, Encode, Result,
};
#[derive(Debug, Clone)]
pub struct Config {
pub endianness: Endianness,
}
impl Config {
#[inline]
pub const fn new_default() -> Self {
Self {
endianness: Endianness::new_default(),
}
}
pub async fn encode<E: Encode + ?Sized>(&self, value: &E) -> Vec<u8> {
let mut bytes = Vec::with_capacity(value.size());
let _ = value.encode_to(self, &mut bytes).await.expect(
"Failed to encode value. Log an issue on nimble's GitHub repository with backtrace.",
);
bytes
}
#[inline]
pub async fn encode_to<E: Encode + ?Sized, W: Write + Unpin + Send>(
&self,
value: &E,
writer: W,
) -> Result<usize> {
value.encode_to(self, writer).await
}
#[inline]
pub async fn decode<D: Decode, T: AsRef<[u8]>>(&self, bytes: T) -> Result<D> {
self.decode_from(&mut bytes.as_ref()).await
}
#[inline]
pub async fn decode_from<D: Decode, R: Read + Unpin + Send>(&self, reader: R) -> Result<D> {
D::decode_from(self, reader).await
}
}
impl Default for Config {
#[inline]
fn default() -> Self {
Self::new_default()
}
}
#[derive(Debug, Clone, Copy)]
pub enum Endianness {
LittleEndian,
BigEndian,
}
impl Endianness {
#[inline]
pub const fn new_default() -> Self {
Self::LittleEndian
}
}
impl Default for Endianness {
#[inline]
fn default() -> Self {
Self::new_default()
}
}