/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use std::collections::BTreeMap; use std::collections::BTreeSet; use std::collections::HashMap; use std::collections::HashSet; use std::hash::Hash; use std::sync::Arc; use bytes::Bytes; use ordered_float::OrderedFloat; use crate::protocol::ProtocolWriter; use crate::ttype::GetTType; // Write trait. Every type that needs to be serialized will implement this trait. pub trait Serialize

where P: ProtocolWriter, { fn write(&self, p: &mut P); } impl Serialize

for &T where P: ProtocolWriter, T: ?Sized + Serialize

, { fn write(&self, p: &mut P) { (**self).write(p); } } impl Serialize

for Box where P: ProtocolWriter, T: Serialize

, { #[inline] fn write(&self, p: &mut P) { self.as_ref().write(p) } } impl Serialize

for Arc where P: ProtocolWriter, T: Serialize

, { fn write(&self, p: &mut P) { (**self).write(p); } } impl

Serialize

for () where P: ProtocolWriter, { #[inline] fn write(&self, _p: &mut P) {} } impl

Serialize

for bool where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_bool(*self) } } impl

Serialize

for i8 where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_byte(*self) } } impl

Serialize

for i16 where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_i16(*self) } } impl

Serialize

for i32 where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_i32(*self) } } impl

Serialize

for i64 where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_i64(*self) } } impl

Serialize

for f64 where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_double(*self) } } impl

Serialize

for f32 where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_float(*self) } } impl

Serialize

for OrderedFloat where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_double(self.0) } } impl

Serialize

for OrderedFloat where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_float(self.0) } } impl

Serialize

for String where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_string(self.as_str()) } } impl

Serialize

for str where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_string(self) } } impl

Serialize

for Bytes where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_binary(self.as_ref()) } } impl

Serialize

for Vec where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_binary(self.as_ref()) } } impl

Serialize

for [u8] where P: ProtocolWriter, { #[inline] fn write(&self, p: &mut P) { p.write_binary(self) } } impl Serialize

for BTreeSet where P: ProtocolWriter, T: GetTType + Ord, T: Serialize

, { fn write(&self, p: &mut P) { p.write_set_begin(T::TTYPE, self.len()); for item in self.iter() { p.write_set_value_begin(); item.write(p); } p.write_set_end(); } } impl Serialize

for HashSet where P: ProtocolWriter, T: GetTType + Hash + Eq, T: Serialize

, S: std::hash::BuildHasher, { fn write(&self, p: &mut P) { p.write_set_begin(T::TTYPE, self.len()); for item in self.iter() { p.write_set_value_begin(); item.write(p); } p.write_set_end(); } } impl Serialize

for BTreeMap where P: ProtocolWriter, K: GetTType + Ord, K: Serialize

, V: GetTType, V: Serialize

, { fn write(&self, p: &mut P) { p.write_map_begin(K::TTYPE, V::TTYPE, self.len()); for (k, v) in self.iter() { p.write_map_key_begin(); k.write(p); p.write_map_value_begin(); v.write(p); } p.write_map_end(); } } impl Serialize

for HashMap where P: ProtocolWriter, K: GetTType + Hash + Eq, K: Serialize

, V: GetTType, V: Serialize

, S: std::hash::BuildHasher, { fn write(&self, p: &mut P) { p.write_map_begin(K::TTYPE, V::TTYPE, self.len()); for (k, v) in self.iter() { p.write_map_key_begin(); k.write(p); p.write_map_value_begin(); v.write(p); } p.write_map_end(); } } impl Serialize

for Vec where P: ProtocolWriter, T: GetTType, T: Serialize

, { /// Vec is Thrift List type fn write(&self, p: &mut P) { p.write_list_begin(T::TTYPE, self.len()); for item in self.iter() { p.write_list_value_begin(); item.write(p); } p.write_list_end(); } } impl Serialize

for [T] where P: ProtocolWriter, T: GetTType, T: Serialize

, { /// \[T\] is Thrift List type fn write(&self, p: &mut P) { p.write_list_begin(T::TTYPE, self.len()); for item in self.iter() { p.write_list_value_begin(); item.write(p); } p.write_list_end(); } }