(ctx: &Context, class: &'static Py<PyType>)
| 300 | |
| 301 | #[extend_class] |
| 302 | fn extend_pyclass(ctx: &Context, class: &'static Py<PyType>) { |
| 303 | // Getters for named visible fields (indices 0 to REQUIRED_FIELD_NAMES.len() - 1) |
| 304 | for (i, &name) in Self::Data::REQUIRED_FIELD_NAMES.iter().enumerate() { |
| 305 | // cast i to a u8 so there's less to store in the getter closure. |
| 306 | // Hopefully there's not struct sequences with >=256 elements :P |
| 307 | let i = i as u8; |
| 308 | class.set_attr( |
| 309 | ctx.intern_str(name), |
| 310 | ctx.new_readonly_getset(name, class, move |zelf: &PyTuple| { |
| 311 | zelf[i as usize].to_owned() |
| 312 | }) |
| 313 | .into(), |
| 314 | ); |
| 315 | } |
| 316 | |
| 317 | // Getters for hidden/skipped fields (indices after visible fields) |
| 318 | let visible_count = Self::Data::REQUIRED_FIELD_NAMES.len() + Self::Data::UNNAMED_FIELDS_LEN; |
| 319 | for (i, &name) in Self::Data::OPTIONAL_FIELD_NAMES.iter().enumerate() { |
| 320 | let idx = (visible_count + i) as u8; |
| 321 | class.set_attr( |
| 322 | ctx.intern_str(name), |
| 323 | ctx.new_readonly_getset(name, class, move |zelf: &PyTuple| { |
| 324 | zelf[idx as usize].to_owned() |
| 325 | }) |
| 326 | .into(), |
| 327 | ); |
| 328 | } |
| 329 | |
| 330 | class.set_attr( |
| 331 | identifier!(ctx, __match_args__), |
| 332 | ctx.new_tuple( |
| 333 | Self::Data::REQUIRED_FIELD_NAMES |
| 334 | .iter() |
| 335 | .map(|&name| ctx.new_str(name).into()) |
| 336 | .collect::<Vec<_>>(), |
| 337 | ) |
| 338 | .into(), |
| 339 | ); |
| 340 | |
| 341 | // special fields: |
| 342 | // n_sequence_fields = visible fields (named + unnamed) |
| 343 | // n_fields = all fields (visible + hidden/skipped) |
| 344 | // n_unnamed_fields |
| 345 | let n_unnamed_fields = Self::Data::UNNAMED_FIELDS_LEN; |
| 346 | let n_sequence_fields = Self::Data::REQUIRED_FIELD_NAMES.len() + n_unnamed_fields; |
| 347 | let n_fields = n_sequence_fields + Self::Data::OPTIONAL_FIELD_NAMES.len(); |
| 348 | class.set_attr( |
| 349 | identifier!(ctx, n_sequence_fields), |
| 350 | ctx.new_int(n_sequence_fields).into(), |
| 351 | ); |
| 352 | class.set_attr(identifier!(ctx, n_fields), ctx.new_int(n_fields).into()); |
| 353 | class.set_attr( |
| 354 | identifier!(ctx, n_unnamed_fields), |
| 355 | ctx.new_int(n_unnamed_fields).into(), |
| 356 | ); |
| 357 | |
| 358 | // Override as_sequence and as_mapping slots to use visible length |
| 359 | class |
nothing calls this directly
no test coverage detected