Search code examples
rustconditional-compilation

How do I use cfg to set different attributes on different target architectures?


I have a struct which must be 16-byte aligned when targeting wasm.

How can I do this?

So for instance, I want to have this struct on wasm:

#[cfg(target_arch = "wasm32")] <- not sure about this
#[repr(c, align(16))]
struct Foo {...}

And this on all other architectures:

#[cfg(target_arch = "wasm32")] <- not sure about this
#[repr(c)]
struct Foo {...}

Is this possible to achieve with cfg? If so how?


Solution

  • Looks like its possible with cfg_attr:

    #[cfg_attr(not(target_arch = "wasm32"), repr(C))]
    #[cfg_attr(target_arch = "wasm32", repr(C, align(16)))]
    struct Foo { ... }