Skip to content

v1.0.25

Compare
Choose a tag to compare
@dtolnay dtolnay released this 24 Dec 04:50
· 1971 commits to master since this release
v1.0.25
ae59c6b
  • Add the following function with default implementation to the Deserialize trait (thanks @gankro!)

    fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
    where
        D: Deserializer<'de>,
    {
        *place = Deserialize::deserialize(deserializer)?;
        Ok(())
    }

    This is a power user feature that allows deserialization to reuse existing allocations for types like String and Vec. Enable the deserialize_in_place feature of serde_derive to generate these efficient implementations for all types that use #[derive(Deserialize)].

    extern crate serde;
    extern crate serde_json;
    
    use serde::Deserialize;
    
    fn main() {
        let mut v = Vec::<i32>::with_capacity(128);
        assert_eq!(v.capacity(), 128);
    
        let j = "[1, 2, 3]";
        let ref mut de = serde_json::Deserializer::from_str(j);
        Deserialize::deserialize_in_place(de, &mut v).unwrap();
        assert_eq!(v, [1, 2, 3]);
        assert_eq!(v.capacity(), 128);
    }