fbthrift_git/
serialize.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::collections::BTreeMap;
18use std::collections::BTreeSet;
19use std::collections::HashMap;
20use std::collections::HashSet;
21use std::hash::Hash;
22use std::sync::Arc;
23
24use bytes::Bytes;
25use ordered_float::OrderedFloat;
26
27use crate::protocol::ProtocolWriter;
28use crate::ttype::GetTType;
29
30// Write trait. Every type that needs to be serialized will implement this trait.
31pub trait Serialize<P>
32where
33    P: ProtocolWriter,
34{
35    fn write(&self, p: &mut P);
36}
37
38impl<P, T> Serialize<P> for &T
39where
40    P: ProtocolWriter,
41    T: ?Sized + Serialize<P>,
42{
43    fn write(&self, p: &mut P) {
44        (**self).write(p);
45    }
46}
47
48impl<P, T> Serialize<P> for Box<T>
49where
50    P: ProtocolWriter,
51    T: Serialize<P>,
52{
53    #[inline]
54    fn write(&self, p: &mut P) {
55        self.as_ref().write(p)
56    }
57}
58
59impl<P, T> Serialize<P> for Arc<T>
60where
61    P: ProtocolWriter,
62    T: Serialize<P>,
63{
64    fn write(&self, p: &mut P) {
65        (**self).write(p);
66    }
67}
68
69impl<P> Serialize<P> for ()
70where
71    P: ProtocolWriter,
72{
73    #[inline]
74    fn write(&self, _p: &mut P) {}
75}
76
77impl<P> Serialize<P> for bool
78where
79    P: ProtocolWriter,
80{
81    #[inline]
82    fn write(&self, p: &mut P) {
83        p.write_bool(*self)
84    }
85}
86
87impl<P> Serialize<P> for i8
88where
89    P: ProtocolWriter,
90{
91    #[inline]
92    fn write(&self, p: &mut P) {
93        p.write_byte(*self)
94    }
95}
96
97impl<P> Serialize<P> for i16
98where
99    P: ProtocolWriter,
100{
101    #[inline]
102    fn write(&self, p: &mut P) {
103        p.write_i16(*self)
104    }
105}
106
107impl<P> Serialize<P> for i32
108where
109    P: ProtocolWriter,
110{
111    #[inline]
112    fn write(&self, p: &mut P) {
113        p.write_i32(*self)
114    }
115}
116
117impl<P> Serialize<P> for i64
118where
119    P: ProtocolWriter,
120{
121    #[inline]
122    fn write(&self, p: &mut P) {
123        p.write_i64(*self)
124    }
125}
126
127impl<P> Serialize<P> for f64
128where
129    P: ProtocolWriter,
130{
131    #[inline]
132    fn write(&self, p: &mut P) {
133        p.write_double(*self)
134    }
135}
136
137impl<P> Serialize<P> for f32
138where
139    P: ProtocolWriter,
140{
141    #[inline]
142    fn write(&self, p: &mut P) {
143        p.write_float(*self)
144    }
145}
146
147impl<P> Serialize<P> for OrderedFloat<f64>
148where
149    P: ProtocolWriter,
150{
151    #[inline]
152    fn write(&self, p: &mut P) {
153        p.write_double(self.0)
154    }
155}
156
157impl<P> Serialize<P> for OrderedFloat<f32>
158where
159    P: ProtocolWriter,
160{
161    #[inline]
162    fn write(&self, p: &mut P) {
163        p.write_float(self.0)
164    }
165}
166
167impl<P> Serialize<P> for String
168where
169    P: ProtocolWriter,
170{
171    #[inline]
172    fn write(&self, p: &mut P) {
173        p.write_string(self.as_str())
174    }
175}
176
177impl<P> Serialize<P> for str
178where
179    P: ProtocolWriter,
180{
181    #[inline]
182    fn write(&self, p: &mut P) {
183        p.write_string(self)
184    }
185}
186
187impl<P> Serialize<P> for Bytes
188where
189    P: ProtocolWriter,
190{
191    #[inline]
192    fn write(&self, p: &mut P) {
193        p.write_binary(self.as_ref())
194    }
195}
196
197impl<P> Serialize<P> for Vec<u8>
198where
199    P: ProtocolWriter,
200{
201    #[inline]
202    fn write(&self, p: &mut P) {
203        p.write_binary(self.as_ref())
204    }
205}
206
207impl<P> Serialize<P> for [u8]
208where
209    P: ProtocolWriter,
210{
211    #[inline]
212    fn write(&self, p: &mut P) {
213        p.write_binary(self)
214    }
215}
216
217impl<P, T> Serialize<P> for BTreeSet<T>
218where
219    P: ProtocolWriter,
220    T: GetTType + Ord,
221    T: Serialize<P>,
222{
223    fn write(&self, p: &mut P) {
224        p.write_set_begin(T::TTYPE, self.len());
225        for item in self.iter() {
226            p.write_set_value_begin();
227            item.write(p);
228        }
229        p.write_set_end();
230    }
231}
232
233impl<P, T, S> Serialize<P> for HashSet<T, S>
234where
235    P: ProtocolWriter,
236    T: GetTType + Hash + Eq,
237    T: Serialize<P>,
238    S: std::hash::BuildHasher,
239{
240    fn write(&self, p: &mut P) {
241        p.write_set_begin(T::TTYPE, self.len());
242        for item in self.iter() {
243            p.write_set_value_begin();
244            item.write(p);
245        }
246        p.write_set_end();
247    }
248}
249
250impl<P, K, V> Serialize<P> for BTreeMap<K, V>
251where
252    P: ProtocolWriter,
253    K: GetTType + Ord,
254    K: Serialize<P>,
255    V: GetTType,
256    V: Serialize<P>,
257{
258    fn write(&self, p: &mut P) {
259        p.write_map_begin(K::TTYPE, V::TTYPE, self.len());
260        for (k, v) in self.iter() {
261            p.write_map_key_begin();
262            k.write(p);
263            p.write_map_value_begin();
264            v.write(p);
265        }
266        p.write_map_end();
267    }
268}
269
270impl<P, K, V, S> Serialize<P> for HashMap<K, V, S>
271where
272    P: ProtocolWriter,
273    K: GetTType + Hash + Eq,
274    K: Serialize<P>,
275    V: GetTType,
276    V: Serialize<P>,
277    S: std::hash::BuildHasher,
278{
279    fn write(&self, p: &mut P) {
280        p.write_map_begin(K::TTYPE, V::TTYPE, self.len());
281        for (k, v) in self.iter() {
282            p.write_map_key_begin();
283            k.write(p);
284            p.write_map_value_begin();
285            v.write(p);
286        }
287        p.write_map_end();
288    }
289}
290
291impl<P, T> Serialize<P> for Vec<T>
292where
293    P: ProtocolWriter,
294    T: GetTType,
295    T: Serialize<P>,
296{
297    /// Vec<T> is Thrift List type
298    fn write(&self, p: &mut P) {
299        p.write_list_begin(T::TTYPE, self.len());
300        for item in self.iter() {
301            p.write_list_value_begin();
302            item.write(p);
303        }
304        p.write_list_end();
305    }
306}
307
308impl<P, T> Serialize<P> for [T]
309where
310    P: ProtocolWriter,
311    T: GetTType,
312    T: Serialize<P>,
313{
314    /// \[T\] is Thrift List type
315    fn write(&self, p: &mut P) {
316        p.write_list_begin(T::TTYPE, self.len());
317        for item in self.iter() {
318            p.write_list_value_begin();
319            item.write(p);
320        }
321        p.write_list_end();
322    }
323}