我在代码中定义了几个结构,如果在机箱上启用了某个功能,我也希望为这些结构生成Python绑定。现在我不能正确地得到它。假设我有一个结构MyStruct,我想为其生成可选的Python。
我试过以下几种方法
cfg_if! {
if #[cfg(feature = "python-bindings")] {
#[pyclass]
}
else {
}
}
struct MyStruct{
value: i32
}我只想在启用了#[pyclass] ( feature python-bindings )而不是其他情况下添加python-bindings。
如果没有启用python-bindings,这很好。但是,如果我使用--features python-bindings进行编译,则会得到以下错误。
error: expected item after attributes
我尽量不想重复代码。喜欢
cfg_if! {
if #[cfg(feature = "python-bindings")] {
#[pyclass]
struct MyStruct{
value: i32
}
}
else {
struct MyStruct{
value: i32
}
}
}有没有一种不重复代码的方法?
发布于 2022-05-19 04:44:34
是的,用#[cfg_attr]
#[cfg_attr(feature = "python-bindings", pyclass)]
struct MyStruct {
value: i32
}https://stackoverflow.com/questions/72298740
复制相似问题