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