如何根据平台选择序列化和反序列化模块?

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

有没有办法在

#[serde(with = "path_handling")]
上添加基于平台的条件? 所以,基本上我只想在 Inix 中使用这个自定义 Serde 方法,在 Windows 上我想使用默认方式。

pub struct Res {
    pub last: bool,
    #[serde(with = "path_handling")] // ignore this line on windows as path_handling module contains unix specific logic
    pub path: PathBuf,
}
rust serde conditional-compilation
1个回答
3
投票

使用

cfg_attr
target_family

pub struct Res {
    pub last: bool,
    #[cfg_attr(target_family = "unix", serde(with = "path_handling"))]
    pub path: PathBuf,
}
© www.soinside.com 2019 - 2024. All rights reserved.