Skip to content

Commit

Permalink
Merge pull request #813 from dimforge/deserialize_uninit
Browse files Browse the repository at this point in the history
Add workaround for the deserialization of a matrix containing an enum.
  • Loading branch information
sebcrozet committed Dec 18, 2020
2 parents 8bc2773 + b15a274 commit 8c61528
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/base/array_storage.rs
Expand Up @@ -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()? {
Expand Down
5 changes: 2 additions & 3 deletions src/base/conversion.rs
Expand Up @@ -257,9 +257,8 @@ macro_rules! impl_from_into_mint_1D(
#[inline]
fn into(self) -> mint::$VT<N> {
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);

res
}
}
Expand Down Expand Up @@ -324,7 +323,7 @@ macro_rules! impl_from_into_mint_2D(
#[inline]
fn into(self) -> mint::$MV<N> {
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();
$(
ptr::copy_nonoverlapping(ptr, &mut res.$component.x, $SZRows);
Expand Down
16 changes: 15 additions & 1 deletion tests/core/serde.rs
Expand Up @@ -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(
Expand Down Expand Up @@ -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<Vector2<Stuff>, _> = serde_json::from_str(json);
println!("parsed: {:?}", parsed);
}

0 comments on commit 8c61528

Please sign in to comment.