8
serde
をサポートする機能を追加しました。しかし、私はかなりそれを正しく使用する方法を理解していない: 条件付きでフィーチャで派生することはできますか?
// #[derive(Debug, Serialize, Deserialize, Clone)] // goes to:
#[derive(Debug, Clone)]
#[cfg(feature = "serde_support")]
#[derive(Serialize, Deserialize)]
pub struct MyStruct;
は現在、このコードはので、私のクレートも
MyStruct
を持っていない私の
serde_support
機能せず、
cfg(feature)
が条件付きコンパイルの下にすべてを扱います。
私は中括弧でそれをラップしようとしたが、それは別のエラーを与える:
コード:
#[derive(Debug, Clone)]
#[cfg(feature = "serde_support")] {
#[derive(Serialize, Deserialize)]
}
pub struct MyStruct;
がエラー:
error: expected item after attributes
--> mycrate/src/lib.rs:65:33
|
65 | #[cfg(feature = "serde_support")] {
| ^
これを行うには、どのように?
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct MyStruct;
それはRust reference about "conditional compilation"で説明しています:
を - それは、より良いドキュメントにさらされていないということ奇妙です。 – ljedrz