Skip to main content

fory_core/serializer/
number.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::buffer::{Reader, Writer};
19use crate::error::Error;
20use crate::resolver::context::ReadContext;
21use crate::resolver::context::WriteContext;
22use crate::resolver::type_resolver::TypeResolver;
23use crate::serializer::util::read_basic_type_info;
24use crate::serializer::{ForyDefault, Serializer};
25use crate::types::TypeId;
26
27macro_rules! impl_num_serializer {
28    ($ty:ty, $writer:expr, $reader:expr, $field_type:expr) => {
29        impl Serializer for $ty {
30            #[inline(always)]
31            fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
32                $writer(&mut context.writer, *self);
33                Ok(())
34            }
35
36            #[inline(always)]
37            fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
38                $reader(&mut context.reader)
39            }
40
41            #[inline(always)]
42            fn fory_reserved_space() -> usize {
43                std::mem::size_of::<$ty>()
44            }
45
46            #[inline(always)]
47            fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
48                Ok($field_type)
49            }
50
51            #[inline(always)]
52            fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
53                Ok($field_type)
54            }
55
56            #[inline(always)]
57            fn fory_static_type_id() -> TypeId {
58                $field_type
59            }
60
61            #[inline(always)]
62            fn as_any(&self) -> &dyn std::any::Any {
63                self
64            }
65
66            #[inline(always)]
67            fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
68                context.writer.write_var_uint32($field_type as u32);
69                Ok(())
70            }
71
72            #[inline(always)]
73            fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
74                read_basic_type_info::<Self>(context)
75            }
76        }
77        impl ForyDefault for $ty {
78            #[inline(always)]
79            fn fory_default() -> Self {
80                0 as $ty
81            }
82        }
83    };
84}
85
86impl_num_serializer!(i8, Writer::write_i8, Reader::read_i8, TypeId::INT8);
87impl_num_serializer!(i16, Writer::write_i16, Reader::read_i16, TypeId::INT16);
88impl_num_serializer!(
89    i32,
90    Writer::write_varint32,
91    Reader::read_varint32,
92    TypeId::VARINT32
93);
94impl_num_serializer!(
95    i64,
96    Writer::write_varint64,
97    Reader::read_varint64,
98    TypeId::VARINT64
99);
100impl_num_serializer!(f32, Writer::write_f32, Reader::read_f32, TypeId::FLOAT32);
101impl_num_serializer!(f64, Writer::write_f64, Reader::read_f64, TypeId::FLOAT64);
102impl_num_serializer!(i128, Writer::write_i128, Reader::read_i128, TypeId::INT128);
103impl_num_serializer!(
104    isize,
105    Writer::write_isize,
106    Reader::read_isize,
107    TypeId::ISIZE
108);