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 #[inline(always)]
298 fn write_data(value: &Self::Target, context: &mut WriteContext) -> Result<(), Error> {
299 if let Some(type_id) = selected_array_type_id::<T, S>() {
300 return primitive_list::write_data::<T, S>(value, context, type_id);
301 }
302 write_collection_value_data::<T, S, _, false, true>(value.iter(), context)
303 }
304
305 #[inline(always)]
306 fn read_data(context: &mut ReadContext) -> Result<Self::Target, Error> {
307 if let Some(type_id) = selected_array_type_id::<T, S>() {
308 return primitive_list::read_array::<T, S, N>(context, type_id);
309 }
310 read_value_object_array::<T, S, N>(context)
311 }
312
313 #[inline(always)]
314 fn default_value(context: &mut ReadContext) -> Result<Self::Target, Error> {
315 try_init_array(|| S::default_value(context))
316 }
317
318 #[inline(always)]
319 fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
320 match selected_array_type_id::<T, S>() {
321 Some(type_id) => primitive_list::write_type_info(context, type_id),
322 None => {
323 context.writer.write_u8(TypeId::LIST as u8);
324 Ok(())
325 }
326 }
327 }
328
329 #[inline(always)]
330 fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
331 match selected_array_type_id::<T, S>() {
332 Some(type_id) => primitive_list::read_type_info(context, type_id),
333 None => {
334 let remote = context.reader.read_u8()? as u32;
335 if remote != TypeId::LIST as u32 {
336 return Err(array_type_mismatch(TypeId::LIST as u32, remote));
337 }
338 Ok(())
339 }
340 }
341 }
342
343 #[inline(always)]
344 fn static_type_id() -> TypeId {
345 selected_array_type_id::<T, S>().unwrap_or(TypeId::LIST)
346 }
347
348 #[inline(always)]
349 fn reserved_space() -> usize {
350 match selected_array_type_id::<T, S>() {
351 Some(_) => std::mem::size_of::<T>() * N + SIZE_OF_REF_AND_TYPE,
352 None => std::mem::size_of::<u32>() + SIZE_OF_REF_AND_TYPE,
353 }
354 }
355}
356
357impl<T, C, const N: usize, const NULLABLE: bool, const TRACK_REF: bool> Codec<[T; N]>
358 for ArrayCodec<T, C, N, NULLABLE, TRACK_REF>
359where
360 T: 'static,
361 C: Codec<T>,
362{
363 #[inline(always)]
364 fn field_type(type_resolver: &TypeResolver) -> Result<FieldType, Error> {
365 if let Some(type_id) = selected_array_type_id::<T, C>() {
366 return Ok(FieldType::new_with_ref(
367 type_id as u32,
368 NULLABLE,
369 TRACK_REF,
370 Vec::new(),
371 ));
372 }
373 Ok(FieldType::new_with_ref(
374 TypeId::LIST as u32,
375 NULLABLE,
376 TRACK_REF,
377 vec![C::field_type(type_resolver)?],
378 ))
379 }
380
381 #[inline(always)]
382 fn write_field(value: &[T; N], context: &mut WriteContext) -> Result<(), Error> {
383 if NULLABLE || TRACK_REF {
384 context.writer.write_i8(RefFlag::NotNullValue as i8);
385 }
386 if selected_array_type_id::<T, C>().is_some() {
387 <Self as Serializer>::write_data(value, context)
388 } else {
389 write_collection_data::<T, C, _, false, true>(value.iter(), context, true)
390 }
391 }
392
393 #[inline(always)]
394 fn read_field(context: &mut ReadContext) -> Result<[T; N], Error> {
395 if (NULLABLE || TRACK_REF) && context.reader.read_i8()? == RefFlag::Null as i8 {
396 return <Self as Serializer>::default_value(context);
397 }
398 <Self as Serializer>::read_data(context)
399 }
400
401 #[inline(always)]
402 fn read_compatible(
403 context: &mut ReadContext,
404 local_field_type: &FieldType,
405 remote_field_type: &FieldType,
406 ) -> Result<Option<[T; N]>, Error> {
407 if field_types_compatible(local_field_type, remote_field_type)
408 || local_field_type.compatible_shape_match(remote_field_type)
409 || (local_field_type.type_id == remote_field_type.type_id
410 && allows_missing_generics(local_field_type.type_id)
411 && (local_field_type.generics.is_empty() || remote_field_type.generics.is_empty()))
412 {
413 return Self::read_field_with_type(context, remote_field_type).map(Some);
414 }
415 Ok(None)
416 }
417
418 #[inline(always)]
419 fn read_data_with_type(
420 context: &mut ReadContext,
421 remote_data_type: &FieldType,
422 ) -> Result<[T; N], Error> {
423 if let Some(type_id) = selected_array_type_id::<T, C>() {
424 if remote_data_type.type_id != TypeId::LIST as u32 {
425 return primitive_list::read_array::<T, C, N>(context, type_id);
426 }
427 }
428 read_field_object_array::<T, C, N>(context, remote_data_type)
429 }
430
431 #[inline(always)]
432 fn read_field_with_type(
433 context: &mut ReadContext,
434 remote_field_type: &FieldType,
435 ) -> Result<[T; N], Error> {
436 if field_ref_mode(remote_field_type) != RefMode::None
437 && context.reader.read_i8()? == RefFlag::Null as i8
438 {
439 return <Self as Serializer>::default_value(context);
440 }
441 Self::read_data_with_type(context, remote_field_type)
442 }
443
444 #[inline(always)]
445 fn write_with_mode(
446 value: &[T; N],
447 context: &mut WriteContext,
448 ref_mode: RefMode,
449 write_type_info: bool,
450 has_generics: bool,
451 ) -> Result<(), Error> {
452 if selected_array_type_id::<T, C>().is_some() || !has_generics {
453 return <Self as Serializer>::write(value, context, ref_mode, write_type_info);
454 }
455 if ref_mode != RefMode::None {
456 context.writer.write_i8(RefFlag::NotNullValue as i8);
457 }
458 if write_type_info {
459 <Self as Serializer>::write_type_info(context)?;
460 }
461 write_collection_data::<T, C, _, false, true>(value.iter(), context, true)
462 }
463}
464
465type RootArrayCodec<S, const N: usize> = ArrayCodec<<S as Serializer>::Target, S, N, false, false>;
466
467pub struct ArraySerializer<S, const N: usize>(PhantomData<fn() -> S>);
472
473impl<S: Serializer, const N: usize> Serializer for ArraySerializer<S, N> {
474 type Target = [S::Target; N];
475
476 #[inline(always)]
477 fn write_data(value: &Self::Target, context: &mut WriteContext) -> Result<(), Error> {
478 <RootArrayCodec<S, N> as Serializer>::write_data(value, context)
479 }
480
481 #[inline(always)]
482 fn read_data(context: &mut ReadContext) -> Result<Self::Target, Error> {
483 <RootArrayCodec<S, N> as Serializer>::read_data(context)
484 }
485
486 #[inline(always)]
487 fn default_value(context: &mut ReadContext) -> Result<Self::Target, Error> {
488 <RootArrayCodec<S, N> as Serializer>::default_value(context)
489 }
490
491 #[inline(always)]
492 fn write(
493 value: &Self::Target,
494 context: &mut WriteContext,
495 ref_mode: RefMode,
496 write_type_info: bool,
497 ) -> Result<(), Error> {
498 <RootArrayCodec<S, N> as Serializer>::write(value, context, ref_mode, write_type_info)
499 }
500
501 #[inline(always)]
502 fn read(
503 context: &mut ReadContext,
504 ref_mode: RefMode,
505 read_type_info: bool,
506 ) -> Result<Self::Target, Error> {
507 <RootArrayCodec<S, N> as Serializer>::read(context, ref_mode, read_type_info)
508 }
509
510 #[inline(always)]
511 fn read_with_type_info(
512 context: &mut ReadContext,
513 ref_mode: RefMode,
514 type_info: &Rc<TypeInfo>,
515 ) -> Result<Self::Target, Error> {
516 <RootArrayCodec<S, N> as Serializer>::read_with_type_info(context, ref_mode, type_info)
517 }
518
519 #[inline(always)]
520 fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
521 <RootArrayCodec<S, N> as Serializer>::write_type_info(context)
522 }
523
524 #[inline(always)]
525 fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
526 <RootArrayCodec<S, N> as Serializer>::read_type_info(context)
527 }
528
529 #[inline(always)]
530 fn static_type_id() -> TypeId {
531 <RootArrayCodec<S, N> as Serializer>::static_type_id()
532 }
533
534 #[inline(always)]
535 fn reserved_space() -> usize {
536 <RootArrayCodec<S, N> as Serializer>::reserved_space()
537 }
538}
539
540impl<T, const N: usize> Serializer for [T; N]
541where
542 T: Serializer<Target = T>,
543{
544 type Target = Self;
545
546 #[inline(always)]
547 fn write_data(value: &Self, context: &mut WriteContext) -> Result<(), Error> {
548 <ArraySerializer<T, N> as Serializer>::write_data(value, context)
549 }
550
551 #[inline(always)]
552 fn read_data(context: &mut ReadContext) -> Result<Self, Error> {
553 <ArraySerializer<T, N> as Serializer>::read_data(context)
554 }
555
556 #[inline(always)]
557 fn default_value(context: &mut ReadContext) -> Result<Self, Error> {
558 <ArraySerializer<T, N> as Serializer>::default_value(context)
559 }
560
561 #[inline(always)]
562 fn write(
563 value: &Self,
564 context: &mut WriteContext,
565 ref_mode: RefMode,
566 write_type_info: bool,
567 ) -> Result<(), Error> {
568 <ArraySerializer<T, N> as Serializer>::write(value, context, ref_mode, write_type_info)
569 }
570
571 #[inline(always)]
572 fn read(
573 context: &mut ReadContext,
574 ref_mode: RefMode,
575 read_type_info: bool,
576 ) -> Result<Self, Error> {
577 <ArraySerializer<T, N> as Serializer>::read(context, ref_mode, read_type_info)
578 }
579
580 #[inline(always)]
581 fn read_with_type_info(
582 context: &mut ReadContext,
583 ref_mode: RefMode,
584 type_info: &Rc<TypeInfo>,
585 ) -> Result<Self, Error> {
586 <ArraySerializer<T, N> as Serializer>::read_with_type_info(context, ref_mode, type_info)
587 }
588
589 #[inline(always)]
590 fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
591 <ArraySerializer<T, N> as Serializer>::write_type_info(context)
592 }
593
594 #[inline(always)]
595 fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
596 <ArraySerializer<T, N> as Serializer>::read_type_info(context)
597 }
598
599 #[inline(always)]
600 fn static_type_id() -> TypeId {
601 <ArraySerializer<T, N> as Serializer>::static_type_id()
602 }
603
604 #[inline(always)]
605 fn reserved_space() -> usize {
606 <ArraySerializer<T, N> as Serializer>::reserved_space()
607 }
608}