wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Rust: Support path-specific `additional_derives`

Open tomasol opened this issue 3 months ago • 1 comments

Currently additional_derives can only be set globally, which feels too broad. For use cases like adding serde annotations it would be better if the derives could be scoped per type or interface. Would it make sense to extend the generate! macro to accept a map of path -> derives?

tomasol avatar Sep 11 '25 10:09 tomasol

Not just derives but heavily in need of type targeted attrs. Use-cases like serde customization are critical in many cases. Also, would be great if it was hierarchical so that it can target whole packages/modules/types with ease.

For the basic need of segregating derives, I'm currently using multiple generate! sections:

mod wit {
   pub mod serde {
        wit_bindgen::generate!({
            //  #1395 unfortunately, the linker doesn't like `generate!` on the same world twice
            world: "serde-world",
            generate_all,
            additional_derives: [serde::Serialize, serde::Deserialize],
        });
    }
    wit_bindgen::generate!({
        world: "true-world",
        with: {
            "path:to/world": crate::wit::serde::path::to::world,
            // wasi io doesn't like serde
            "wasi:io/[email protected]": generate,
            "wasi:logging/[email protected]": wasi::wit,
        }
    });
}

With how popular proc-macros are in the rust ecosystem, I suspect I'll always wanting more customization in generate!. Though maybe there's some elegant solution lying in wait.

Right now, the solution I've settled on is to write a simple code generator that spits out wit types, corresponding rust types with customization and their binding generate! from a single source of truth.

dman-os avatar Oct 05 '25 07:10 dman-os