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 super::codec::{codec_read_type_info_static, codec_ref_mode, codec_write_type_info, Codec};
19use super::pointee::{PointeeCodec, PointeeSerializer};
20use crate::context::{ReadContext, WriteContext};
21use crate::error::Error;
22use crate::meta::FieldType;
23use crate::resolver::{RefMode, TypeInfo, TypeResolver};
24use crate::serializer::Serializer;
25use crate::type_id::TypeId;
26use std::marker::PhantomData;
27use std::rc::Rc;
28
29pub struct BoxCodec<T: ?Sized, C, const NULLABLE: bool, const TRACK_REF: bool>(
30    PhantomData<(*const T, C)>,
31);
32
33#[inline(always)]
34fn reserve_box<T: ?Sized, C: PointeeSerializer<T>>(context: &mut ReadContext) -> Result<(), Error> {
35    let bytes = C::POINTEE_SIZE;
36    if bytes != 0 {
37        context.reserve_graph_memory(bytes)?;
38    }
39    Ok(())
40}
41
42impl<T, C, const NULLABLE: bool, const TRACK_REF: bool> Serializer
43    for BoxCodec<T, C, NULLABLE, TRACK_REF>
44where
45    T: ?Sized + 'static,
46    C: PointeeSerializer<T>,
47    Box<T>: From<C::Target>,
48{
49    type Target = Box<T>;
50
51    #[inline(always)]
52    fn reserved_space() -> usize {
53        C::reserved_space()
54    }
55
56    #[inline(always)]
57    fn write_data(value: &Box<T>, context: &mut WriteContext) -> Result<(), Error> {
58        C::write_pointee_data(value, context)
59    }
60
61    #[inline(always)]
62    fn read_data(context: &mut ReadContext) -> Result<Box<T>, Error> {
63        reserve_box::<T, C>(context)?;
64        Ok(Box::from(C::read_data(context)?))
65    }
66
67    #[inline(always)]
68    fn write(
69        value: &Box<T>,
70        context: &mut WriteContext,
71        ref_mode: RefMode,
72        write_type_info: bool,
73    ) -> Result<(), Error> {
74        C::write_pointee(value, context, ref_mode, write_type_info)
75    }
76
77    #[inline(always)]
78    fn write_type_info_value(
79        context: &mut WriteContext,
80        target_type_id: std::any::TypeId,
81    ) -> Result<Rc<TypeInfo>, Error> {
82        C::write_type_info_value(context, target_type_id)
83    }
84
85    #[inline(always)]
86    fn write_with_type_info(
87        value: &Box<T>,
88        context: &mut WriteContext,
89        ref_mode: RefMode,
90        type_info: &Rc<TypeInfo>,
91    ) -> Result<(), Error> {
92        C::write_pointee_with_type_info(value, context, ref_mode, type_info)
93    }
94
95    #[inline(always)]
96    fn read(
97        context: &mut ReadContext,
98        ref_mode: RefMode,
99        read_type_info: bool,
100    ) -> Result<Box<T>, Error> {
101        reserve_box::<T, C>(context)?;
102        Ok(Box::from(C::read(context, ref_mode, read_type_info)?))
103    }
104
105    #[inline(always)]
106    fn read_with_type_info(
107        context: &mut ReadContext,
108        ref_mode: RefMode,
109        type_info: &Rc<TypeInfo>,
110    ) -> Result<Box<T>, Error> {
111        reserve_box::<T, C>(context)?;
112        Ok(Box::from(C::read_with_type_info(
113            context, ref_mode, type_info,
114        )?))
115    }
116
117    #[inline(always)]
118    fn default_value(context: &mut ReadContext) -> Result<Box<T>, Error> {
119        reserve_box::<T, C>(context)?;
120        Ok(Box::from(C::default_value(context)?))
121    }
122
123    #[inline(always)]
124    fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
125        C::write_type_info(context)
126    }
127
128    #[inline(always)]
129    fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
130        C::read_type_info(context)
131    }
132
133    #[inline(always)]
134    fn static_type_id() -> TypeId {
135        C::static_type_id()
136    }
137
138    #[inline(always)]
139    fn metadata_target_type_id() -> std::any::TypeId {
140        C::metadata_target_type_id()
141    }
142
143    const IS_OPTIONAL: bool = C::IS_OPTIONAL;
144
145    const IS_POLYMORPHIC: bool = C::IS_POLYMORPHIC;
146
147    const IS_SHARED_REF: bool = C::IS_SHARED_REF;
148
149    const IS_WRAPPER: bool = true;
150
151    const REQUIRES_SCOPED_ACCESS: bool = C::REQUIRES_SCOPED_ACCESS;
152
153    #[inline(always)]
154    fn is_none(value: &Box<T>) -> bool {
155        C::pointee_is_none(value)
156    }
157
158    #[inline(always)]
159    fn dynamic_type_id(value: &Box<T>) -> Result<Option<std::any::TypeId>, Error> {
160        C::pointee_dynamic_type_id(value)
161    }
162}
163
164impl<T, V, C, const NULLABLE: bool, const TRACK_REF: bool> Codec<Box<T>>
165    for BoxCodec<T, C, NULLABLE, TRACK_REF>
166where
167    T: ?Sized + 'static,
168    V: 'static,
169    C: Serializer<Target = V> + PointeeCodec<T, V>,
170    Box<T>: From<C::Target>,
171{
172    #[inline(always)]
173    fn field_type(type_resolver: &TypeResolver) -> Result<FieldType, Error> {
174        let mut field_type = C::field_type(type_resolver)?;
175        field_type.nullable = NULLABLE;
176        field_type.track_ref = TRACK_REF;
177        Ok(field_type)
178    }
179
180    #[inline(always)]
181    fn field_reserved_space() -> usize {
182        C::field_reserved_space()
183    }
184
185    #[inline(always)]
186    fn write_field(value: &Box<T>, context: &mut WriteContext) -> Result<(), Error> {
187        Self::write_with_mode(
188            value,
189            context,
190            codec_ref_mode::<C::Target, C, NULLABLE, TRACK_REF>(),
191            codec_write_type_info::<C::Target, C>(context),
192            true,
193        )
194    }
195
196    #[inline(always)]
197    fn read_field(context: &mut ReadContext) -> Result<Box<T>, Error> {
198        <Self as Serializer>::read(
199            context,
200            codec_ref_mode::<C::Target, C, NULLABLE, TRACK_REF>(),
201            codec_read_type_info_static::<C::Target, C>(context),
202        )
203    }
204
205    #[inline(always)]
206    fn read_data_with_type(
207        context: &mut ReadContext,
208        remote_data_type: &FieldType,
209    ) -> Result<Box<T>, Error> {
210        reserve_box::<T, C>(context)?;
211        Ok(Box::from(C::read_data_with_type(
212            context,
213            remote_data_type,
214        )?))
215    }
216
217    #[inline(always)]
218    fn read_field_with_type(
219        context: &mut ReadContext,
220        remote_field_type: &FieldType,
221    ) -> Result<Box<T>, Error> {
222        reserve_box::<T, C>(context)?;
223        Ok(Box::from(C::read_field_with_type(
224            context,
225            remote_field_type,
226        )?))
227    }
228
229    #[inline(always)]
230    fn write_with_mode(
231        value: &Box<T>,
232        context: &mut WriteContext,
233        ref_mode: RefMode,
234        write_type_info: bool,
235        has_generics: bool,
236    ) -> Result<(), Error> {
237        C::write_pointee_with_mode(value, context, ref_mode, write_type_info, has_generics)
238    }
239
240    #[inline(always)]
241    fn write_with_type_info(
242        value: &Box<T>,
243        context: &mut WriteContext,
244        ref_mode: RefMode,
245        type_info: &Rc<TypeInfo>,
246        has_generics: bool,
247    ) -> Result<(), Error> {
248        C::write_pointee_field_with_type_info(value, context, ref_mode, type_info, has_generics)
249    }
250
251    #[inline(always)]
252    fn read_type_info_value(
253        context: &mut ReadContext,
254    ) -> Result<super::codec::CodecReadType, Error> {
255        C::read_type_info_value(context)
256    }
257}
258
259impl_unsized_carrier_serializer!(Box, BoxCodec);
260
261impl_single_carrier_serializer!(BoxSerializer, Box, BoxCodec, wrapper = true);