in src/bindgen/ir/structure.rs [315:373]
fn rename_for_config(&mut self, config: &Config) {
// Rename the name of the struct
if !(self.has_tag_field && config.language == Language::Cxx) {
config.export.rename(&mut self.export_name);
}
// Rename the types used in fields
{
let fields = self.fields.iter_mut().skip(self.has_tag_field as usize);
for field in fields {
field.ty.rename_for_config(config, &self.generic_params);
}
}
// Apply renaming rules to fields in the following order
// 1. `cbindgen::field-names` annotation
// 2. `cbindgen::rename-all` annotation
// 3. config struct rename rule
// If the struct is a tuple struct and we have not renamed the
// fields, then prefix each of them with an underscore.
// If any field is a reserved keyword, then postfix it with an
// underscore.
// Scope for mutable borrow of fields
{
let names = self.fields.iter_mut().map(|field| &mut field.name);
let field_rules = self.annotations.parse_atom::<RenameRule>("rename-all");
let field_rules = field_rules
.as_ref()
.unwrap_or(&config.structure.rename_fields);
if let Some(o) = self.annotations.list("field-names") {
for (dest, src) in names.zip(o) {
*dest = src;
}
} else if let Some(r) = field_rules.not_none() {
for name in names {
*name = r.apply(name, IdentifierType::StructMember).into_owned();
}
} else {
// If we don't have any rules for a tuple struct, prefix them with
// an underscore so it still compiles.
for name in names {
if name.starts_with(|c: char| c.is_ascii_digit()) {
name.insert(0, '_');
}
}
}
}
for field in &mut self.fields {
reserved::escape(&mut field.name);
}
for c in self.associated_constants.iter_mut() {
c.rename_for_config(config);
}
}