rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.23k stars 679 forks source link

RFE: API for renaming struct fields #2788

Open jgcodes2020 opened 3 months ago

jgcodes2020 commented 3 months ago

Background

ParseCallbacks::enum_variant_name is already useful for renaming enum variants. I'm using it to ensure that generated bindings fit Rust conventions. However, I'd like to also rename struct fields, and there doesn't seem to be a callback for that.

Proposal

Add fn struct_field_name to ParseCallbacks with the following signature:

fn struct_field_name(
    &self,
    struct_name: Option<&str>,
    original_field_name: &str,
) -> Option<String>

This works in a similar manner to ParseCallbacks::enum_variant_name; if the user chooses to return None, the name isn't changed, but if the user chooses to return Some(string), then the field is renamed to string.

pvdrz commented 3 months ago

:thinking: I wonder if having a single callback for both variants and structs would be a reasonable alternative or not