(source_fields: &[SourceField<'_>])
| 130 | } |
| 131 | |
| 132 | pub fn gen_read_data(source_fields: &[SourceField<'_>]) -> TokenStream { |
| 133 | let fields: Vec<&Field> = source_fields.iter().map(|sf| sf.field).collect(); |
| 134 | // Generate runtime version hash computation that detects enum fields |
| 135 | let version_hash_ts = gen_struct_version_hash_ts(&fields); |
| 136 | let read_fields = if source_fields.is_empty() { |
| 137 | quote! {} |
| 138 | } else { |
| 139 | let loop_ts = get_source_fields_loop_ts(source_fields); |
| 140 | quote! { |
| 141 | #loop_ts |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | let is_tuple = source_fields |
| 146 | .first() |
| 147 | .map(|sf| sf.is_tuple_struct) |
| 148 | .unwrap_or(false); |
| 149 | |
| 150 | // Generate field initializations, sorted by original index for tuple structs |
| 151 | let mut indexed: Vec<_> = source_fields |
| 152 | .iter() |
| 153 | .map(|sf| { |
| 154 | let private_ident = create_private_field_name(sf.field, sf.original_index); |
| 155 | let value = quote! { #private_ident }; |
| 156 | (sf.original_index, sf.field_init(value)) |
| 157 | }) |
| 158 | .collect(); |
| 159 | |
| 160 | if is_tuple { |
| 161 | indexed.sort_by_key(|(idx, _)| *idx); |
| 162 | } |
| 163 | |
| 164 | let field_inits: Vec<_> = indexed.into_iter().map(|(_, ts)| ts).collect(); |
| 165 | let self_construction = crate::util::ok_self_construction(is_tuple, &field_inits); |
| 166 | |
| 167 | quote! { |
| 168 | // Read and check version hash when class version checking is enabled |
| 169 | if context.is_check_struct_version() { |
| 170 | let read_version = context.reader.read_i32()?; |
| 171 | let type_name = ::std::any::type_name::<Self>(); |
| 172 | let local_version: i32 = #version_hash_ts; |
| 173 | fory_core::meta::TypeMeta::check_struct_version(read_version, local_version, type_name)?; |
| 174 | } |
| 175 | #read_fields |
| 176 | #self_construction |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | pub fn gen_read(_struct_ident: &Ident) -> TokenStream { |
| 181 | // Note: We use `Self` instead of `#struct_ident` to correctly handle generic types. |
no test coverage detected