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