v1.0.25
-
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
andVec
. Enable thedeserialize_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); }