Skip to main content

fory_core/serializer/
refcell.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
18//! Serialization support for `RefCell<T>`.
19//!
20//! This module implements `Serializer` and `ForyDefault` for `std::cell::RefCell<T>`.
21//! It allows mutable reference containers to be part of serialized graphs, which is often
22//! necessary when modeling object structures with interior mutability (e.g. parent/child links).
23//!
24//! Unlike `Rc` and `Arc`, `RefCell` does not do reference counting, so this wrapper relies
25//! on the serialization of the contained `T` only.
26//!
27//! This is commonly used together with `Rc<RefCell<T>>` in graph structures.
28//!
29//! # Example
30//! ```rust
31//! use std::cell::RefCell;
32//! let cell = RefCell::new(42);
33//! // Can be serialized by the Fory framework
34//! ```
35use crate::error::Error;
36use crate::resolver::context::{ReadContext, WriteContext};
37use crate::resolver::type_resolver::{TypeInfo, TypeResolver};
38use crate::serializer::{ForyDefault, Serializer};
39use crate::types::{RefMode, TypeId};
40use std::cell::RefCell;
41use std::rc::Rc;
42
43/// `Serializer` impl for `RefCell<T>`
44///
45/// Simply delegates to the serializer for `T`, allowing interior mutable
46/// containers to be included in serialized graphs.
47impl<T: Serializer + ForyDefault> Serializer for RefCell<T> {
48    #[inline(always)]
49    fn fory_read(
50        context: &mut ReadContext,
51        ref_mode: RefMode,
52        read_type_info: bool,
53    ) -> Result<Self, Error>
54    where
55        Self: Sized + ForyDefault,
56    {
57        Ok(RefCell::new(T::fory_read(
58            context,
59            ref_mode,
60            read_type_info,
61        )?))
62    }
63
64    #[inline(always)]
65    fn fory_read_with_type_info(
66        context: &mut ReadContext,
67        ref_mode: RefMode,
68        type_info: Rc<TypeInfo>,
69    ) -> Result<Self, Error>
70    where
71        Self: Sized + ForyDefault,
72    {
73        Ok(RefCell::new(T::fory_read_with_type_info(
74            context, ref_mode, type_info,
75        )?))
76    }
77
78    #[inline(always)]
79    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
80        Ok(RefCell::new(T::fory_read_data(context)?))
81    }
82
83    #[inline(always)]
84    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
85        T::fory_read_type_info(context)
86    }
87
88    #[inline(always)]
89    fn fory_write(
90        &self,
91        context: &mut WriteContext,
92        ref_mode: RefMode,
93        write_type_info: bool,
94        has_generics: bool,
95    ) -> Result<(), Error> {
96        // Don't add ref tracking for RefCell itself, just delegate to inner type
97        // The inner type will handle its own ref tracking
98        T::fory_write(
99            &*self.borrow(),
100            context,
101            ref_mode,
102            write_type_info,
103            has_generics,
104        )
105    }
106
107    #[inline(always)]
108    fn fory_write_data_generic(
109        &self,
110        context: &mut WriteContext,
111        has_generics: bool,
112    ) -> Result<(), Error> {
113        T::fory_write_data_generic(&*self.borrow(), context, has_generics)
114    }
115
116    #[inline(always)]
117    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
118        T::fory_write_data(&*self.borrow(), context)
119    }
120
121    #[inline(always)]
122    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
123        T::fory_write_type_info(context)
124    }
125
126    #[inline(always)]
127    fn fory_reserved_space() -> usize {
128        // RefCell is transparent, delegate to inner type
129        T::fory_reserved_space()
130    }
131
132    #[inline(always)]
133    fn fory_get_type_id(type_resolver: &TypeResolver) -> Result<TypeId, Error> {
134        T::fory_get_type_id(type_resolver)
135    }
136
137    #[inline(always)]
138    fn fory_get_type_info(type_resolver: &TypeResolver) -> Result<Rc<TypeInfo>, Error> {
139        match type_resolver.get_type_info(&std::any::TypeId::of::<T>()) {
140            Ok(info) => Ok(info),
141            Err(e) => Err(Error::enhance_type_error::<T>(e)),
142        }
143    }
144
145    #[inline(always)]
146    fn fory_type_id_dyn(&self, type_resolver: &TypeResolver) -> Result<TypeId, Error> {
147        (*self.borrow()).fory_type_id_dyn(type_resolver)
148    }
149
150    #[inline(always)]
151    fn fory_static_type_id() -> TypeId
152    where
153        Self: Sized,
154    {
155        T::fory_static_type_id()
156    }
157
158    #[inline(always)]
159    fn fory_is_wrapper_type() -> bool
160    where
161        Self: Sized,
162    {
163        true
164    }
165
166    #[inline(always)]
167    fn as_any(&self) -> &dyn std::any::Any {
168        self
169    }
170}
171
172impl<T: ForyDefault> ForyDefault for RefCell<T> {
173    fn fory_default() -> Self {
174        RefCell::new(T::fory_default())
175    }
176}