Skip to content

Allow per-type control of the derive_debug and impl_debug behavior via a new ParseCallbacks method #3285

@ivmaykov

Description

@ivmaykov

Related: #3268

It seems that bindgen has some bugs regarding derive_debug and impl_debug behavior. Since there may be many corner cases and multiple bugs with various root causes involved, fixing it in the general case may be hard. I propose adding a new method to ParseCallbacks that allows one to override the default derive_debug / impl_debug behavior on a per-type basis. This would be opt-in, similar to the current add_derives and add_attributes methods. This way, users who don't care about this can keep the current behavior, and users who do care (like me!) can fine-tune bindgen's behavior to fit their needs.

Proposed API is something like this:

#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Hash)]
enum DebugBehavior {
    /// will not derive or impl Debug for the type
    NoDebug,
    /// will derive(Debug) for the type
    DeriveDebug,
    /// will impl Debug for the type
    ImplDebug,
}

#[non_exhaustive]
pub struct OverrideDebugBehaviorInfo<'a> {
    pub name: &'a str,
    pub kind: TypeKind,
    pub behavior: DebugBehavior,
}

pub trait ParseCallbacks: Debug {
    // ... existing methods

    /// Method which allows overriding the debug behavior for a type.
    /// Input describes the name and kind of the type, as well as the debug behavior
    /// that will be applied to the type unless it is overriden.
    /// - implementations can return None to keep the current behavior, or:
    /// - return Some(DebugBehavior::NoDebug) to force the type to be generated without `#[derive(Debug)]` or `impl Debug`
    /// - return Some(DebugBehavior::DeriveDebug) to force the type to be generated with `#[derive(Debug)]`
    /// - return Some(DebugBehavior::ImplDebug) to force the type to be generated with `impl Debug`
    ///
    /// The default implementation returns None for all inputs
    fn override_debug_behavior(&self, _info: &OverrideDebugBehaviorInfo<'_>) -> Option<DebugBehavior> { ... }

This callback would be invoked after all other decisions about the debug behavior of the type are made (i.e. we compute whether we should derive or impl debug, and we call the add_derives callback, before calling this one)

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