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
use crate::traits::*;
pub trait BoxedConstructor: Sized {
const TL_ID: u32;
fn boxed_writer(&self) -> BoxedWriter<&Self> {
BoxedWriter(self)
}
fn into_boxed_writer(self) -> BoxedWriter<Self> {
BoxedWriter(self)
}
}
impl<T> BoxedConstructor for &T
where
T: BoxedConstructor,
{
const TL_ID: u32 = T::TL_ID;
}
#[derive(Debug, Clone)]
pub struct BoxedWriter<T>(pub T);
impl<T> TlWrite for BoxedWriter<T>
where
T: BoxedConstructor + TlWrite,
{
const TL_WRITE_BOXED: bool = true;
#[inline(always)]
fn max_size_hint(&self) -> usize {
4 + self.0.max_size_hint()
}
#[inline(always)]
fn write_to<P>(&self, packet: &mut P)
where
P: TlPacket,
{
let _ = TlAssert::<T>::NOT_BOXED_WRITE;
T::TL_ID.write_to(packet);
self.0.write_to(packet);
}
}
#[derive(Debug, Clone)]
pub struct BoxedReader<T>(pub T);
impl<'a, T> TlRead<'a> for BoxedReader<T>
where
T: BoxedConstructor + TlRead<'a>,
{
const TL_READ_BOXED: bool = true;
fn read_from(packet: &'a [u8], offset: &mut usize) -> TlResult<Self> {
let _ = TlAssert::<T>::NOT_BOXED_READ;
if u32::read_from(packet, offset)? == T::TL_ID {
T::read_from(packet, offset).map(BoxedReader)
} else {
Err(TlError::UnknownConstructor)
}
}
}
pub struct TlAssert<T>(std::marker::PhantomData<T>);
impl<'a, T> TlAssert<T>
where
T: TlWrite,
{
pub const BOXED_WRITE: () = if !T::TL_WRITE_BOXED {
panic!("Type must be boxed for write")
};
pub const NOT_BOXED_WRITE: () = if T::TL_WRITE_BOXED {
panic!("Type must be bare for write")
};
}
impl<'a, T> TlAssert<T>
where
T: TlRead<'a>,
{
pub const BOXED_READ: () = if !T::TL_READ_BOXED {
panic!("Type must be boxed for read")
};
pub const NOT_BOXED_READ: () = if T::TL_READ_BOXED {
panic!("Type must be bare for read")
};
}