Merge pull request #813 from dimforge/deserialize_uninit

Add workaround for the deserialization of a matrix containing an enum.
This commit is contained in:
Sébastien Crozet 2020-12-18 12:06:39 +01:00 committed by GitHub
commit 8c615289dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -377,7 +377,7 @@ where
where where
V: SeqAccess<'a>, 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; let mut curr = 0;
while let Some(value) = visitor.next_element()? { while let Some(value) = visitor.next_element()? {

View File

@ -257,9 +257,8 @@ macro_rules! impl_from_into_mint_1D(
#[inline] #[inline]
fn into(self) -> mint::$VT<N> { fn into(self) -> mint::$VT<N> {
unsafe { unsafe {
let mut res: mint::$VT<N> = mem::uninitialized(); let mut res: mint::$VT<N> = mem::MaybeUninit::uninit().assume_init();
ptr::copy_nonoverlapping(self.data.ptr(), &mut res.x, $SZ); ptr::copy_nonoverlapping(self.data.ptr(), &mut res.x, $SZ);
res res
} }
} }
@ -324,7 +323,7 @@ macro_rules! impl_from_into_mint_2D(
#[inline] #[inline]
fn into(self) -> mint::$MV<N> { fn into(self) -> mint::$MV<N> {
unsafe { unsafe {
let mut res: mint::$MV<N> = mem::uninitialized(); let mut res: mint::$MV<N> = mem::MaybeUninit::uninit().assume_init();
let mut ptr = self.data.ptr(); let mut ptr = self.data.ptr();
$( $(
ptr::copy_nonoverlapping(ptr, &mut res.$component.x, $SZRows); ptr::copy_nonoverlapping(ptr, &mut res.$component.x, $SZRows);

View File

@ -3,9 +3,10 @@
use na::{ use na::{
DMatrix, Isometry2, Isometry3, IsometryMatrix2, IsometryMatrix3, Matrix3x4, Point2, Point3, DMatrix, Isometry2, Isometry3, IsometryMatrix2, IsometryMatrix3, Matrix3x4, Point2, Point3,
Quaternion, Rotation2, Rotation3, Similarity2, Similarity3, SimilarityMatrix2, Quaternion, Rotation2, Rotation3, Similarity2, Similarity3, SimilarityMatrix2,
SimilarityMatrix3, Translation2, Translation3, Unit, SimilarityMatrix3, Translation2, Translation3, Unit, Vector2,
}; };
use rand; use rand;
use serde::{Deserialize, Serialize};
use serde_json; use serde_json;
macro_rules! test_serde( macro_rules! test_serde(
@ -54,3 +55,16 @@ fn serde_flat() {
let serialized = serde_json::to_string(&v).unwrap(); let serialized = serde_json::to_string(&v).unwrap();
assert_eq!(serialized, "[0.0,0.0,1.0,0.0]"); 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<Vector2<Stuff>, _> = serde_json::from_str(json);
println!("parsed: {:?}", parsed);
}