1use super::codec::{
19 allows_missing_generics, field_ref_mode, field_types_compatible, generic_field_type, Codec,
20 CodecReadType,
21};
22use super::collection::{
23 write_collection_data, write_collection_value_data, DECL_ELEMENT_TYPE, HAS_NULL, IS_SAME_TYPE,
24 TRACKING_REF,
25};
26use super::primitive_list;
27use crate::context::{ReadContext, WriteContext};
28use crate::error::Error;
29use crate::meta::FieldType;
30use crate::resolver::{RefFlag, RefMode, TypeInfo, TypeResolver};
31use crate::serializer::{core::read_value_type_info, Serializer};
32use crate::type_id::{TypeId, SIZE_OF_REF_AND_TYPE};
33use std::marker::PhantomData;
34use std::mem::MaybeUninit;
35use std::rc::Rc;
36
37pub struct ArrayCodec<T, C, const N: usize, const NULLABLE: bool, const TRACK_REF: bool>(
38 PhantomData<(T, C)>,
39);
40
41struct ArrayInitGuard<T, const N: usize> {
42 values: [MaybeUninit<T>; N],
43 initialized: usize,
44}
45
46impl<T, const N: usize> ArrayInitGuard<T, N> {
47 #[inline(always)]
48 fn new() -> Self {
49 Self {
50 values: unsafe { MaybeUninit::uninit().assume_init() },
51 initialized: 0,
52 }
53 }
54
55 #[inline(always)]
56 fn push(&mut self, value: T) {
57 self.values[self.initialized].write(value);
58 self.initialized += 1;
59 }
60
61 #[inline(always)]
62 unsafe fn finish(self) -> [T; N] {
63 debug_assert_eq!(self.initialized, N);
64 let result = std::ptr::read(self.values.as_ptr().cast::<[T; N]>());
65 std::mem::forget(self);
66 result
67 }
68}
69
70impl<T, const N: usize> Drop for ArrayInitGuard<T, N> {
71 fn drop(&mut self) {
72 for value in &mut self.values[..self.initialized] {
73 unsafe {
74 value.assume_init_drop();
75 }
76 }
77 }
78}
79
80pub(super) fn try_init_array<T, E, const N: usize>(
81 mut read: impl FnMut() -> Result<T, E>,
82) -> Result<[T; N], E> {
83 let mut values = ArrayInitGuard::<T, N>::new();
84 for _ in 0..N {
85 values.push(read()?);
86 }
87 Ok(unsafe { values.finish() })
88}
89
90#[inline(always)]
91fn selected_array_type_id<T: 'static, S: Serializer<Target = T>>() -> Option<TypeId> {
92 primitive_list::array_type_id::<T, S>(false)
93}
94
95#[cold]
96#[inline(never)]
97fn array_len_mismatch(len: usize, expected: usize) -> Error {
98 Error::invalid_data(format!(
99 "Array length mismatch: expected {expected}, got {len}"
100 ))
101}
102
103#[cold]
104#[inline(never)]
105fn non_polymorphic_array() -> Error {
106 Error::type_error("Type inconsistent, target array element is not polymorphic")
107}
108
109#[cold]
110#[inline(never)]
111fn array_type_mismatch(expected: u32, actual: u32) -> Error {
112 Error::type_mismatch(expected, actual)
113}
114
115#[inline(always)]
116fn check_array_len(len: u32, expected: usize) -> Result<(), Error> {
117 let len = len as usize;
118 if len != expected {
119 return Err(array_len_mismatch(len, expected));
120 }
121 Ok(())
124}
125
126#[inline(always)]
127fn read_declared_items<T, C, const N: usize>(
128 context: &mut ReadContext,
129 element_type: &FieldType,
130 has_null: bool,
131) -> Result<[T; N], Error>
132where
133 T: 'static,
134 C: Codec<T>,
135{
136 try_init_array(|| {
137 if has_null && context.reader.read_i8()? == RefFlag::Null as i8 {
138 C::default_value(context)
139 } else {
140 C::read_data_with_type(context, element_type)
141 }
142 })
143}
144
145#[inline(always)]
146fn read_typed_items<T, C, const N: usize>(
147 context: &mut ReadContext,
148 read_type: &CodecReadType,
149 has_null: bool,
150) -> Result<[T; N], Error>
151where
152 T: 'static,
153 C: Codec<T>,
154{
155 try_init_array(|| {
156 if has_null && context.reader.read_i8()? == RefFlag::Null as i8 {
157 return C::default_value(context);
158 }
159 match read_type {
160 CodecReadType::Field(field_type) => C::read_data_with_type(context, field_type),
161 CodecReadType::TypeInfo(type_info) => C::read_data_with_type_info(context, type_info),
162 }
163 })
164}
165
166macro_rules! array_read_declared_dyn {
167 (value, $T:ty, $S:ty, $N:ident, $context:expr, $remote:expr, $ref_mode:expr, $has_null:expr, $track_ref:expr) => {
168 try_init_array(|| <$S as Serializer>::read($context, $ref_mode, false))
169 };
170 (field, $T:ty, $C:ty, $N:ident, $context:expr, $remote:expr, $ref_mode:expr, $has_null:expr, $track_ref:expr) => {{
171 let element_type = generic_field_type($remote, 0, "array")?;
172 if field_ref_mode(element_type) != $ref_mode {
173 return Err(array_ref_mismatch());
174 }
175 try_init_array(|| <$C as Codec<$T>>::read_field_with_type($context, element_type))
176 }};
177}
178
179macro_rules! array_read_declared {
180 (value, $T:ty, $S:ty, $N:ident, $context:expr, $remote:expr, $has_null:expr) => {
181 try_init_array(|| {
182 if $has_null && $context.reader.read_i8()? == RefFlag::Null as i8 {
183 <$S as Serializer>::default_value($context)
184 } else {
185 <$S as Serializer>::read_data($context)
186 }
187 })
188 };
189 (field, $T:ty, $C:ty, $N:ident, $context:expr, $remote:expr, $has_null:expr) => {{
190 let element_type = generic_field_type($remote, 0, "array")?;
191 read_declared_items::<$T, $C, $N>($context, element_type, $has_null)
192 }};
193}
194
195macro_rules! array_read_typed {
196 (value, $T:ty, $S:ty, $N:ident, $context:expr, $has_null:expr) => {{
197 let type_info = read_value_type_info::<$S>($context)?;
198 try_init_array(|| {
199 if $has_null && $context.reader.read_i8()? == RefFlag::Null as i8 {
200 return <$S as Serializer>::default_value($context);
201 }
202 match type_info.as_ref() {
203 Some(type_info) => {
204 <$S as Serializer>::read_with_type_info($context, RefMode::None, type_info)
205 }
206 None => <$S as Serializer>::read_data($context),
207 }
208 })
209 }};
210 (field, $T:ty, $C:ty, $N:ident, $context:expr, $has_null:expr) => {{
211 let read_type = <$C as Codec<$T>>::read_type_info_value($context)?;
212 read_typed_items::<$T, $C, $N>($context, &read_type, $has_null)
213 }};
214}
215
216macro_rules! read_object_array_body {
217 ($layer:ident, $T:ident, $C:ident, $N:ident, $context:expr, $remote:expr) => {{
218 let context = $context;
219 let len = context.reader.read_var_u32()?;
220 check_array_len(len, $N)?;
221 if $N == 0 {
222 return try_init_array(|| unreachable!());
223 }
224 let header = context.reader.read_u8()?;
225 let track_ref = (header & TRACKING_REF) != 0;
226 let same_type = (header & IS_SAME_TYPE) != 0;
227 let has_null = (header & HAS_NULL) != 0;
228 let declared = (header & DECL_ELEMENT_TYPE) != 0;
229 let ref_mode = if track_ref {
230 RefMode::Tracking
231 } else if has_null {
232 RefMode::NullOnly
233 } else {
234 RefMode::None
235 };
236
237 if $C::IS_POLYMORPHIC || $C::IS_SHARED_REF {
238 if same_type {
239 if declared {
240 return array_read_declared_dyn!(
241 $layer, $T, $C, $N, context, $remote, ref_mode, has_null, track_ref
242 );
243 }
244 let type_info = context.read_any_type_info()?;
245 return try_init_array(|| {
246 <$C as Serializer>::read_with_type_info(context, ref_mode, &type_info)
247 });
248 }
249 return try_init_array(|| <$C as Serializer>::read(context, ref_mode, true));
250 }
251
252 if !same_type {
253 return Err(non_polymorphic_array());
254 }
255 if declared {
256 return array_read_declared!($layer, $T, $C, $N, context, $remote, has_null);
257 }
258 array_read_typed!($layer, $T, $C, $N, context, has_null)
259 }};
260}
261
262#[inline(always)]
263fn read_value_object_array<T, S, const N: usize>(context: &mut ReadContext) -> Result<[T; N], Error>
264where
265 T: 'static,
266 S: Serializer<Target = T>,
267{
268 read_object_array_body!(value, T, S, N, context, ())
269}
270
271#[inline(always)]
272fn read_field_object_array<T, C, const N: usize>(
273 context: &mut ReadContext,
274 remote_field_type: &FieldType,
275) -> Result<[T; N], Error>
276where
277 T: 'static,
278 C: Codec<T>,
279{
280 read_object_array_body!(field, T, C, N, context, remote_field_type)
281}
282
283#[cold]
284#[inline(never)]
285fn array_ref_mismatch() -> Error {
286 Error::invalid_data("array header conflicts with declared element metadata")
287}
288
289impl<T, S, const N: usize, const NULLABLE: bool, const TRACK_REF: bool> Serializer
290 for ArrayCodec<T, S, N, NULLABLE, TRACK_REF>
291where
292 T: 'static,
293 S: Serializer<Target = T>,
294{
295 type Target = [T; N];
296
297 const READ_DATA_ALWAYS_ADVANCES: bool = true;
298
299 #[inline(always)]
300 fn write_data(value: &Self::Target, context: &mut WriteContext) -> Result<(), Error> {
301 if let Some(type_id) = selected_array_type_id::<T, S>() {
302 return primitive_list::write_data::<T, S>(value, context, type_id);
303 }
304 write_collection_value_data::<T, S, _, false, true>(value.iter(), context)
305 }
306
307 #[inline(always)]
308 fn read_data(context: &mut ReadContext) -> Result<Self::Target, Error> {
309 if let Some(type_id) = selected_array_type_id::<T, S>() {
310 return primitive_list::read_array::<T, S, N>(context, type_id);
311 }
312 read_value_object_array::<T, S, N>(context)
313 }
314
315 #[inline(always)]
316 fn default_value(context: &mut ReadContext) -> Result<Self::Target, Error> {
317 try_init_array(|| S::default_value(context))
318 }
319
320 #[inline(always)]
321 fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
322 match selected_array_type_id::<T, S>() {
323 Some(type_id) => primitive_list::write_type_info(context, type_id),
324 None => {
325 context.writer.write_u8(TypeId::LIST as u8);
326 Ok(())
327 }
328 }
329 }
330
331 #[inline(always)]
332 fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
333 match selected_array_type_id::<T, S>() {
334 Some(type_id) => primitive_list::read_type_info(context, type_id),
335 None => {
336 let remote = context.reader.read_u8()? as u32;
337 if remote != TypeId::LIST as u32 {
338 return Err(array_type_mismatch(TypeId::LIST as u32, remote));
339 }
340 Ok(())
341 }
342 }
343 }
344
345 #[inline(always)]
346 fn static_type_id() -> TypeId {
347 selected_array_type_id::<T, S>().unwrap_or(TypeId::LIST)
348 }
349
350 #[inline(always)]
351 fn reserved_space() -> usize {
352 match selected_array_type_id::<T, S>() {
353 Some(_) => std::mem::size_of::<T>() * N + SIZE_OF_REF_AND_TYPE,
354 None => std::mem::size_of::<u32>() + SIZE_OF_REF_AND_TYPE,
355 }
356 }
357}
358
359impl<T, C, const N: usize, const NULLABLE: bool, const TRACK_REF: bool> Codec<[T; N]>
360 for ArrayCodec<T, C, N, NULLABLE, TRACK_REF>
361where
362 T: 'static,
363 C: Codec<T>,
364{
365 #[inline(always)]
366 fn field_type(type_resolver: &TypeResolver) -> Result<FieldType, Error> {
367 if let Some(type_id) = selected_array_type_id::<T, C>() {
368 return Ok(FieldType::new_with_ref(
369 type_id as u32,
370 NULLABLE,
371 TRACK_REF,
372 Vec::new(),
373 ));
374 }
375 Ok(FieldType::new_with_ref(
376 TypeId::LIST as u32,
377 NULLABLE,
378 TRACK_REF,
379 vec![C::field_type(type_resolver)?],
380 ))
381 }
382
383 #[inline(always)]
384 fn write_field(value: &[T; N], context: &mut WriteContext) -> Result<(), Error> {
385 if NULLABLE || TRACK_REF {
386 context.writer.write_i8(RefFlag::NotNullValue as i8);
387 }
388 if selected_array_type_id::<T, C>().is_some() {
389 <Self as Serializer>::write_data(value, context)
390 } else {
391 write_collection_data::<T, C, _, false, true>(value.iter(), context, true)
392 }
393 }
394
395 #[inline(always)]
396 fn read_field(context: &mut ReadContext) -> Result<[T; N], Error> {
397 if (NULLABLE || TRACK_REF) && context.reader.read_i8()? == RefFlag::Null as i8 {
398 return <Self as Serializer>::default_value(context);
399 }
400 <Self as Serializer>::read_data(context)
401 }
402
403 #[inline(always)]
404 fn read_compatible(
405 context: &mut ReadContext,
406 local_field_type: &FieldType,
407 remote_field_type: &FieldType,
408 ) -> Result<Option<[T; N]>, Error> {
409 if field_types_compatible(local_field_type, remote_field_type)
410 || local_field_type.compatible_shape_match(remote_field_type)
411 || (local_field_type.type_id == remote_field_type.type_id
412 && allows_missing_generics(local_field_type.type_id)
413 && (local_field_type.generics.is_empty() || remote_field_type.generics.is_empty()))
414 {
415 return Self::read_field_with_type(context, remote_field_type).map(Some);
416 }
417 Ok(None)
418 }
419
420 #[inline(always)]
421 fn read_data_with_type(
422 context: &mut ReadContext,
423 remote_data_type: &FieldType,
424 ) -> Result<[T; N], Error> {
425 if let Some(type_id) = selected_array_type_id::<T, C>() {
426 if remote_data_type.type_id != TypeId::LIST as u32 {
427 return primitive_list::read_array::<T, C, N>(context, type_id);
428 }
429 }
430 read_field_object_array::<T, C, N>(context, remote_data_type)
431 }
432
433 #[inline(always)]
434 fn read_field_with_type(
435 context: &mut ReadContext,
436 remote_field_type: &FieldType,
437 ) -> Result<[T; N], Error> {
438 if field_ref_mode(remote_field_type) != RefMode::None
439 && context.reader.read_i8()? == RefFlag::Null as i8
440 {
441 return <Self as Serializer>::default_value(context);
442 }
443 Self::read_data_with_type(context, remote_field_type)
444 }
445
446 #[inline(always)]
447 fn write_with_mode(
448 value: &[T; N],
449 context: &mut WriteContext,
450 ref_mode: RefMode,
451 write_type_info: bool,
452 has_generics: bool,
453 ) -> Result<(), Error> {
454 if selected_array_type_id::<T, C>().is_some() || !has_generics {
455 return <Self as Serializer>::write(value, context, ref_mode, write_type_info);
456 }
457 if ref_mode != RefMode::None {
458 context.writer.write_i8(RefFlag::NotNullValue as i8);
459 }
460 if write_type_info {
461 <Self as Serializer>::write_type_info(context)?;
462 }
463 write_collection_data::<T, C, _, false, true>(value.iter(), context, true)
464 }
465}
466
467type RootArrayCodec<S, const N: usize> = ArrayCodec<<S as Serializer>::Target, S, N, false, false>;
468
469pub struct ArraySerializer<S, const N: usize>(PhantomData<fn() -> S>);
474
475impl<S: Serializer, const N: usize> Serializer for ArraySerializer<S, N> {
476 type Target = [S::Target; N];
477
478 const READ_DATA_ALWAYS_ADVANCES: bool = true;
479
480 #[inline(always)]
481 fn write_data(value: &Self::Target, context: &mut WriteContext) -> Result<(), Error> {
482 <RootArrayCodec<S, N> as Serializer>::write_data(value, context)
483 }
484
485 #[inline(always)]
486 fn read_data(context: &mut ReadContext) -> Result<Self::Target, Error> {
487 <RootArrayCodec<S, N> as Serializer>::read_data(context)
488 }
489
490 #[inline(always)]
491 fn default_value(context: &mut ReadContext) -> Result<Self::Target, Error> {
492 <RootArrayCodec<S, N> as Serializer>::default_value(context)
493 }
494
495 #[inline(always)]
496 fn write(
497 value: &Self::Target,
498 context: &mut WriteContext,
499 ref_mode: RefMode,
500 write_type_info: bool,
501 ) -> Result<(), Error> {
502 <RootArrayCodec<S, N> as Serializer>::write(value, context, ref_mode, write_type_info)
503 }
504
505 #[inline(always)]
506 fn read(
507 context: &mut ReadContext,
508 ref_mode: RefMode,
509 read_type_info: bool,
510 ) -> Result<Self::Target, Error> {
511 <RootArrayCodec<S, N> as Serializer>::read(context, ref_mode, read_type_info)
512 }
513
514 #[inline(always)]
515 fn read_with_type_info(
516 context: &mut ReadContext,
517 ref_mode: RefMode,
518 type_info: &Rc<TypeInfo>,
519 ) -> Result<Self::Target, Error> {
520 <RootArrayCodec<S, N> as Serializer>::read_with_type_info(context, ref_mode, type_info)
521 }
522
523 #[inline(always)]
524 fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
525 <RootArrayCodec<S, N> as Serializer>::write_type_info(context)
526 }
527
528 #[inline(always)]
529 fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
530 <RootArrayCodec<S, N> as Serializer>::read_type_info(context)
531 }
532
533 #[inline(always)]
534 fn static_type_id() -> TypeId {
535 <RootArrayCodec<S, N> as Serializer>::static_type_id()
536 }
537
538 #[inline(always)]
539 fn reserved_space() -> usize {
540 <RootArrayCodec<S, N> as Serializer>::reserved_space()
541 }
542}
543
544impl<T, const N: usize> Serializer for [T; N]
545where
546 T: Serializer<Target = T>,
547{
548 type Target = Self;
549
550 const READ_DATA_ALWAYS_ADVANCES: bool = true;
551
552 #[inline(always)]
553 fn write_data(value: &Self, context: &mut WriteContext) -> Result<(), Error> {
554 <ArraySerializer<T, N> as Serializer>::write_data(value, context)
555 }
556
557 #[inline(always)]
558 fn read_data(context: &mut ReadContext) -> Result<Self, Error> {
559 <ArraySerializer<T, N> as Serializer>::read_data(context)
560 }
561
562 #[inline(always)]
563 fn default_value(context: &mut ReadContext) -> Result<Self, Error> {
564 <ArraySerializer<T, N> as Serializer>::default_value(context)
565 }
566
567 #[inline(always)]
568 fn write(
569 value: &Self,
570 context: &mut WriteContext,
571 ref_mode: RefMode,
572 write_type_info: bool,
573 ) -> Result<(), Error> {
574 <ArraySerializer<T, N> as Serializer>::write(value, context, ref_mode, write_type_info)
575 }
576
577 #[inline(always)]
578 fn read(
579 context: &mut ReadContext,
580 ref_mode: RefMode,
581 read_type_info: bool,
582 ) -> Result<Self, Error> {
583 <ArraySerializer<T, N> as Serializer>::read(context, ref_mode, read_type_info)
584 }
585
586 #[inline(always)]
587 fn read_with_type_info(
588 context: &mut ReadContext,
589 ref_mode: RefMode,
590 type_info: &Rc<TypeInfo>,
591 ) -> Result<Self, Error> {
592 <ArraySerializer<T, N> as Serializer>::read_with_type_info(context, ref_mode, type_info)
593 }
594
595 #[inline(always)]
596 fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
597 <ArraySerializer<T, N> as Serializer>::write_type_info(context)
598 }
599
600 #[inline(always)]
601 fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
602 <ArraySerializer<T, N> as Serializer>::read_type_info(context)
603 }
604
605 #[inline(always)]
606 fn static_type_id() -> TypeId {
607 <ArraySerializer<T, N> as Serializer>::static_type_id()
608 }
609
610 #[inline(always)]
611 fn reserved_space() -> usize {
612 <ArraySerializer<T, N> as Serializer>::reserved_space()
613 }
614}