1#![warn(missing_docs)]
2#![doc = include_str!("../README.md")]
3
4#[cfg(feature = "derive")]
5pub use tl_proto_proc::{id, TlRead, TlWrite};
6
7pub use self::boxed::*;
8pub use self::hasher::*;
9pub use self::seq::*;
10pub use self::traits::*;
11
12macro_rules! ok {
17 ($expr:expr) => {
18 match $expr {
19 Ok(val) => val,
20 Err(err) => return Err(err),
21 }
22 };
23}
24
25mod boxed;
26mod hasher;
27mod option;
28mod primitive;
29mod seq;
30mod traits;
31mod tuple;
32mod util;
33
34pub fn deserialize<'a, T>(mut packet: &'a [u8]) -> TlResult<T>
36where
37 T: TlRead<'a>,
38{
39 T::read_from(&mut packet)
40}
41
42#[inline(always)]
57pub fn deserialize_as_boxed<'a, T>(packet: &'a [u8]) -> TlResult<T>
58where
59 T: TlRead<'a, Repr = Bare> + BoxedConstructor,
60{
61 match deserialize(packet) {
62 Ok(BoxedWrapper(result)) => Ok(result),
63 Err(e) => Err(e),
64 }
65}
66
67pub fn serialize<T>(data: T) -> Vec<u8>
69where
70 T: TlWrite,
71{
72 let mut result = Vec::with_capacity(data.max_size_hint());
73 data.write_to(&mut result);
74 result
75}
76
77#[inline(always)]
81pub fn serialize_as_boxed<T>(data: T) -> Vec<u8>
82where
83 T: TlWrite<Repr = Bare> + BoxedConstructor,
84{
85 serialize(data.into_boxed())
86}
87
88pub fn serialize_into<T>(data: T, buffer: &mut Vec<u8>)
90where
91 T: TlWrite,
92{
93 buffer.clear();
94 buffer.reserve(data.max_size_hint());
95 data.write_to(buffer);
96}
97
98#[inline(always)]
103pub fn serialize_into_as_boxed<T>(data: T, buffer: &mut Vec<u8>)
104where
105 T: TlWrite<Repr = Bare> + BoxedConstructor,
106{
107 serialize_into(data.into_boxed(), buffer);
108}
109
110#[cfg(feature = "hash")]
112pub fn hash<T>(data: T) -> [u8; 32]
113where
114 T: TlWrite<Repr = Boxed>,
115{
116 use digest::Digest;
117
118 let mut hasher = sha2::Sha256::new();
119 HashWrapper(data).update_hasher(&mut hasher);
120 hasher.finalize().into()
121}
122
123#[cfg(feature = "hash")]
128#[inline(always)]
129pub fn hash_as_boxed<T>(data: T) -> [u8; 32]
130where
131 T: TlWrite<Repr = Bare> + BoxedConstructor,
132{
133 hash(data.into_boxed())
134}
135
136#[doc(hidden)]
137pub mod __internal {
138 pub use crate::util::unlikely;
139}