如何使用cfg在不同的目标架构上设置不同的属性?

问题描述 投票:0回答:1

我有一个结构,在定位

wasm
时必须是 16 字节对齐。

我该怎么做?

例如,我想在

wasm
上使用这个结构:

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

在所有其他架构上也是如此:

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

用cfg可以实现吗?如果是的话怎么办?

rust conditional-compilation
1个回答
2
投票

看起来可以用

cfg_attr
:

#[cfg_attr(not(target_arch = "wasm32"), repr(C))]
#[cfg_attr(target_arch = "wasm32", repr(C, align(16)))]
struct Foo { ... }
© www.soinside.com 2019 - 2024. All rights reserved.