tl_proto/
lib.rs

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
12// None of this crate's error handling needs the `From::from` error conversion
13// performed implicitly by the `?` operator or the standard library's `try!`
14// macro. This simplified macro gives a 5.5% improvement in compile time
15// compared to standard `try!`, and 9% improvement compared to `?`.
16macro_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
34/// Tries to deserialize `T` from the TL representation.
35pub 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/// Tries to deserialize `T` as boxed from the TL representation.
43///
44/// `T` must be `Bare` type.
45///
46/// Equivalent to this:
47/// ```
48/// # use tl_proto::{Bare, BoxedConstructor, BoxedWrapper, TlRead, TlResult};
49/// # fn test<'a, T>(packet: &'a [u8]) -> TlResult<T>
50/// # where T: TlRead<'a, Repr = Bare> + BoxedConstructor {
51/// let BoxedWrapper::<T>(data) = tl_proto::deserialize(packet)?;
52/// # Ok(data) }
53/// ```
54///
55/// See [`BoxedWrapper`]
56#[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
67/// Serializes `T` into bytes.
68pub 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/// Wraps `T` into [`BoxedWrapper`] and serializes into bytes.
78///
79/// `T` must be `Bare` type.
80#[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
88/// Serializes `T` into an existing buffer **overwriting its content**.
89pub 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/// Wraps `T` into [`BoxedWrapper`] and serializes it into an existing
99/// buffer **overwriting its content**.
100///
101/// `T` must be `Bare` type.
102#[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/// Computes the `sha256` hash of the TL representation of `T`.
111#[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/// Computes the `sha256` hash of the TL representation of `T`
124/// wrapped into [`BoxedWrapper`].
125///
126/// `T` must be `Bare` type.
127#[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}