Skip to main content

fory_core/serializer/
mod.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
18macro_rules! impl_single_carrier_serializer {
19    (
20        $provider:ident,
21        $target:ident,
22        $codec:ident,
23        wrapper = $wrapper:expr
24        $(, bounds = [$($bounds:tt)+])?
25    ) => {
26        #[doc = concat!(
27            "Statically composes `",
28            stringify!($provider),
29            "<S>` over `S::Target` for serializer-selected roots and nested carrier composition. ",
30            "Generated fields may select this exact carrier or recursively select child serializers; ",
31            "both forms use the same carrier body implementation. This zero-sized carrier is not registered independently."
32        )]
33        pub struct $provider<S>(std::marker::PhantomData<fn() -> S>);
34
35        impl<S> $crate::serializer::Serializer for $provider<S>
36        where
37            S: $crate::serializer::Serializer,
38            $(S::Target: $($bounds)+,)?
39        {
40            type Target = $target<S::Target>;
41
42            #[inline(always)]
43            fn write_data(
44                value: &Self::Target,
45                context: &mut $crate::WriteContext,
46            ) -> Result<(), $crate::Error> {
47                <$codec<
48                    S::Target,
49                    S,
50                    false,
51                    false,
52                > as $crate::serializer::Serializer>::write_data(
53                    value, context,
54                )
55            }
56
57            #[inline(always)]
58            fn read_data(
59                context: &mut $crate::ReadContext,
60            ) -> Result<Self::Target, $crate::Error> {
61                <$codec<
62                    S::Target,
63                    S,
64                    false,
65                    false,
66                > as $crate::serializer::Serializer>::read_data(context)
67            }
68
69            #[inline(always)]
70            fn default_value(
71                context: &mut $crate::ReadContext,
72            ) -> Result<Self::Target, $crate::Error> {
73                <$codec<
74                    S::Target,
75                    S,
76                    false,
77                    false,
78                > as $crate::serializer::Serializer>::default_value(
79                    context,
80                )
81            }
82
83            #[inline(always)]
84            fn write(
85                value: &Self::Target,
86                context: &mut $crate::WriteContext,
87                ref_mode: $crate::RefMode,
88                write_type_info: bool,
89            ) -> Result<(), $crate::Error> {
90                <$codec<
91                    S::Target,
92                    S,
93                    false,
94                    false,
95                > as $crate::serializer::Serializer>::write(
96                    value,
97                    context,
98                    ref_mode,
99                    write_type_info,
100                )
101            }
102
103            #[inline(always)]
104            fn write_type_info_value(
105                context: &mut $crate::WriteContext,
106                target_type_id: std::any::TypeId,
107            ) -> Result<std::rc::Rc<$crate::TypeInfo>, $crate::Error> {
108                <$codec<
109                    S::Target,
110                    S,
111                    false,
112                    false,
113                > as $crate::serializer::Serializer>::write_type_info_value(
114                    context,
115                    target_type_id,
116                )
117            }
118
119            #[inline(always)]
120            fn write_with_type_info(
121                value: &Self::Target,
122                context: &mut $crate::WriteContext,
123                ref_mode: $crate::RefMode,
124                type_info: &std::rc::Rc<$crate::TypeInfo>,
125            ) -> Result<(), $crate::Error> {
126                <$codec<
127                    S::Target,
128                    S,
129                    false,
130                    false,
131                > as $crate::serializer::Serializer>::write_with_type_info(
132                    value,
133                    context,
134                    ref_mode,
135                    type_info,
136                )
137            }
138
139            #[inline(always)]
140            fn read(
141                context: &mut $crate::ReadContext,
142                ref_mode: $crate::RefMode,
143                read_type_info: bool,
144            ) -> Result<Self::Target, $crate::Error> {
145                <$codec<
146                    S::Target,
147                    S,
148                    false,
149                    false,
150                > as $crate::serializer::Serializer>::read(
151                    context,
152                    ref_mode,
153                    read_type_info,
154                )
155            }
156
157            #[inline(always)]
158            fn read_with_type_info(
159                context: &mut $crate::ReadContext,
160                ref_mode: $crate::RefMode,
161                type_info: &std::rc::Rc<$crate::TypeInfo>,
162            ) -> Result<Self::Target, $crate::Error> {
163                <$codec<
164                    S::Target,
165                    S,
166                    false,
167                    false,
168                > as $crate::serializer::Serializer>::read_with_type_info(
169                    context, ref_mode, type_info,
170                )
171            }
172
173            #[inline(always)]
174            fn write_type_info(
175                context: &mut $crate::WriteContext,
176            ) -> Result<(), $crate::Error> {
177                <$codec<
178                    S::Target,
179                    S,
180                    false,
181                    false,
182                > as $crate::serializer::Serializer>::write_type_info(
183                    context,
184                )
185            }
186
187            #[inline(always)]
188            fn read_type_info(
189                context: &mut $crate::ReadContext,
190            ) -> Result<(), $crate::Error> {
191                <$codec<
192                    S::Target,
193                    S,
194                    false,
195                    false,
196                > as $crate::serializer::Serializer>::read_type_info(
197                    context,
198                )
199            }
200
201            #[inline(always)]
202            fn static_type_id() -> $crate::TypeId {
203                <$codec<
204                    S::Target,
205                    S,
206                    false,
207                    false,
208                > as $crate::serializer::Serializer>::static_type_id()
209            }
210
211            #[inline(always)]
212            fn reserved_space() -> usize {
213                <$codec<
214                    S::Target,
215                    S,
216                    false,
217                    false,
218                > as $crate::serializer::Serializer>::reserved_space()
219            }
220
221            const IS_OPTIONAL: bool = <$codec<
222                    S::Target,
223                    S,
224                    false,
225                    false,
226                > as $crate::serializer::Serializer>::IS_OPTIONAL;
227
228            #[inline(always)]
229            fn is_none(value: &Self::Target) -> bool {
230                <$codec<
231                    S::Target,
232                    S,
233                    false,
234                    false,
235                > as $crate::serializer::Serializer>::is_none(value)
236            }
237
238            const IS_POLYMORPHIC: bool = <$codec<
239                    S::Target,
240                    S,
241                    false,
242                    false,
243                > as $crate::serializer::Serializer>::IS_POLYMORPHIC;
244
245            const IS_SHARED_REF: bool = <$codec<
246                    S::Target,
247                    S,
248                    false,
249                    false,
250                > as $crate::serializer::Serializer>::IS_SHARED_REF;
251
252            const IS_WRAPPER: bool = $wrapper;
253
254            const REQUIRES_SCOPED_ACCESS: bool = <$codec<
255                    S::Target,
256                    S,
257                    false,
258                    false,
259                > as $crate::serializer::Serializer>::REQUIRES_SCOPED_ACCESS;
260
261            #[inline(always)]
262            fn dynamic_type_id(
263                value: &Self::Target,
264            ) -> Result<Option<std::any::TypeId>, $crate::Error> {
265                <$codec<
266                    S::Target,
267                    S,
268                    false,
269                    false,
270                > as $crate::serializer::Serializer>::dynamic_type_id(
271                    value,
272                )
273            }
274
275        }
276
277        impl<T> $crate::serializer::Serializer for $target<T>
278        where
279            T: $crate::serializer::Serializer<Target = T>,
280            $(T: $($bounds)+,)?
281        {
282            type Target = Self;
283
284            #[inline(always)]
285            fn write_data(
286                value: &Self,
287                context: &mut $crate::WriteContext,
288            ) -> Result<(), $crate::Error> {
289                <$provider<T> as $crate::serializer::Serializer>::write_data(value, context)
290            }
291
292            #[inline(always)]
293            fn read_data(
294                context: &mut $crate::ReadContext,
295            ) -> Result<Self, $crate::Error> {
296                <$provider<T> as $crate::serializer::Serializer>::read_data(context)
297            }
298
299            #[inline(always)]
300            fn default_value(
301                context: &mut $crate::ReadContext,
302            ) -> Result<Self, $crate::Error> {
303                <$provider<T> as $crate::serializer::Serializer>::default_value(context)
304            }
305
306            #[inline(always)]
307            fn write(
308                value: &Self,
309                context: &mut $crate::WriteContext,
310                ref_mode: $crate::RefMode,
311                write_type_info: bool,
312            ) -> Result<(), $crate::Error> {
313                <$provider<T> as $crate::serializer::Serializer>::write(
314                    value,
315                    context,
316                    ref_mode,
317                    write_type_info,
318                )
319            }
320
321            #[inline(always)]
322            fn write_type_info_value(
323                context: &mut $crate::WriteContext,
324                target_type_id: std::any::TypeId,
325            ) -> Result<std::rc::Rc<$crate::TypeInfo>, $crate::Error> {
326                <$provider<T> as $crate::serializer::Serializer>::write_type_info_value(
327                    context,
328                    target_type_id,
329                )
330            }
331
332            #[inline(always)]
333            fn write_with_type_info(
334                value: &Self,
335                context: &mut $crate::WriteContext,
336                ref_mode: $crate::RefMode,
337                type_info: &std::rc::Rc<$crate::TypeInfo>,
338            ) -> Result<(), $crate::Error> {
339                <$provider<T> as $crate::serializer::Serializer>::write_with_type_info(
340                    value,
341                    context,
342                    ref_mode,
343                    type_info,
344                )
345            }
346
347            #[inline(always)]
348            fn read(
349                context: &mut $crate::ReadContext,
350                ref_mode: $crate::RefMode,
351                read_type_info: bool,
352            ) -> Result<Self, $crate::Error> {
353                <$provider<T> as $crate::serializer::Serializer>::read(
354                    context,
355                    ref_mode,
356                    read_type_info,
357                )
358            }
359
360            #[inline(always)]
361            fn read_with_type_info(
362                context: &mut $crate::ReadContext,
363                ref_mode: $crate::RefMode,
364                type_info: &std::rc::Rc<$crate::TypeInfo>,
365            ) -> Result<Self, $crate::Error> {
366                <$provider<T> as $crate::serializer::Serializer>::read_with_type_info(
367                    context, ref_mode, type_info,
368                )
369            }
370
371            #[inline(always)]
372            fn write_type_info(
373                context: &mut $crate::WriteContext,
374            ) -> Result<(), $crate::Error> {
375                <$provider<T> as $crate::serializer::Serializer>::write_type_info(context)
376            }
377
378            #[inline(always)]
379            fn read_type_info(
380                context: &mut $crate::ReadContext,
381            ) -> Result<(), $crate::Error> {
382                <$provider<T> as $crate::serializer::Serializer>::read_type_info(context)
383            }
384
385            #[inline(always)]
386            fn static_type_id() -> $crate::TypeId {
387                <$provider<T> as $crate::serializer::Serializer>::static_type_id()
388            }
389
390            #[inline(always)]
391            fn reserved_space() -> usize {
392                <$provider<T> as $crate::serializer::Serializer>::reserved_space()
393            }
394
395            const IS_OPTIONAL: bool =
396                <$provider<T> as $crate::serializer::Serializer>::IS_OPTIONAL;
397
398            #[inline(always)]
399            fn is_none(value: &Self) -> bool {
400                <$provider<T> as $crate::serializer::Serializer>::is_none(value)
401            }
402
403            const IS_POLYMORPHIC: bool =
404                <$provider<T> as $crate::serializer::Serializer>::IS_POLYMORPHIC;
405
406            const IS_SHARED_REF: bool =
407                <$provider<T> as $crate::serializer::Serializer>::IS_SHARED_REF;
408
409            const IS_WRAPPER: bool = $wrapper;
410
411            const REQUIRES_SCOPED_ACCESS: bool =
412                <$provider<T> as $crate::serializer::Serializer>::REQUIRES_SCOPED_ACCESS;
413
414            #[inline(always)]
415            fn dynamic_type_id(
416                value: &Self,
417            ) -> Result<Option<std::any::TypeId>, $crate::Error> {
418                <$provider<T> as $crate::serializer::Serializer>::dynamic_type_id(value)
419            }
420
421        }
422    };
423}
424
425macro_rules! impl_collection_carrier_codec {
426    (
427        $codec:ident,
428        $container:ident,
429        $wire_type:ident,
430        zst_no_backing = $zst_no_backing:literal
431        $(, bounds = [$($bounds:tt)+])?
432    ) => {
433        pub struct $codec<T, C, const NULLABLE: bool, const TRACK_REF: bool>(
434            std::marker::PhantomData<(T, C)>,
435        );
436
437        impl<T, S, const NULLABLE: bool, const TRACK_REF: bool>
438            $crate::serializer::Serializer for $codec<T, S, NULLABLE, TRACK_REF>
439        where
440            T: 'static,
441            S: $crate::serializer::Serializer<Target = T>,
442            $(T: $($bounds)+,)?
443            $container<T>: FromIterator<T>,
444            for<'a> &'a $container<T>: IntoIterator<Item = &'a T>,
445            for<'a> <&'a $container<T> as IntoIterator>::IntoIter:
446                ExactSizeIterator + Clone,
447        {
448            type Target = $container<T>;
449
450            #[inline(always)]
451            fn reserved_space() -> usize {
452                std::mem::size_of::<u32>() + $crate::type_id::SIZE_OF_REF_AND_TYPE
453            }
454
455            #[inline(always)]
456            fn write_data(
457                value: &$container<T>,
458                context: &mut $crate::WriteContext,
459            ) -> Result<(), $crate::Error> {
460                $crate::serializer::collection::write_collection_value_data::<
461                    T,
462                    S,
463                    _,
464                    true,
465                    $zst_no_backing,
466                >(value, context)
467            }
468
469            #[inline(always)]
470            fn read_data(
471                context: &mut $crate::ReadContext,
472            ) -> Result<$container<T>, $crate::Error> {
473                $crate::serializer::collection::read_collection_value_data::<
474                    $container<T>,
475                    T,
476                    S,
477                    true,
478                    $zst_no_backing,
479                >(context)
480            }
481
482            #[inline(always)]
483            fn default_value(
484                _context: &mut $crate::ReadContext,
485            ) -> Result<$container<T>, $crate::Error> {
486                Ok($container::new())
487            }
488
489            #[inline(always)]
490            fn write_type_info(
491                context: &mut $crate::WriteContext,
492            ) -> Result<(), $crate::Error> {
493                $crate::serializer::collection::write_collection_type_info(
494                    context,
495                    $crate::TypeId::$wire_type as u32,
496                )
497            }
498
499            #[inline(always)]
500            fn read_type_info(
501                context: &mut $crate::ReadContext,
502            ) -> Result<(), $crate::Error> {
503                $crate::serializer::collection::read_collection_type_info(
504                    context,
505                    $crate::TypeId::$wire_type as u32,
506                )
507            }
508
509            #[inline(always)]
510            fn static_type_id() -> $crate::TypeId {
511                $crate::TypeId::$wire_type
512            }
513        }
514
515        impl<T, C, const NULLABLE: bool, const TRACK_REF: bool>
516            $crate::serializer::codec::Codec<$container<T>>
517            for $codec<T, C, NULLABLE, TRACK_REF>
518        where
519            T: 'static,
520            C: $crate::serializer::codec::Codec<T>,
521            $(T: $($bounds)+,)?
522            $container<T>: FromIterator<T>,
523            for<'a> &'a $container<T>: IntoIterator<Item = &'a T>,
524            for<'a> <&'a $container<T> as IntoIterator>::IntoIter:
525                ExactSizeIterator + Clone,
526        {
527            #[inline(always)]
528            fn field_type(
529                type_resolver: &$crate::resolver::TypeResolver,
530            ) -> Result<$crate::meta::FieldType, $crate::Error> {
531                Ok($crate::meta::FieldType::new_with_ref(
532                    $crate::TypeId::$wire_type as u32,
533                    NULLABLE,
534                    TRACK_REF,
535                    vec![C::field_type(type_resolver)?],
536                ))
537            }
538
539            #[inline(always)]
540            fn write_field(
541                value: &$container<T>,
542                context: &mut $crate::WriteContext,
543            ) -> Result<(), $crate::Error> {
544                if NULLABLE || TRACK_REF {
545                    context
546                        .writer
547                        .write_i8($crate::RefFlag::NotNullValue as i8);
548                }
549                $crate::serializer::collection::write_collection_data::<
550                    T,
551                    C,
552                    _,
553                    true,
554                    $zst_no_backing,
555                >(
556                    value,
557                    context,
558                    true,
559                )
560            }
561
562            #[inline(always)]
563            fn read_field(
564                context: &mut $crate::ReadContext,
565            ) -> Result<$container<T>, $crate::Error> {
566                if NULLABLE || TRACK_REF {
567                    if context.reader.read_i8()? == $crate::RefFlag::Null as i8 {
568                        return Ok($container::new());
569                    }
570                }
571                $crate::serializer::collection::read_collection_data::<
572                    $container<T>,
573                    T,
574                    C,
575                    true,
576                    $zst_no_backing,
577                >(context)
578            }
579
580            #[inline(always)]
581            fn read_compatible(
582                context: &mut $crate::ReadContext,
583                local_field_type: &$crate::meta::FieldType,
584                remote_field_type: &$crate::meta::FieldType,
585            ) -> Result<Option<$container<T>>, $crate::Error> {
586                if $crate::serializer::codec::field_types_compatible(
587                    local_field_type,
588                    remote_field_type,
589                ) || local_field_type.compatible_shape_match(remote_field_type)
590                    || (local_field_type.type_id == remote_field_type.type_id
591                        && $crate::serializer::codec::allows_missing_generics(
592                            local_field_type.type_id,
593                        )
594                        && (local_field_type.generics.is_empty()
595                            || remote_field_type.generics.is_empty()))
596                {
597                    return Self::read_field_with_type(context, remote_field_type).map(Some);
598                }
599                Ok(None)
600            }
601
602            #[inline(always)]
603            fn read_data_with_type(
604                context: &mut $crate::ReadContext,
605                remote_data_type: &$crate::meta::FieldType,
606            ) -> Result<$container<T>, $crate::Error> {
607                $crate::serializer::collection::read_collection_data_with_type::<
608                    $container<T>,
609                    T,
610                    C,
611                    true,
612                    $zst_no_backing,
613                >(context, remote_data_type)
614            }
615
616            #[inline(always)]
617            fn read_field_with_type(
618                context: &mut $crate::ReadContext,
619                remote_field_type: &$crate::meta::FieldType,
620            ) -> Result<$container<T>, $crate::Error> {
621                if $crate::serializer::codec::field_ref_mode(remote_field_type)
622                    != $crate::RefMode::None
623                    && context.reader.read_i8()? == $crate::RefFlag::Null as i8
624                {
625                    return Ok($container::new());
626                }
627                Self::read_data_with_type(context, remote_field_type)
628            }
629
630            #[inline(always)]
631            fn write_with_mode(
632                value: &$container<T>,
633                context: &mut $crate::WriteContext,
634                ref_mode: $crate::RefMode,
635                write_type_info: bool,
636                has_generics: bool,
637            ) -> Result<(), $crate::Error> {
638                if ref_mode != $crate::RefMode::None {
639                    context
640                        .writer
641                        .write_i8($crate::RefFlag::NotNullValue as i8);
642                }
643                if write_type_info {
644                    <Self as $crate::serializer::Serializer>::write_type_info(context)?;
645                }
646                $crate::serializer::collection::write_collection_data::<
647                    T,
648                    C,
649                    _,
650                    true,
651                    $zst_no_backing,
652                >(
653                    value,
654                    context,
655                    has_generics,
656                )
657            }
658
659        }
660    };
661}
662
663pub mod any;
664mod arc;
665mod array;
666mod bool;
667mod box_;
668#[doc(hidden)]
669pub mod codec;
670pub mod collection;
671mod datetime;
672pub mod enum_;
673mod heap;
674mod list;
675pub mod map;
676mod marker;
677mod mutex;
678mod number;
679mod option;
680mod primitive_list;
681mod rc;
682mod refcell;
683mod scalar_conversion;
684mod set;
685pub mod skip;
686mod string;
687pub mod struct_;
688pub mod trait_object;
689mod tuple;
690#[doc(hidden)]
691pub mod unknown_case;
692mod unsigned_number;
693pub mod util;
694pub mod weak;
695
696mod core;
697mod decimal;
698pub use any::{read_box_any, write_box_any};
699pub use arc::ArcSerializer;
700pub use array::ArraySerializer;
701pub use box_::BoxSerializer;
702pub use core::{read_data, write_data, Serializer, StructSerializer};
703pub use heap::BinaryHeapSerializer;
704pub use list::{LinkedListSerializer, VecDequeSerializer, VecSerializer};
705pub use map::{BTreeMapSerializer, HashMapSerializer};
706pub use mutex::MutexSerializer;
707pub use option::OptionSerializer;
708pub use rc::RcSerializer;
709pub use refcell::RefCellSerializer;
710pub use set::{BTreeSetSerializer, HashSetSerializer};
711pub use tuple::{
712    Tuple10Serializer, Tuple11Serializer, Tuple12Serializer, Tuple13Serializer, Tuple14Serializer,
713    Tuple15Serializer, Tuple16Serializer, Tuple17Serializer, Tuple18Serializer, Tuple19Serializer,
714    Tuple1Serializer, Tuple20Serializer, Tuple21Serializer, Tuple22Serializer, Tuple2Serializer,
715    Tuple3Serializer, Tuple4Serializer, Tuple5Serializer, Tuple6Serializer, Tuple7Serializer,
716    Tuple8Serializer, Tuple9Serializer,
717};
718pub use util::send_sync::box_send_sync;
719pub use weak::{ArcWeakSerializer, RcWeakSerializer};