Skip to content

v1.0.97

Compare
Choose a tag to compare
@dtolnay dtolnay released this 17 Jul 22:10
· 1106 commits to master since this release
v1.0.97
3ae2bee
  • Introduce serde(try_from = "...") attribute to derive Deserialize based on a given implementation of std::convert::TryFrom (#1526, thanks @fanzeyi)

    #[derive(Deserialize)]
    #[serde(try_from = "u32")]
    enum N {
        Zero,
        One,
        Two,
    }
    
    impl TryFrom<u32> for N {
        type Error = String;
    
        fn try_from(u: u32) -> Result<Self, Self::Error> {
            match u {
                0 => Ok(Self::Zero),
                1 => Ok(Self::One),
                2 => Ok(Self::Two),
                other => Err(format!("out of range: {}", other)),
            }
        }
    }