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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::marker::PhantomData;
use bytes::BufMut;
use crate::{traits::Signable, wire_types::*, Fixed32, Fixed64, Signed, VarInt};
pub trait Encodable {
type Wire: WireType;
fn encoded_size<V: VarInt>(&self, field_number: V) -> usize;
fn encode(&self, s: &mut ProtobufSerializer<impl BufMut>);
fn encode_field<V: VarInt>(&self, s: &mut ProtobufSerializer<impl BufMut>, field_number: V) {
let var = field_number << 3 | V::from(Self::Wire::BITS);
s.write_varint(var);
self.encode(s);
}
unsafe fn encode_field_precomputed(
&self,
s: &mut ProtobufSerializer<impl BufMut>,
field_number: &[u8],
) {
s.buf.put_slice(field_number);
self.encode(s);
}
}
pub trait EncodableMessage {
fn encoded_size(&self) -> usize;
fn encode<T: BufMut>(&self, s: &mut ProtobufSerializer<T>);
}
pub struct EncodeAsRef<T, U: ?Sized>(T, PhantomData<U>);
impl<T, U: ?Sized> EncodeAsRef<T, U> {
pub fn new(t: T) -> Self {
Self(t, PhantomData)
}
}
impl<T: AsRef<U>, U: ?Sized + Encodable> Encodable for EncodeAsRef<T, U> {
type Wire = U::Wire;
fn encoded_size<V: VarInt>(&self, field_number: V) -> usize {
self.0.as_ref().encoded_size(field_number)
}
fn encode(&self, s: &mut ProtobufSerializer<impl BufMut>) {
self.0.as_ref().encode(s)
}
fn encode_field<V: VarInt>(&self, s: &mut ProtobufSerializer<impl BufMut>, field_number: V) {
self.0.as_ref().encode_field(s, field_number)
}
unsafe fn encode_field_precomputed(&self, s: &mut ProtobufSerializer<impl BufMut>, field_number: &[u8]) {
self.0.as_ref().encode_field_precomputed(s, field_number)
}
}
pub struct ProtobufSerializer<T> {
pub(crate) buf: T,
}
impl<T: bytes::BufMut> ProtobufSerializer<T> {
#[inline]
pub fn new(buf: T) -> Self {
Self { buf }
}
#[inline]
pub fn write_varint(&mut self, value: impl VarInt) {
value.write(&mut self.buf)
}
fn _write_str(&mut self, s: &str) {
self.write_varint(s.len());
self.buf.put_slice(s.as_bytes());
}
#[inline]
pub fn write_str(&mut self, s: impl AsRef<str>) {
self._write_str(s.as_ref())
}
#[inline]
pub fn write_u8(&mut self, n: u8) {
self.buf.put_u8(n);
}
#[inline]
pub fn write_u32(&mut self, n: u32) {
self.buf.put_u32_le(n)
}
#[inline]
pub fn write_u64(&mut self, n: u64) {
self.buf.put_u64_le(n)
}
#[inline]
pub fn write_bytes(&mut self, bytes: &[u8]) {
self.buf.put_slice(bytes)
}
#[inline]
pub fn encode_field<F: Encodable, V: VarInt>(&mut self, field_number: V, field: &F) {
field.encode_field(self, field_number);
}
}
impl<T: BufMut> From<T> for ProtobufSerializer<T> {
fn from(buf: T) -> Self {
Self::new(buf)
}
}
impl Encodable for Fixed32 {
type Wire = Fixed32Wire;
fn encoded_size<V: VarInt>(&self, field_number: V) -> usize {
field_number.size() + 4
}
fn encode(&self, s: &mut ProtobufSerializer<impl BufMut>) {
s.write_u32(self.0)
}
}
impl Encodable for Fixed64 {
type Wire = Fixed64Wire;
fn encoded_size<V: VarInt>(&self, field_number: V) -> usize {
field_number.size() + 8
}
fn encode(&self, s: &mut ProtobufSerializer<impl BufMut>) {
s.write_u64(self.0)
}
}
impl<T: Signable> Encodable for Signed<T> {
type Wire = <T::Storage as Encodable>::Wire;
fn encoded_size<V: VarInt>(&self, field_number: V) -> usize {
self.0.encoded_size(field_number)
}
fn encode(&self, s: &mut ProtobufSerializer<impl BufMut>) {
self.0.encode(s)
}
}
impl Encodable for [u8] {
type Wire = LengthDelimitedWire;
fn encoded_size<V: VarInt>(&self, field_number: V) -> usize {
field_number.size() + self.len().size() + self.len()
}
fn encode(&self, s: &mut ProtobufSerializer<impl BufMut>) {
s.write_varint(self.len());
s.write_bytes(self)
}
}
impl Encodable for Vec<u8> {
type Wire = <[u8] as Encodable>::Wire;
fn encoded_size<V: VarInt>(&self, field_number: V) -> usize {
<[u8] as Encodable>::encoded_size(self, field_number)
}
fn encode(&self, s: &mut ProtobufSerializer<impl BufMut>) {
<[u8] as Encodable>::encode(self, s)
}
}
impl<'a, T: Encodable + ?Sized> Encodable for &'a T {
type Wire = T::Wire;
fn encoded_size<V: VarInt>(&self, field_number: V) -> usize {
T::encoded_size(*self, field_number)
}
fn encode(&self, s: &mut crate::encoding::ProtobufSerializer<impl BufMut>) {
T::encode(*self, s)
}
fn encode_field<V: VarInt>(&self, s: &mut ProtobufSerializer<impl BufMut>, field_number: V) {
(*self).encode_field(s, field_number)
}
unsafe fn encode_field_precomputed(
&self,
s: &mut ProtobufSerializer<impl BufMut>,
field_number: &[u8],
) {
(*self).encode_field_precomputed(s, field_number)
}
}