Skip to main content

fory_core/serializer/
unsigned_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
27// Macro for xlang-compatible unsigned types (u8, u16, u32, u64)
28macro_rules! impl_xlang_unsigned_num_serializer {
29    ($ty:ty, $writer:expr, $reader:expr, $field_type:expr) => {
30        impl Serializer for $ty {
31            #[inline(always)]
32            fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
33                $writer(&mut context.writer, *self);
34                Ok(())
35            }
36
37            #[inline(always)]
38            fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
39                $reader(&mut context.reader)
40            }
41
42            #[inline(always)]
43            fn fory_reserved_space() -> usize {
44                std::mem::size_of::<$ty>()
45            }
46
47            #[inline(always)]
48            fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
49                Ok($field_type)
50            }
51
52            #[inline(always)]
53            fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
54                Ok($field_type)
55            }
56
57            #[inline(always)]
58            fn fory_static_type_id() -> TypeId {
59                $field_type
60            }
61
62            #[inline(always)]
63            fn as_any(&self) -> &dyn std::any::Any {
64                self
65            }
66
67            #[inline(always)]
68            fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
69                context.writer.write_var_uint32($field_type as u32);
70                Ok(())
71            }
72
73            #[inline(always)]
74            fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
75                read_basic_type_info::<Self>(context)
76            }
77        }
78        impl ForyDefault for $ty {
79            #[inline(always)]
80            fn fory_default() -> Self {
81                0 as $ty
82            }
83        }
84    };
85}
86
87// Macro for Rust-specific unsigned types (u128, usize) - not supported in xlang mode
88macro_rules! impl_rust_unsigned_num_serializer {
89    ($ty:ty, $writer:expr, $reader:expr, $field_type:expr) => {
90        impl Serializer for $ty {
91            #[inline(always)]
92            fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
93                if context.is_xlang() {
94                    return Err(Error::not_allowed(concat!(
95                        stringify!($ty),
96                        " is not supported in cross-language mode"
97                    )));
98                }
99                $writer(&mut context.writer, *self);
100                Ok(())
101            }
102
103            #[inline(always)]
104            fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
105                $reader(&mut context.reader)
106            }
107
108            #[inline(always)]
109            fn fory_reserved_space() -> usize {
110                std::mem::size_of::<$ty>()
111            }
112
113            #[inline(always)]
114            fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
115                Ok($field_type)
116            }
117
118            #[inline(always)]
119            fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
120                Ok($field_type)
121            }
122
123            #[inline(always)]
124            fn fory_static_type_id() -> TypeId {
125                $field_type
126            }
127
128            #[inline(always)]
129            fn as_any(&self) -> &dyn std::any::Any {
130                self
131            }
132
133            #[inline(always)]
134            fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
135                context.writer.write_var_uint32($field_type as u32);
136                Ok(())
137            }
138
139            #[inline(always)]
140            fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
141                read_basic_type_info::<Self>(context)
142            }
143        }
144        impl ForyDefault for $ty {
145            #[inline(always)]
146            fn fory_default() -> Self {
147                0 as $ty
148            }
149        }
150    };
151}
152
153// xlang-compatible unsigned types
154impl_xlang_unsigned_num_serializer!(u8, Writer::write_u8, Reader::read_u8, TypeId::UINT8);
155impl_xlang_unsigned_num_serializer!(u16, Writer::write_u16, Reader::read_u16, TypeId::UINT16);
156impl_xlang_unsigned_num_serializer!(
157    u32,
158    Writer::write_var_uint32,
159    Reader::read_varuint32,
160    TypeId::VAR_UINT32
161);
162impl_xlang_unsigned_num_serializer!(
163    u64,
164    Writer::write_var_uint64,
165    Reader::read_varuint64,
166    TypeId::VAR_UINT64
167);
168
169// Rust-specific unsigned types (not supported in xlang mode)
170impl_rust_unsigned_num_serializer!(u128, Writer::write_u128, Reader::read_u128, TypeId::U128);
171impl_rust_unsigned_num_serializer!(
172    usize,
173    Writer::write_usize,
174    Reader::read_usize,
175    TypeId::USIZE
176);