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 metadata_target_type_id() -> std::any::TypeId {
263                if $wrapper {
264                    S::metadata_target_type_id()
265                } else {
266                    std::any::TypeId::of::<Self::Target>()
267                }
268            }
269
270            const READ_DATA_ALWAYS_ADVANCES: bool = <$codec<
271                    S::Target,
272                    S,
273                    false,
274                    false,
275                > as $crate::serializer::Serializer>::READ_DATA_ALWAYS_ADVANCES;
276
277            #[inline(always)]
278            fn dynamic_type_id(
279                value: &Self::Target,
280            ) -> Result<Option<std::any::TypeId>, $crate::Error> {
281                <$codec<
282                    S::Target,
283                    S,
284                    false,
285                    false,
286                > as $crate::serializer::Serializer>::dynamic_type_id(
287                    value,
288                )
289            }
290
291        }
292
293        impl<T> $crate::serializer::Serializer for $target<T>
294        where
295            T: $crate::serializer::Serializer<Target = T>,
296            $(T: $($bounds)+,)?
297        {
298            type Target = Self;
299
300            #[inline(always)]
301            fn write_data(
302                value: &Self,
303                context: &mut $crate::WriteContext,
304            ) -> Result<(), $crate::Error> {
305                <$provider<T> as $crate::serializer::Serializer>::write_data(value, context)
306            }
307
308            #[inline(always)]
309            fn read_data(
310                context: &mut $crate::ReadContext,
311            ) -> Result<Self, $crate::Error> {
312                <$provider<T> as $crate::serializer::Serializer>::read_data(context)
313            }
314
315            #[inline(always)]
316            fn default_value(
317                context: &mut $crate::ReadContext,
318            ) -> Result<Self, $crate::Error> {
319                <$provider<T> as $crate::serializer::Serializer>::default_value(context)
320            }
321
322            #[inline(always)]
323            fn write(
324                value: &Self,
325                context: &mut $crate::WriteContext,
326                ref_mode: $crate::RefMode,
327                write_type_info: bool,
328            ) -> Result<(), $crate::Error> {
329                <$provider<T> as $crate::serializer::Serializer>::write(
330                    value,
331                    context,
332                    ref_mode,
333                    write_type_info,
334                )
335            }
336
337            #[inline(always)]
338            fn write_type_info_value(
339                context: &mut $crate::WriteContext,
340                target_type_id: std::any::TypeId,
341            ) -> Result<std::rc::Rc<$crate::TypeInfo>, $crate::Error> {
342                <$provider<T> as $crate::serializer::Serializer>::write_type_info_value(
343                    context,
344                    target_type_id,
345                )
346            }
347
348            #[inline(always)]
349            fn write_with_type_info(
350                value: &Self,
351                context: &mut $crate::WriteContext,
352                ref_mode: $crate::RefMode,
353                type_info: &std::rc::Rc<$crate::TypeInfo>,
354            ) -> Result<(), $crate::Error> {
355                <$provider<T> as $crate::serializer::Serializer>::write_with_type_info(
356                    value,
357                    context,
358                    ref_mode,
359                    type_info,
360                )
361            }
362
363            #[inline(always)]
364            fn read(
365                context: &mut $crate::ReadContext,
366                ref_mode: $crate::RefMode,
367                read_type_info: bool,
368            ) -> Result<Self, $crate::Error> {
369                <$provider<T> as $crate::serializer::Serializer>::read(
370                    context,
371                    ref_mode,
372                    read_type_info,
373                )
374            }
375
376            #[inline(always)]
377            fn read_with_type_info(
378                context: &mut $crate::ReadContext,
379                ref_mode: $crate::RefMode,
380                type_info: &std::rc::Rc<$crate::TypeInfo>,
381            ) -> Result<Self, $crate::Error> {
382                <$provider<T> as $crate::serializer::Serializer>::read_with_type_info(
383                    context, ref_mode, type_info,
384                )
385            }
386
387            #[inline(always)]
388            fn write_type_info(
389                context: &mut $crate::WriteContext,
390            ) -> Result<(), $crate::Error> {
391                <$provider<T> as $crate::serializer::Serializer>::write_type_info(context)
392            }
393
394            #[inline(always)]
395            fn read_type_info(
396                context: &mut $crate::ReadContext,
397            ) -> Result<(), $crate::Error> {
398                <$provider<T> as $crate::serializer::Serializer>::read_type_info(context)
399            }
400
401            #[inline(always)]
402            fn static_type_id() -> $crate::TypeId {
403                <$provider<T> as $crate::serializer::Serializer>::static_type_id()
404            }
405
406            #[inline(always)]
407            fn reserved_space() -> usize {
408                <$provider<T> as $crate::serializer::Serializer>::reserved_space()
409            }
410
411            const IS_OPTIONAL: bool =
412                <$provider<T> as $crate::serializer::Serializer>::IS_OPTIONAL;
413
414            #[inline(always)]
415            fn is_none(value: &Self) -> bool {
416                <$provider<T> as $crate::serializer::Serializer>::is_none(value)
417            }
418
419            const IS_POLYMORPHIC: bool =
420                <$provider<T> as $crate::serializer::Serializer>::IS_POLYMORPHIC;
421
422            const IS_SHARED_REF: bool =
423                <$provider<T> as $crate::serializer::Serializer>::IS_SHARED_REF;
424
425            const IS_WRAPPER: bool = $wrapper;
426
427            const REQUIRES_SCOPED_ACCESS: bool =
428                <$provider<T> as $crate::serializer::Serializer>::REQUIRES_SCOPED_ACCESS;
429
430            #[inline(always)]
431            fn metadata_target_type_id() -> std::any::TypeId {
432                <$provider<T> as $crate::serializer::Serializer>::metadata_target_type_id()
433            }
434
435            const READ_DATA_ALWAYS_ADVANCES: bool =
436                <$provider<T> as $crate::serializer::Serializer>::READ_DATA_ALWAYS_ADVANCES;
437
438            #[inline(always)]
439            fn dynamic_type_id(
440                value: &Self,
441            ) -> Result<Option<std::any::TypeId>, $crate::Error> {
442                <$provider<T> as $crate::serializer::Serializer>::dynamic_type_id(value)
443            }
444
445        }
446    };
447}
448
449macro_rules! impl_collection_carrier_codec {
450    (
451        $codec:ident,
452        $container:ident,
453        $wire_type:ident,
454        zst_no_backing = $zst_no_backing:literal
455        $(, bounds = [$($bounds:tt)+])?
456    ) => {
457        pub struct $codec<T, C, const NULLABLE: bool, const TRACK_REF: bool>(
458            std::marker::PhantomData<(T, C)>,
459        );
460
461        impl<T, S, const NULLABLE: bool, const TRACK_REF: bool>
462            $crate::serializer::Serializer for $codec<T, S, NULLABLE, TRACK_REF>
463        where
464            T: 'static,
465            S: $crate::serializer::Serializer<Target = T>,
466            $(T: $($bounds)+,)?
467            $container<T>: FromIterator<T>,
468            for<'a> &'a $container<T>: IntoIterator<Item = &'a T>,
469            for<'a> <&'a $container<T> as IntoIterator>::IntoIter:
470                ExactSizeIterator + Clone,
471        {
472            type Target = $container<T>;
473
474            #[inline(always)]
475            fn reserved_space() -> usize {
476                std::mem::size_of::<u32>() + $crate::type_id::SIZE_OF_REF_AND_TYPE
477            }
478
479            #[inline(always)]
480            fn write_data(
481                value: &$container<T>,
482                context: &mut $crate::WriteContext,
483            ) -> Result<(), $crate::Error> {
484                $crate::serializer::collection::write_collection_value_data::<
485                    T,
486                    S,
487                    _,
488                    true,
489                    $zst_no_backing,
490                >(value, context)
491            }
492
493            #[inline(always)]
494            fn read_data(
495                context: &mut $crate::ReadContext,
496            ) -> Result<$container<T>, $crate::Error> {
497                $crate::serializer::collection::read_collection_value_data::<
498                    $container<T>,
499                    T,
500                    S,
501                    true,
502                    $zst_no_backing,
503                >(context)
504            }
505
506            #[inline(always)]
507            fn default_value(
508                _context: &mut $crate::ReadContext,
509            ) -> Result<$container<T>, $crate::Error> {
510                Ok($container::new())
511            }
512
513            #[inline(always)]
514            fn write_type_info(
515                context: &mut $crate::WriteContext,
516            ) -> Result<(), $crate::Error> {
517                $crate::serializer::collection::write_collection_type_info(
518                    context,
519                    $crate::TypeId::$wire_type as u32,
520                )
521            }
522
523            #[inline(always)]
524            fn read_type_info(
525                context: &mut $crate::ReadContext,
526            ) -> Result<(), $crate::Error> {
527                $crate::serializer::collection::read_collection_type_info(
528                    context,
529                    $crate::TypeId::$wire_type as u32,
530                )
531            }
532
533            #[inline(always)]
534            fn static_type_id() -> $crate::TypeId {
535                $crate::TypeId::$wire_type
536            }
537
538            const READ_DATA_ALWAYS_ADVANCES: bool = true;
539        }
540
541        impl<T, C, const NULLABLE: bool, const TRACK_REF: bool>
542            $crate::serializer::codec::Codec<$container<T>>
543            for $codec<T, C, NULLABLE, TRACK_REF>
544        where
545            T: 'static,
546            C: $crate::serializer::codec::Codec<T>,
547            $(T: $($bounds)+,)?
548            $container<T>: FromIterator<T>,
549            for<'a> &'a $container<T>: IntoIterator<Item = &'a T>,
550            for<'a> <&'a $container<T> as IntoIterator>::IntoIter:
551                ExactSizeIterator + Clone,
552        {
553            #[inline(always)]
554            fn field_type(
555                type_resolver: &$crate::resolver::TypeResolver,
556            ) -> Result<$crate::meta::FieldType, $crate::Error> {
557                Ok($crate::meta::FieldType::new_with_ref(
558                    $crate::TypeId::$wire_type as u32,
559                    NULLABLE,
560                    TRACK_REF,
561                    vec![C::field_type(type_resolver)?],
562                ))
563            }
564
565            #[inline(always)]
566            fn write_field(
567                value: &$container<T>,
568                context: &mut $crate::WriteContext,
569            ) -> Result<(), $crate::Error> {
570                if NULLABLE || TRACK_REF {
571                    context
572                        .writer
573                        .write_i8($crate::RefFlag::NotNullValue as i8);
574                }
575                $crate::serializer::collection::write_collection_data::<
576                    T,
577                    C,
578                    _,
579                    true,
580                    $zst_no_backing,
581                >(
582                    value,
583                    context,
584                    true,
585                )
586            }
587
588            #[inline(always)]
589            fn read_field(
590                context: &mut $crate::ReadContext,
591            ) -> Result<$container<T>, $crate::Error> {
592                if NULLABLE || TRACK_REF {
593                    if context.reader.read_i8()? == $crate::RefFlag::Null as i8 {
594                        return Ok($container::new());
595                    }
596                }
597                $crate::serializer::collection::read_collection_data::<
598                    $container<T>,
599                    T,
600                    C,
601                    true,
602                    $zst_no_backing,
603                >(context)
604            }
605
606            #[inline(always)]
607            fn read_compatible(
608                context: &mut $crate::ReadContext,
609                local_field_type: &$crate::meta::FieldType,
610                remote_field_type: &$crate::meta::FieldType,
611            ) -> Result<Option<$container<T>>, $crate::Error> {
612                if $crate::serializer::codec::field_types_compatible(
613                    local_field_type,
614                    remote_field_type,
615                ) || local_field_type.compatible_shape_match(remote_field_type)
616                    || (local_field_type.type_id == remote_field_type.type_id
617                        && $crate::serializer::codec::allows_missing_generics(
618                            local_field_type.type_id,
619                        )
620                        && (local_field_type.generics.is_empty()
621                            || remote_field_type.generics.is_empty()))
622                {
623                    return Self::read_field_with_type(context, remote_field_type).map(Some);
624                }
625                Ok(None)
626            }
627
628            #[inline(always)]
629            fn read_data_with_type(
630                context: &mut $crate::ReadContext,
631                remote_data_type: &$crate::meta::FieldType,
632            ) -> Result<$container<T>, $crate::Error> {
633                $crate::serializer::collection::read_collection_data_with_type::<
634                    $container<T>,
635                    T,
636                    C,
637                    true,
638                    $zst_no_backing,
639                >(context, remote_data_type)
640            }
641
642            #[inline(always)]
643            fn read_field_with_type(
644                context: &mut $crate::ReadContext,
645                remote_field_type: &$crate::meta::FieldType,
646            ) -> Result<$container<T>, $crate::Error> {
647                if $crate::serializer::codec::field_ref_mode(remote_field_type)
648                    != $crate::RefMode::None
649                    && context.reader.read_i8()? == $crate::RefFlag::Null as i8
650                {
651                    return Ok($container::new());
652                }
653                Self::read_data_with_type(context, remote_field_type)
654            }
655
656            #[inline(always)]
657            fn write_with_mode(
658                value: &$container<T>,
659                context: &mut $crate::WriteContext,
660                ref_mode: $crate::RefMode,
661                write_type_info: bool,
662                has_generics: bool,
663            ) -> Result<(), $crate::Error> {
664                if ref_mode != $crate::RefMode::None {
665                    context
666                        .writer
667                        .write_i8($crate::RefFlag::NotNullValue as i8);
668                }
669                if write_type_info {
670                    <Self as $crate::serializer::Serializer>::write_type_info(context)?;
671                }
672                $crate::serializer::collection::write_collection_data::<
673                    T,
674                    C,
675                    _,
676                    true,
677                    $zst_no_backing,
678                >(
679                    value,
680                    context,
681                    has_generics,
682                )
683            }
684
685        }
686    };
687}
688
689pub mod any;
690mod arc;
691mod array;
692mod bool;
693mod box_;
694#[doc(hidden)]
695pub mod codec;
696pub mod collection;
697mod datetime;
698pub mod enum_;
699mod heap;
700mod list;
701pub mod map;
702mod marker;
703mod mutex;
704mod number;
705mod option;
706mod primitive_list;
707mod rc;
708mod refcell;
709mod scalar_conversion;
710mod set;
711pub mod skip;
712mod string;
713pub mod struct_;
714pub mod trait_object;
715mod tuple;
716#[doc(hidden)]
717pub mod unknown_case;
718mod unsigned_number;
719pub mod util;
720pub mod weak;
721
722mod core;
723mod decimal;
724pub use any::{read_box_any, write_box_any};
725pub use arc::ArcSerializer;
726pub use array::ArraySerializer;
727pub use box_::BoxSerializer;
728pub use core::{read_data, write_data, Serializer, StructSerializer};
729pub use heap::BinaryHeapSerializer;
730pub use list::{LinkedListSerializer, VecDequeSerializer, VecSerializer};
731pub use map::{BTreeMapSerializer, HashMapSerializer};
732pub use mutex::MutexSerializer;
733pub use option::OptionSerializer;
734pub use rc::RcSerializer;
735pub use refcell::RefCellSerializer;
736pub use set::{BTreeSetSerializer, HashSetSerializer};
737pub use tuple::{
738    Tuple10Serializer, Tuple11Serializer, Tuple12Serializer, Tuple13Serializer, Tuple14Serializer,
739    Tuple15Serializer, Tuple16Serializer, Tuple17Serializer, Tuple18Serializer, Tuple19Serializer,
740    Tuple1Serializer, Tuple20Serializer, Tuple21Serializer, Tuple22Serializer, Tuple2Serializer,
741    Tuple3Serializer, Tuple4Serializer, Tuple5Serializer, Tuple6Serializer, Tuple7Serializer,
742    Tuple8Serializer, Tuple9Serializer,
743};
744pub use util::send_sync::box_send_sync;
745pub use weak::{ArcWeakSerializer, RcWeakSerializer};