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