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::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        read_ref_info: bool,
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            read_ref_info,
60            read_type_info,
61        )?))
62    }
63
64    #[inline(always)]
65    fn fory_read_with_type_info(
66        context: &mut ReadContext,
67        read_ref_info: bool,
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,
75            read_ref_info,
76            type_info,
77        )?))
78    }
79
80    #[inline(always)]
81    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
82        Ok(RefCell::new(T::fory_read_data(context)?))
83    }
84
85    #[inline(always)]
86    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
87        T::fory_read_type_info(context)
88    }
89
90    #[inline(always)]
91    fn fory_write(
92        &self,
93        context: &mut WriteContext,
94        write_ref_info: bool,
95        write_type_info: bool,
96        has_generics: bool,
97    ) -> Result<(), Error> {
98        // Don't add ref tracking for RefCell itself, just delegate to inner type
99        // The inner type will handle its own ref tracking
100        T::fory_write(
101            &*self.borrow(),
102            context,
103            write_ref_info,
104            write_type_info,
105            has_generics,
106        )
107    }
108
109    #[inline(always)]
110    fn fory_write_data_generic(
111        &self,
112        context: &mut WriteContext,
113        has_generics: bool,
114    ) -> Result<(), Error> {
115        T::fory_write_data_generic(&*self.borrow(), context, has_generics)
116    }
117
118    #[inline(always)]
119    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
120        T::fory_write_data(&*self.borrow(), context)
121    }
122
123    #[inline(always)]
124    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
125        T::fory_write_type_info(context)
126    }
127
128    #[inline(always)]
129    fn fory_reserved_space() -> usize {
130        // RefCell is transparent, delegate to inner type
131        T::fory_reserved_space()
132    }
133
134    #[inline(always)]
135    fn fory_get_type_id(type_resolver: &TypeResolver) -> Result<u32, Error> {
136        T::fory_get_type_id(type_resolver)
137    }
138
139    #[inline(always)]
140    fn fory_type_id_dyn(&self, type_resolver: &TypeResolver) -> Result<u32, Error> {
141        (*self.borrow()).fory_type_id_dyn(type_resolver)
142    }
143
144    #[inline(always)]
145    fn fory_static_type_id() -> TypeId
146    where
147        Self: Sized,
148    {
149        T::fory_static_type_id()
150    }
151
152    #[inline(always)]
153    fn fory_is_wrapper_type() -> bool
154    where
155        Self: Sized,
156    {
157        true
158    }
159
160    #[inline(always)]
161    fn as_any(&self) -> &dyn std::any::Any {
162        self
163    }
164}
165
166impl<T: ForyDefault> ForyDefault for RefCell<T> {
167    fn fory_default() -> Self {
168        RefCell::new(T::fory_default())
169    }
170}