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, WriteContext};
20use crate::error::Error;
21use crate::serializer::util::read_basic_type_info;
22use crate::serializer::Serializer;
23use crate::type_id::TypeId;
24use std::sync::Arc;
25
26macro_rules! impl_unsigned_serializer {
27    (
28        $ty:ty,
29        $writer:expr,
30        $reader:expr,
31        $type_id:expr,
32        $xlang:expr
33    ) => {
34        impl Serializer for $ty {
35            type Target = Self;
36
37            #[inline(always)]
38            fn write_data(value: &Self, context: &mut WriteContext) -> Result<(), Error> {
39                if !$xlang && context.is_xlang() {
40                    return Err(Error::not_allowed(concat!(
41                        stringify!($ty),
42                        " is not supported in cross-language mode"
43                    )));
44                }
45                $writer(&mut context.writer, *value);
46                Ok(())
47            }
48
49            #[inline(always)]
50            fn read_data(context: &mut ReadContext) -> Result<Self, Error> {
51                $reader(&mut context.reader)
52            }
53
54            #[inline(always)]
55            fn default_value(_: &mut ReadContext) -> Result<Self, Error> {
56                Ok(0)
57            }
58
59            #[inline(always)]
60            fn read_arc_any(
61                context: &mut ReadContext,
62            ) -> Result<Arc<dyn std::any::Any + Send + Sync>, Error> {
63                Ok(Arc::new(Self::read_data(context)?))
64            }
65
66            #[inline(always)]
67            fn reserved_space() -> usize {
68                std::mem::size_of::<Self>()
69            }
70
71            #[inline(always)]
72            fn static_type_id() -> TypeId {
73                $type_id
74            }
75
76            #[inline(always)]
77            fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
78                context.writer.write_var_u32($type_id as u32);
79                Ok(())
80            }
81
82            #[inline(always)]
83            fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
84                read_basic_type_info::<Self>(context)
85            }
86        }
87    };
88}
89
90impl_unsigned_serializer!(u8, Writer::write_u8, Reader::read_u8, TypeId::UINT8, true);
91impl_unsigned_serializer!(
92    u16,
93    Writer::write_u16,
94    Reader::read_u16,
95    TypeId::UINT16,
96    true
97);
98impl_unsigned_serializer!(
99    u32,
100    Writer::write_var_u32,
101    Reader::read_var_u32,
102    TypeId::VAR_UINT32,
103    true
104);
105impl_unsigned_serializer!(
106    u64,
107    Writer::write_var_u64,
108    Reader::read_var_u64,
109    TypeId::VAR_UINT64,
110    true
111);
112impl_unsigned_serializer!(
113    u128,
114    Writer::write_u128,
115    Reader::read_u128,
116    TypeId::U128,
117    false
118);
119impl_unsigned_serializer!(
120    usize,
121    Writer::write_usize,
122    Reader::read_usize,
123    TypeId::USIZE,
124    false
125);