From b15a274a14c401f75fdf15bb38c0c3ba8b4c8a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Fri, 18 Dec 2020 11:54:56 +0100 Subject: [PATCH] Add workaround for the deserialization of a matrix containing an enum. --- src/base/array_storage.rs | 2 +- src/base/conversion.rs | 5 ++--- tests/core/serde.rs | 16 +++++++++++++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index b86e3a80..4fdd3e73 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -377,7 +377,7 @@ where where V: SeqAccess<'a>, { - let mut out: Self::Value = unsafe { mem::uninitialized() }; + let mut out: Self::Value = unsafe { mem::MaybeUninit::uninit().assume_init() }; let mut curr = 0; while let Some(value) = visitor.next_element()? { diff --git a/src/base/conversion.rs b/src/base/conversion.rs index 583632a8..77bf4005 100644 --- a/src/base/conversion.rs +++ b/src/base/conversion.rs @@ -257,9 +257,8 @@ macro_rules! impl_from_into_mint_1D( #[inline] fn into(self) -> mint::$VT { unsafe { - let mut res: mint::$VT = mem::uninitialized(); + let mut res: mint::$VT = mem::MaybeUninit::uninit().assume_init(); ptr::copy_nonoverlapping(self.data.ptr(), &mut res.x, $SZ); - res } } @@ -324,7 +323,7 @@ macro_rules! impl_from_into_mint_2D( #[inline] fn into(self) -> mint::$MV { unsafe { - let mut res: mint::$MV = mem::uninitialized(); + let mut res: mint::$MV = mem::MaybeUninit::uninit().assume_init(); let mut ptr = self.data.ptr(); $( ptr::copy_nonoverlapping(ptr, &mut res.$component.x, $SZRows); diff --git a/tests/core/serde.rs b/tests/core/serde.rs index aced2a9f..0a3be7e9 100644 --- a/tests/core/serde.rs +++ b/tests/core/serde.rs @@ -3,9 +3,10 @@ use na::{ DMatrix, Isometry2, Isometry3, IsometryMatrix2, IsometryMatrix3, Matrix3x4, Point2, Point3, Quaternion, Rotation2, Rotation3, Similarity2, Similarity3, SimilarityMatrix2, - SimilarityMatrix3, Translation2, Translation3, Unit, + SimilarityMatrix3, Translation2, Translation3, Unit, Vector2, }; use rand; +use serde::{Deserialize, Serialize}; use serde_json; macro_rules! test_serde( @@ -54,3 +55,16 @@ fn serde_flat() { let serialized = serde_json::to_string(&v).unwrap(); assert_eq!(serialized, "[0.0,0.0,1.0,0.0]"); } + +#[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone)] +enum Stuff { + A(f64), + B(f64), +} + +#[test] +fn deserialize_enum() { + let json = r#"[{"letter":"A", "value":123.4}, {"letter":"B", "value":567.8}]"#; + let parsed: Result, _> = serde_json::from_str(json); + println!("parsed: {:?}", parsed); +}