Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deer: implement Deserialize for core::sync #2386

Merged
merged 14 commits into from
Apr 13, 2023
2 changes: 1 addition & 1 deletion libs/deer/src/impls/core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod array;
mod atomic;
mod bool;
mod cell;
mod cmp;
Expand All @@ -10,4 +9,5 @@ mod mem;
mod non_zero;
mod option;
mod string;
mod sync;
mod unit;
19 changes: 19 additions & 0 deletions libs/deer/src/impls/core/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[cfg(nightly)]
use core::sync::Exclusive;

#[cfg(nightly)]
use crate::{error::DeserializeError, Deserialize, Deserializer};

mod atomic;

#[cfg(nightly)]
impl<'de, T> Deserialize<'de> for Exclusive<T>
where
T: Deserialize<'de>,
{
type Reflection = T::Reflection;

fn deserialize<D: Deserializer<'de>>(de: D) -> error_stack::Result<Self, DeserializeError> {
T::deserialize(de).map(Self::new)
}
}
3 changes: 2 additions & 1 deletion libs/deer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
error_in_core,
error_generic_member_access,
integer_atomics,
sync_unsafe_cell
sync_unsafe_cell,
exclusive_wrapper
)
)]
#![cfg_attr(not(feature = "std"), no_std)]
Expand Down
36 changes: 36 additions & 0 deletions libs/deer/tests/test_impls_core_sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![feature(exclusive_wrapper)]
#![cfg(nightly)]

use core::sync::Exclusive;

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

#[cfg(not(miri))]
proptest! {
#[test]
fn exclusive_ok(value in any::<u8>()) {
assert_tokens_with_assertion(|mut received: Exclusive<u8>| {
assert_eq!(*received.get_mut(), value);
}, &[Token::Number(Number::from(value))]);
}
}

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 exclusive_reflection_same() {
let lhs = Exclusive::<u8>::reflection();
let rhs = u8::reflection();

assert_json(lhs, rhs);
}