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::context::ReadContext;
20use crate::context::WriteContext;
21use crate::error::Error;
22use crate::resolver::TypeResolver;
23use crate::serializer::util::read_basic_type_info;
24use crate::serializer::{ForyDefault, Serializer};
25use crate::type_id::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            #[inline]
42            fn fory_read_data_as_send_sync_any(
43                context: &mut ReadContext,
44            ) -> Result<Box<dyn std::any::Any + Send + Sync>, Error>
45            where
46                Self: Sized + ForyDefault,
47            {
48                Ok(crate::serializer::box_send_sync(Self::fory_read_data(
49                    context,
50                )?))
51            }
52
53            #[inline(always)]
54            fn fory_reserved_space() -> usize {
55                std::mem::size_of::<$ty>()
56            }
57
58            #[inline(always)]
59            fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
60                Ok($field_type)
61            }
62
63            #[inline(always)]
64            fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
65                Ok($field_type)
66            }
67
68            #[inline(always)]
69            fn fory_static_type_id() -> TypeId {
70                $field_type
71            }
72
73            #[inline(always)]
74            fn as_any(&self) -> &dyn std::any::Any {
75                self
76            }
77
78            #[inline(always)]
79            fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
80                context.writer.write_var_u32($field_type as u32);
81                Ok(())
82            }
83
84            #[inline(always)]
85            fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
86                read_basic_type_info::<Self>(context)
87            }
88        }
89        impl ForyDefault for $ty {
90            #[inline(always)]
91            fn fory_default() -> Self {
92                0 as $ty
93            }
94        }
95    };
96}
97
98// Macro for Rust-specific unsigned types (u128, usize) - not supported in xlang mode
99macro_rules! impl_rust_unsigned_num_serializer {
100    ($ty:ty, $writer:expr, $reader:expr, $field_type:expr) => {
101        impl Serializer for $ty {
102            #[inline(always)]
103            fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
104                if context.is_xlang() {
105                    return Err(Error::not_allowed(concat!(
106                        stringify!($ty),
107                        " is not supported in cross-language mode"
108                    )));
109                }
110                $writer(&mut context.writer, *self);
111                Ok(())
112            }
113
114            #[inline(always)]
115            fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
116                $reader(&mut context.reader)
117            }
118            #[inline]
119            fn fory_read_data_as_send_sync_any(
120                context: &mut ReadContext,
121            ) -> Result<Box<dyn std::any::Any + Send + Sync>, Error>
122            where
123                Self: Sized + ForyDefault,
124            {
125                Ok(crate::serializer::box_send_sync(Self::fory_read_data(
126                    context,
127                )?))
128            }
129
130            #[inline(always)]
131            fn fory_reserved_space() -> usize {
132                std::mem::size_of::<$ty>()
133            }
134
135            #[inline(always)]
136            fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
137                Ok($field_type)
138            }
139
140            #[inline(always)]
141            fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
142                Ok($field_type)
143            }
144
145            #[inline(always)]
146            fn fory_static_type_id() -> TypeId {
147                $field_type
148            }
149
150            #[inline(always)]
151            fn as_any(&self) -> &dyn std::any::Any {
152                self
153            }
154
155            #[inline(always)]
156            fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
157                context.writer.write_var_u32($field_type as u32);
158                Ok(())
159            }
160
161            #[inline(always)]
162            fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
163                read_basic_type_info::<Self>(context)
164            }
165        }
166        impl ForyDefault for $ty {
167            #[inline(always)]
168            fn fory_default() -> Self {
169                0 as $ty
170            }
171        }
172    };
173}
174
175// xlang-compatible unsigned types
176impl_xlang_unsigned_num_serializer!(u8, Writer::write_u8, Reader::read_u8, TypeId::UINT8);
177impl_xlang_unsigned_num_serializer!(u16, Writer::write_u16, Reader::read_u16, TypeId::UINT16);
178impl_xlang_unsigned_num_serializer!(
179    u32,
180    Writer::write_var_u32,
181    Reader::read_var_u32,
182    TypeId::VAR_UINT32
183);
184impl_xlang_unsigned_num_serializer!(
185    u64,
186    Writer::write_var_u64,
187    Reader::read_var_u64,
188    TypeId::VAR_UINT64
189);
190
191// Rust-specific unsigned types (not supported in xlang mode)
192impl_rust_unsigned_num_serializer!(u128, Writer::write_u128, Reader::read_u128, TypeId::U128);
193impl_rust_unsigned_num_serializer!(
194    usize,
195    Writer::write_usize,
196    Reader::read_usize,
197    TypeId::USIZE
198);