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, WriteContext};
19use crate::error::Error;
20use crate::serializer::util::read_basic_type_info;
21use crate::serializer::Serializer;
22use crate::type_id::TypeId;
23use std::sync::Arc;
24
25impl Serializer for bool {
26    type Target = Self;
27
28    const READ_DATA_ALWAYS_ADVANCES: bool = true;
29
30    #[inline(always)]
31    fn write_data(value: &Self, context: &mut WriteContext) -> Result<(), Error> {
32        context.writer.write_u8(u8::from(*value));
33        Ok(())
34    }
35
36    #[inline(always)]
37    fn read_data(context: &mut ReadContext) -> Result<Self, Error> {
38        Ok(context.reader.read_u8()? == 1)
39    }
40
41    #[inline(always)]
42    fn default_value(_: &mut ReadContext) -> Result<Self, Error> {
43        Ok(false)
44    }
45
46    #[inline(always)]
47    fn read_arc_any(
48        context: &mut ReadContext,
49    ) -> Result<Arc<dyn std::any::Any + Send + Sync>, Error> {
50        Ok(Arc::new(Self::read_data(context)?))
51    }
52
53    #[inline(always)]
54    fn reserved_space() -> usize {
55        std::mem::size_of::<i32>()
56    }
57
58    #[inline(always)]
59    fn static_type_id() -> TypeId {
60        TypeId::BOOL
61    }
62
63    #[inline(always)]
64    fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
65        context.writer.write_u8(TypeId::BOOL as u8);
66        Ok(())
67    }
68
69    #[inline(always)]
70    fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
71        read_basic_type_info::<Self>(context)
72    }
73}