Skip to content

Commit

Permalink
deer: implement Deserialize for core::mem (#2384)
Browse files Browse the repository at this point in the history
* feat: move from #1875

* test: `ManuallyDrop`

* fix: `Reflection`

* chore: remove unused imports

* fix: miri
  • Loading branch information
indietyp authored Apr 13, 2023
1 parent 833d95f commit f66d077
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/deer/src/impls/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod bool;
mod cmp;
mod floating;
mod integral;
mod mem;
mod non_zero;
mod string;
mod unit;
13 changes: 13 additions & 0 deletions libs/deer/src/impls/core/mem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use core::mem::ManuallyDrop;

use error_stack::Result;

use crate::{error::DeserializeError, Deserialize, Deserializer};

impl<'de, T: Deserialize<'de>> Deserialize<'de> for ManuallyDrop<T> {
type Reflection = T::Reflection;

fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, DeserializeError> {
T::deserialize(de).map(Self::new)
}
}
31 changes: 31 additions & 0 deletions libs/deer/tests/test_impls_core_mem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use core::mem::ManuallyDrop;

use deer::Deserialize;
use deer_desert::{assert_tokens, Token};
use proptest::prelude::*;
use serde::Serialize;
use similar_asserts::assert_serde_eq;

#[cfg(not(miri))]
proptest! {
#[test]
fn manually_drop_ok(value in any::<u8>()) {
assert_tokens(&ManuallyDrop::new(value), &[Token::Number(value.into())]);
}
}

fn assert_json(lhs: impl Serialize, rhs: impl Serialize) {
let lhs = serde_json::to_value(lhs).expect("should be able to serialize lhs");
let rhs = serde_json::to_value(rhs).expect("should be able to serialize rhs");

assert_serde_eq!(lhs, rhs);
}

// test that the `Reflection` of all types are the same as their underlying type
#[test]
fn manually_drop_reflection_same() {
let lhs = ManuallyDrop::<u8>::reflection();
let rhs = u8::reflection();

assert_json(lhs, rhs);
}

0 comments on commit f66d077

Please sign in to comment.