1use crate::context::{ReadContext, WriteContext};
19use crate::error::Error;
20use crate::resolver::{RefMode, TypeInfo};
21use crate::serializer::codec::OptionCodec;
22use crate::serializer::Serializer;
23use crate::type_id::TypeId;
24use std::marker::PhantomData;
25use std::rc::Rc;
26
27pub struct OptionSerializer<S>(PhantomData<fn() -> S>);
29
30type RootSerializer<S> = OptionCodec<<S as Serializer>::Target, S, false>;
31
32impl<S: Serializer> Serializer for OptionSerializer<S> {
33 type Target = Option<S::Target>;
34
35 #[inline(always)]
36 fn write_data(value: &Self::Target, context: &mut WriteContext) -> Result<(), Error> {
37 <RootSerializer<S> as Serializer>::write_data(value, context)
38 }
39
40 #[inline(always)]
41 fn read_data(context: &mut ReadContext) -> Result<Self::Target, Error> {
42 <RootSerializer<S> as Serializer>::read_data(context)
43 }
44
45 #[inline(always)]
46 fn default_value(context: &mut ReadContext) -> Result<Self::Target, Error> {
47 <RootSerializer<S> as Serializer>::default_value(context)
48 }
49
50 #[inline(always)]
51 fn write(
52 value: &Self::Target,
53 context: &mut WriteContext,
54 ref_mode: RefMode,
55 write_type_info: bool,
56 ) -> Result<(), Error> {
57 <RootSerializer<S> as Serializer>::write(value, context, ref_mode, write_type_info)
58 }
59
60 #[inline(always)]
61 fn write_type_info_value(
62 context: &mut WriteContext,
63 target_type_id: std::any::TypeId,
64 ) -> Result<Rc<TypeInfo>, Error> {
65 <RootSerializer<S> as Serializer>::write_type_info_value(context, target_type_id)
66 }
67
68 #[inline(always)]
69 fn write_with_type_info(
70 value: &Self::Target,
71 context: &mut WriteContext,
72 ref_mode: RefMode,
73 type_info: &Rc<TypeInfo>,
74 ) -> Result<(), Error> {
75 <RootSerializer<S> as Serializer>::write_with_type_info(value, context, ref_mode, type_info)
76 }
77
78 #[inline(always)]
79 fn read(
80 context: &mut ReadContext,
81 ref_mode: RefMode,
82 read_type_info: bool,
83 ) -> Result<Self::Target, Error> {
84 <RootSerializer<S> as Serializer>::read(context, ref_mode, read_type_info)
85 }
86
87 #[inline(always)]
88 fn read_with_type_info(
89 context: &mut ReadContext,
90 ref_mode: RefMode,
91 type_info: &Rc<TypeInfo>,
92 ) -> Result<Self::Target, Error> {
93 <RootSerializer<S> as Serializer>::read_with_type_info(context, ref_mode, type_info)
94 }
95
96 #[inline(always)]
97 fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
98 <RootSerializer<S> as Serializer>::write_type_info(context)
99 }
100
101 #[inline(always)]
102 fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
103 <RootSerializer<S> as Serializer>::read_type_info(context)
104 }
105
106 #[inline(always)]
107 fn static_type_id() -> TypeId {
108 <RootSerializer<S> as Serializer>::static_type_id()
109 }
110
111 #[inline(always)]
112 fn reserved_space() -> usize {
113 <RootSerializer<S> as Serializer>::reserved_space()
114 }
115
116 const IS_OPTIONAL: bool = true;
117
118 const IS_POLYMORPHIC: bool = <RootSerializer<S> as Serializer>::IS_POLYMORPHIC;
119
120 const IS_SHARED_REF: bool = <RootSerializer<S> as Serializer>::IS_SHARED_REF;
121
122 const IS_WRAPPER: bool = true;
123
124 const REQUIRES_SCOPED_ACCESS: bool = S::REQUIRES_SCOPED_ACCESS;
125
126 const READ_DATA_ALWAYS_ADVANCES: bool = S::READ_DATA_ALWAYS_ADVANCES;
127
128 #[inline(always)]
129 fn is_none(value: &Self::Target) -> bool {
130 value.is_none()
131 }
132
133 #[inline(always)]
134 fn dynamic_type_id(value: &Self::Target) -> Result<Option<std::any::TypeId>, Error> {
135 match value {
136 Some(value) => S::dynamic_type_id(value),
137 None => Ok(None),
138 }
139 }
140}
141
142impl<T> Serializer for Option<T>
143where
144 T: Serializer<Target = T>,
145{
146 type Target = Self;
147
148 #[inline(always)]
149 fn write_data(value: &Self, context: &mut WriteContext) -> Result<(), Error> {
150 OptionSerializer::<T>::write_data(value, context)
151 }
152
153 #[inline(always)]
154 fn read_data(context: &mut ReadContext) -> Result<Self, Error> {
155 OptionSerializer::<T>::read_data(context)
156 }
157
158 #[inline(always)]
159 fn default_value(context: &mut ReadContext) -> Result<Self, Error> {
160 OptionSerializer::<T>::default_value(context)
161 }
162
163 #[inline(always)]
164 fn write(
165 value: &Self,
166 context: &mut WriteContext,
167 ref_mode: RefMode,
168 write_type_info: bool,
169 ) -> Result<(), Error> {
170 OptionSerializer::<T>::write(value, context, ref_mode, write_type_info)
171 }
172
173 #[inline(always)]
174 fn write_type_info_value(
175 context: &mut WriteContext,
176 target_type_id: std::any::TypeId,
177 ) -> Result<Rc<TypeInfo>, Error> {
178 OptionSerializer::<T>::write_type_info_value(context, target_type_id)
179 }
180
181 #[inline(always)]
182 fn write_with_type_info(
183 value: &Self,
184 context: &mut WriteContext,
185 ref_mode: RefMode,
186 type_info: &Rc<TypeInfo>,
187 ) -> Result<(), Error> {
188 OptionSerializer::<T>::write_with_type_info(value, context, ref_mode, type_info)
189 }
190
191 #[inline(always)]
192 fn read(
193 context: &mut ReadContext,
194 ref_mode: RefMode,
195 read_type_info: bool,
196 ) -> Result<Self, Error> {
197 OptionSerializer::<T>::read(context, ref_mode, read_type_info)
198 }
199
200 #[inline(always)]
201 fn read_with_type_info(
202 context: &mut ReadContext,
203 ref_mode: RefMode,
204 type_info: &Rc<TypeInfo>,
205 ) -> Result<Self, Error> {
206 OptionSerializer::<T>::read_with_type_info(context, ref_mode, type_info)
207 }
208
209 #[inline(always)]
210 fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
211 OptionSerializer::<T>::write_type_info(context)
212 }
213
214 #[inline(always)]
215 fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
216 OptionSerializer::<T>::read_type_info(context)
217 }
218
219 #[inline(always)]
220 fn static_type_id() -> TypeId {
221 OptionSerializer::<T>::static_type_id()
222 }
223
224 #[inline(always)]
225 fn reserved_space() -> usize {
226 OptionSerializer::<T>::reserved_space()
227 }
228
229 const IS_OPTIONAL: bool = true;
230
231 const IS_POLYMORPHIC: bool = OptionSerializer::<T>::IS_POLYMORPHIC;
232
233 const IS_SHARED_REF: bool = OptionSerializer::<T>::IS_SHARED_REF;
234
235 const IS_WRAPPER: bool = true;
236
237 const REQUIRES_SCOPED_ACCESS: bool = OptionSerializer::<T>::REQUIRES_SCOPED_ACCESS;
238
239 const READ_DATA_ALWAYS_ADVANCES: bool = T::READ_DATA_ALWAYS_ADVANCES;
240
241 #[inline(always)]
242 fn is_none(value: &Self) -> bool {
243 value.is_none()
244 }
245
246 #[inline(always)]
247 fn dynamic_type_id(value: &Self) -> Result<Option<std::any::TypeId>, Error> {
248 OptionSerializer::<T>::dynamic_type_id(value)
249 }
250}