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
27macro_rules! impl_unsigned_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                if context.is_xlang() {
33                    return Err(Error::not_allowed(
34                        "Unsigned types are not supported in cross-language mode",
35                    ));
36                }
37                $writer(&mut context.writer, *self);
38                Ok(())
39            }
40
41            #[inline(always)]
42            fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
43                $reader(&mut context.reader)
44            }
45
46            #[inline(always)]
47            fn fory_reserved_space() -> usize {
48                std::mem::size_of::<$ty>()
49            }
50
51            #[inline(always)]
52            fn fory_get_type_id(_: &TypeResolver) -> Result<u32, Error> {
53                Ok($field_type as u32)
54            }
55
56            #[inline(always)]
57            fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<u32, Error> {
58                Ok($field_type as u32)
59            }
60
61            #[inline(always)]
62            fn fory_static_type_id() -> TypeId {
63                $field_type
64            }
65
66            #[inline(always)]
67            fn as_any(&self) -> &dyn std::any::Any {
68                self
69            }
70
71            #[inline(always)]
72            fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
73                context.writer.write_varuint32($field_type as u32);
74                Ok(())
75            }
76
77            #[inline(always)]
78            fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
79                read_basic_type_info::<Self>(context)
80            }
81        }
82        impl ForyDefault for $ty {
83            #[inline(always)]
84            fn fory_default() -> Self {
85                0 as $ty
86            }
87        }
88    };
89}
90
91impl_unsigned_num_serializer!(u8, Writer::write_u8, Reader::read_u8, TypeId::U8);
92impl_unsigned_num_serializer!(u16, Writer::write_u16, Reader::read_u16, TypeId::U16);
93impl_unsigned_num_serializer!(u32, Writer::write_u32, Reader::read_u32, TypeId::U32);
94impl_unsigned_num_serializer!(u64, Writer::write_u64, Reader::read_u64, TypeId::U64);
95impl_unsigned_num_serializer!(
96    usize,
97    Writer::write_usize,
98    Reader::read_usize,
99    TypeId::USIZE
100);
101impl_unsigned_num_serializer!(u128, Writer::write_u128, Reader::read_u128, TypeId::U128);