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

Function read_complex_array

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

Source from the content-addressed store, hash-verified

90/// Read complex (non-primitive) array directly without intermediate Vec allocation
91#[inline]
92fn read_complex_array<T, const N: usize>(context: &mut ReadContext) -> Result<[T; N], Error>
93where
94 T: Serializer + ForyDefault,
95{
96 // Read collection length
97 let len = context.reader.read_var_u32()? as usize;
98 validate_array_length(len, N)?;
99 // Handle zero-sized arrays
100 if N == 0 {
101 // Safe: std::mem::zeroed() is explicitly safe for zero-sized types
102 return Ok(unsafe { std::mem::zeroed() });
103 }
104 // Handle polymorphic or shared ref types - need to use collection logic
105 if T::fory_is_polymorphic() || T::fory_is_shared_ref() {
106 return read_complex_array_dyn_ref(context, len);
107 }
108 // Read header
109 let header = context.reader.read_u8()?;
110 let declared = (header & DECL_ELEMENT_TYPE) != 0;
111 if !declared {
112 T::fory_read_type_info(context)?;
113 }
114 let has_null = (header & HAS_NULL) != 0;
115 ensure!(
116 (header & IS_SAME_TYPE) != 0,
117 Error::type_error("Type inconsistent, target type is not polymorphic")
118 );
119 // Create uninitialized array
120 let mut arr: [MaybeUninit<T>; N] = unsafe { MaybeUninit::uninit().assume_init() };
121 // Read elements directly into array
122 if !has_null {
123 for elem_slot in &mut arr[..] {
124 let elem = T::fory_read_data(context)?;
125 elem_slot.write(elem);
126 }
127 } else {
128 for elem_slot in &mut arr[..] {
129 let flag = context.reader.read_i8()?;
130 let elem = if flag == RefFlag::Null as i8 {
131 T::fory_default()
132 } else {
133 T::fory_read_data(context)?
134 };
135 elem_slot.write(elem);
136 }
137 }
138 // Safety: all elements are now initialized
139 Ok(unsafe { std::ptr::read(&arr as *const _ as *const [T; N]) })
140}
141
142/// Read complex array with dynamic/polymorphic types
143#[inline]

Callers 1

fory_read_dataMethod · 0.85

Calls 9

validate_array_lengthFunction · 0.85
fory_read_type_infoFunction · 0.85
fory_read_dataFunction · 0.85
read_var_u32Method · 0.80
read_u8Method · 0.80
read_i8Method · 0.80
readFunction · 0.70
writeMethod · 0.65

Tested by

no test coverage detected