Skip to main content

fory_core/serializer/
bool.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::context::ReadContext;
19use crate::context::WriteContext;
20use crate::error::Error;
21use crate::resolver::TypeResolver;
22use crate::serializer::util::read_basic_type_info;
23use crate::serializer::{ForyDefault, Serializer};
24use crate::type_id::TypeId;
25use std::mem;
26
27impl Serializer for bool {
28    #[inline(always)]
29    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
30        context.writer.write_u8(if *self { 1 } else { 0 });
31        Ok(())
32    }
33
34    #[inline(always)]
35    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
36        Ok(context.reader.read_u8()? == 1)
37    }
38    #[inline]
39    fn fory_read_data_as_send_sync_any(
40        context: &mut ReadContext,
41    ) -> Result<Box<dyn std::any::Any + Send + Sync>, Error>
42    where
43        Self: Sized + ForyDefault,
44    {
45        Ok(crate::serializer::box_send_sync(Self::fory_read_data(
46            context,
47        )?))
48    }
49
50    #[inline(always)]
51    fn fory_reserved_space() -> usize {
52        mem::size_of::<i32>()
53    }
54
55    #[inline(always)]
56    fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
57        Ok(TypeId::BOOL)
58    }
59
60    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
61        Ok(TypeId::BOOL)
62    }
63
64    fn fory_static_type_id() -> TypeId {
65        TypeId::BOOL
66    }
67
68    #[inline(always)]
69    fn as_any(&self) -> &dyn std::any::Any {
70        self
71    }
72
73    #[inline(always)]
74    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
75        context.writer.write_u8(TypeId::BOOL as u8);
76        Ok(())
77    }
78
79    #[inline(always)]
80    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
81        read_basic_type_info::<Self>(context)
82    }
83}
84
85impl ForyDefault for bool {
86    #[inline(always)]
87    fn fory_default() -> Self {
88        false
89    }
90}