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    fn fory_read(
49        context: &mut ReadContext,
50        read_ref_info: bool,
51        read_type_info: bool,
52    ) -> Result<Self, Error>
53    where
54        Self: Sized + ForyDefault,
55    {
56        Ok(RefCell::new(T::fory_read(
57            context,
58            read_ref_info,
59            read_type_info,
60        )?))
61    }
62
63    fn fory_read_with_type_info(
64        context: &mut ReadContext,
65        read_ref_info: bool,
66        type_info: Rc<TypeInfo>,
67    ) -> Result<Self, Error>
68    where
69        Self: Sized + ForyDefault,
70    {
71        Ok(RefCell::new(T::fory_read_with_type_info(
72            context,
73            read_ref_info,
74            type_info,
75        )?))
76    }
77
78    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
79        Ok(RefCell::new(T::fory_read_data(context)?))
80    }
81
82    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
83        T::fory_read_type_info(context)
84    }
85
86    fn fory_write(
87        &self,
88        context: &mut WriteContext,
89        write_ref_info: bool,
90        write_type_info: bool,
91        has_generics: bool,
92    ) -> Result<(), Error> {
93        // Don't add ref tracking for RefCell itself, just delegate to inner type
94        // The inner type will handle its own ref tracking
95        T::fory_write(
96            &*self.borrow(),
97            context,
98            write_ref_info,
99            write_type_info,
100            has_generics,
101        )
102    }
103
104    fn fory_write_data_generic(
105        &self,
106        context: &mut WriteContext,
107        has_generics: bool,
108    ) -> Result<(), Error> {
109        T::fory_write_data_generic(&*self.borrow(), context, has_generics)
110    }
111
112    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
113        T::fory_write_data(&*self.borrow(), context)
114    }
115
116    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
117        T::fory_write_type_info(context)
118    }
119
120    fn fory_reserved_space() -> usize {
121        // RefCell is transparent, delegate to inner type
122        T::fory_reserved_space()
123    }
124
125    fn fory_get_type_id(type_resolver: &TypeResolver) -> Result<u32, Error> {
126        T::fory_get_type_id(type_resolver)
127    }
128
129    fn fory_type_id_dyn(&self, type_resolver: &TypeResolver) -> Result<u32, Error> {
130        (*self.borrow()).fory_type_id_dyn(type_resolver)
131    }
132
133    fn fory_static_type_id() -> TypeId
134    where
135        Self: Sized,
136    {
137        T::fory_static_type_id()
138    }
139
140    fn as_any(&self) -> &dyn std::any::Any {
141        self
142    }
143}
144
145impl<T: ForyDefault> ForyDefault for RefCell<T> {
146    fn fory_default() -> Self {
147        RefCell::new(T::fory_default())
148    }
149}