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
#[cfg(feature = "derive")]
pub use tl_proto_proc::{id, TlRead, TlWrite};
pub use self::boxed::*;
pub use self::hasher::*;
pub use self::seq::*;
pub use self::traits::*;
macro_rules! ok {
($expr:expr) => {
match $expr {
Ok(val) => val,
Err(err) => return Err(err),
}
};
}
mod boxed;
mod hasher;
mod option;
mod primitive;
mod seq;
mod traits;
mod tuple;
mod util;
pub fn deserialize<'a, T>(packet: &'a [u8]) -> TlResult<T>
where
T: TlRead<'a>,
{
T::read_from(packet, &mut 0)
}
#[inline(always)]
pub fn deserialize_as_boxed<'a, T>(packet: &'a [u8]) -> TlResult<T>
where
T: TlRead<'a, Repr = Bare> + BoxedConstructor,
{
match deserialize(packet) {
Ok(BoxedWrapper(result)) => Ok(result),
Err(e) => Err(e),
}
}
pub fn serialize<T>(data: T) -> Vec<u8>
where
T: TlWrite,
{
let mut result = Vec::with_capacity(data.max_size_hint());
data.write_to(&mut result);
result
}
#[inline(always)]
pub fn serialize_as_boxed<T>(data: T) -> Vec<u8>
where
T: TlWrite<Repr = Bare> + BoxedConstructor,
{
serialize(data.into_boxed())
}
pub fn serialize_into<T>(data: T, buffer: &mut Vec<u8>)
where
T: TlWrite,
{
buffer.clear();
buffer.reserve(data.max_size_hint());
data.write_to(buffer);
}
#[inline(always)]
pub fn serialize_into_as_boxed<T>(data: T, buffer: &mut Vec<u8>)
where
T: TlWrite<Repr = Bare> + BoxedConstructor,
{
serialize_into(data.into_boxed(), buffer);
}
#[cfg(feature = "hash")]
pub fn hash<T>(data: T) -> [u8; 32]
where
T: TlWrite<Repr = Boxed>,
{
use digest::Digest;
let mut hasher = sha2::Sha256::new();
HashWrapper(data).update_hasher(&mut hasher);
hasher.finalize().into()
}
#[cfg(feature = "hash")]
#[inline(always)]
pub fn hash_as_boxed<T>(data: T) -> [u8; 32]
where
T: TlWrite<Repr = Bare> + BoxedConstructor,
{
hash(data.into_boxed())
}