Skip to content

Cannot provide user-defined Debug implementation for enum #2143

@gonzalobg

Description

@gonzalobg

Input C/C++ Header

typedef enum E {
  A = 0,
  B = 1
} ET;

Bindgen Invocation

fn main() {
    let bindings = bindgen::builder()
        .header("src/header.h")
        .default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: true })
        .derive_debug(false)  // Don't derive debug
        .parse_callbacks(Box::new(MyCallback))
        .generate()
        .expect("Unable to generate bindings");

    let out_path = std::path::Path::new("src/ffi.rs");
    bindings.write_to_file(out_path).expect("Couldn't write bindings");
}

#[derive(Debug)]
struct MyCallback;
impl core::panic::UnwindSafe for MyCallback {}

impl bindgen::callbacks::ParseCallbacks for MyCallback {
    fn add_derives(&self, name: &str) -> Vec<String> {
        let mut v: Vec<String> = Vec::new();
        if name == "E" {
            v = vec!["Debug".to_string()];  // Provide my own Debug impl somewhere
        }
        v
    }
}

Actual Results

The above fails with:

error[E0119]: conflicting implementations of trait `std::fmt::Debug` for type `E`
 --> src/ffi.rs:5:51
  |
5 | #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Debug)]
  |          ----- first implementation here          ^^^^^ conflicting implementation for `E`

The .derive_debug(false) is set, but a Debug impl is still being added, which prevents me from providing my own implementation somewhere (not necessarily in a callback).

Expected Results

I'd like to be able to provide my own implementation of Debug for the type above, while still automatically adding the Debug trait to all other types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions