Skip to main content

gen_struct_with_rename

Function gen_struct_with_rename 

Source
pub fn gen_struct_with_rename(
    typ: &TypeDef,
    mapper: &dyn TypeMapper,
    cfg: &RustBindingConfig<'_>,
    extra_field_attrs: impl Fn(&FieldDef) -> Vec<String>,
    field_name_override: impl Fn(&FieldDef) -> Option<String>,
) -> String
Expand description

Generate a struct definition using the builder, with per-field attribute and name override callbacks.

This is the most flexible variant. Use it when the target language may need to escape reserved keywords in field names (e.g. Python’s classclass_).

  • extra_field_attrs — called per field, returns additional #[…] attribute strings to append after cfg.field_attrs. Return an empty vec for the default behaviour.
  • field_name_override — called per field, returns Some(escaped_name) when the Rust binding struct field name should differ from field.name (e.g. for keyword escaping), or None to keep the original name.

When a field name is overridden the caller is responsible for adding the appropriate language attribute (e.g. pyo3(get, name = "original")) via extra_field_attrs. cfg.field_attrs is still applied for non-renamed fields; for renamed fields the caller should replace the default field attrs entirely by returning them from extra_field_attrs and passing a modified cfg with empty field_attrs.