rustserdeconditional-compilation

How do I choose a module for serialization and deserialization based on platform?


Is there any way to add a platform based condition on #[serde(with = "path_handling")]? So, basically I want to use this custome Serde method only in Inix and on Windows I want to use default way.

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,
}

Solution

  • Using cfg_attr and target_family.

    pub struct Res {
        pub last: bool,
        #[cfg_attr(target_family = "unix", serde(with = "path_handling"))]
        pub path: PathBuf,
    }