fory_core/serializer/
box_.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::error::Error;
19use crate::resolver::context::ReadContext;
20use crate::resolver::context::WriteContext;
21use crate::resolver::type_resolver::TypeResolver;
22use crate::serializer::{ForyDefault, Serializer};
23use crate::types::TypeId;
24
25impl<T: Serializer + ForyDefault> Serializer for Box<T> {
26    #[inline(always)]
27    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error>
28    where
29        Self: Sized + ForyDefault,
30    {
31        Ok(Box::new(T::fory_read_data(context)?))
32    }
33
34    #[inline(always)]
35    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
36        T::fory_read_type_info(context)
37    }
38
39    #[inline(always)]
40    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
41        T::fory_write_data(self.as_ref(), context)
42    }
43
44    #[inline(always)]
45    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
46        T::fory_write_type_info(context)
47    }
48
49    #[inline(always)]
50    fn fory_reserved_space() -> usize {
51        T::fory_reserved_space()
52    }
53
54    #[inline(always)]
55    fn fory_get_type_id(type_resolver: &TypeResolver) -> Result<u32, Error> {
56        T::fory_get_type_id(type_resolver)
57    }
58
59    #[inline(always)]
60    fn fory_type_id_dyn(&self, type_resolver: &TypeResolver) -> Result<u32, Error> {
61        (**self).fory_type_id_dyn(type_resolver)
62    }
63
64    #[inline(always)]
65    fn fory_static_type_id() -> TypeId {
66        T::fory_static_type_id()
67    }
68
69    fn fory_is_wrapper_type() -> bool
70    where
71        Self: Sized,
72    {
73        true
74    }
75
76    #[inline(always)]
77    fn as_any(&self) -> &dyn std::any::Any {
78        self
79    }
80}
81
82impl<T: ForyDefault> ForyDefault for Box<T> {
83    fn fory_default() -> Self {
84        Box::new(T::fory_default())
85    }
86}