(context: &mut ReadContext)
| 59 | /// Read primitive array directly without intermediate Vec allocation |
| 60 | #[inline] |
| 61 | fn read_primitive_array<T, const N: usize>(context: &mut ReadContext) -> Result<[T; N], Error> |
| 62 | where |
| 63 | T: Serializer + ForyDefault, |
| 64 | { |
| 65 | // Read the size in bytes |
| 66 | let size_bytes = context.reader.read_var_u32()? as usize; |
| 67 | let elem_size = mem::size_of::<T>(); |
| 68 | if size_bytes % elem_size != 0 { |
| 69 | return Err(Error::invalid_data("Invalid data length")); |
| 70 | } |
| 71 | let len = size_bytes / elem_size; |
| 72 | validate_array_length(len, N)?; |
| 73 | // Handle zero-sized arrays |
| 74 | if N == 0 { |
| 75 | // Safe: std::mem::zeroed() is explicitly safe for zero-sized types |
| 76 | return Ok(unsafe { std::mem::zeroed() }); |
| 77 | } |
| 78 | // Create uninitialized array |
| 79 | let mut arr: [MaybeUninit<T>; N] = unsafe { MaybeUninit::uninit().assume_init() }; |
| 80 | // Read bytes directly into array memory |
| 81 | unsafe { |
| 82 | let dst_ptr = arr.as_mut_ptr() as *mut u8; |
| 83 | let src = context.reader.read_bytes(size_bytes)?; |
| 84 | std::ptr::copy_nonoverlapping(src.as_ptr(), dst_ptr, size_bytes); |
| 85 | } |
| 86 | // Safety: all elements are now initialized with data from the reader |
| 87 | Ok(unsafe { assume_array_init(&arr) }) |
| 88 | } |
| 89 | |
| 90 | /// Read complex (non-primitive) array directly without intermediate Vec allocation |
| 91 | #[inline] |
no test coverage detected