MCPcopy Create free account
hub / github.com/apache/fory / read_primitive_array

Function read_primitive_array

rust/fory-core/src/serializer/array.rs:61–88  ·  view source on GitHub ↗
(context: &mut ReadContext)

Source from the content-addressed store, hash-verified

59/// Read primitive array directly without intermediate Vec allocation
60#[inline]
61fn read_primitive_array<T, const N: usize>(context: &mut ReadContext) -> Result<[T; N], Error>
62where
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]

Callers 1

fory_read_dataMethod · 0.85

Calls 4

validate_array_lengthFunction · 0.85
assume_array_initFunction · 0.85
read_var_u32Method · 0.80
read_bytesMethod · 0.45

Tested by

no test coverage detected