Skip to main content

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::context::ReadContext;
19use crate::context::WriteContext;
20use crate::error::Error;
21use crate::resolver::{TypeInfo, TypeResolver};
22use crate::serializer::{ForyDefault, Serializer};
23use crate::type_id::TypeId;
24use std::rc::Rc;
25
26impl<T: Serializer + ForyDefault> Serializer for Box<T> {
27    #[inline(always)]
28    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error>
29    where
30        Self: Sized + ForyDefault,
31    {
32        // Box owns the heap storage for T; inline value serializers do not
33        // reserve their own self storage.
34        let heap_bytes = std::mem::size_of::<T>();
35        if heap_bytes != 0 {
36            context.reserve_graph_memory(heap_bytes)?;
37        }
38        Ok(Box::new(T::fory_read_data(context)?))
39    }
40
41    #[inline(always)]
42    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
43        T::fory_read_type_info(context)
44    }
45
46    #[inline(always)]
47    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
48        T::fory_write_data(self.as_ref(), context)
49    }
50
51    #[inline(always)]
52    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
53        T::fory_write_type_info(context)
54    }
55
56    #[inline(always)]
57    fn fory_reserved_space() -> usize {
58        T::fory_reserved_space()
59    }
60
61    #[inline(always)]
62    fn fory_get_type_id(type_resolver: &TypeResolver) -> Result<TypeId, Error> {
63        T::fory_get_type_id(type_resolver)
64    }
65
66    #[inline(always)]
67    fn fory_get_type_info(type_resolver: &TypeResolver) -> Result<Rc<TypeInfo>, Error> {
68        T::fory_get_type_info(type_resolver)
69    }
70
71    #[inline(always)]
72    fn fory_type_id_dyn(&self, type_resolver: &TypeResolver) -> Result<TypeId, Error> {
73        (**self).fory_type_id_dyn(type_resolver)
74    }
75
76    #[inline(always)]
77    fn fory_static_type_id() -> TypeId {
78        T::fory_static_type_id()
79    }
80
81    fn fory_is_wrapper_type() -> bool
82    where
83        Self: Sized,
84    {
85        true
86    }
87
88    #[inline(always)]
89    fn as_any(&self) -> &dyn std::any::Any {
90        self
91    }
92}
93
94impl<T: ForyDefault> ForyDefault for Box<T> {
95    fn fory_default() -> Self {
96        Box::new(T::fory_default())
97    }
98}