diff --git a/libs/deer/src/impls/core.rs b/libs/deer/src/impls/core.rs index 9c0952c85f6..75749330d69 100644 --- a/libs/deer/src/impls/core.rs +++ b/libs/deer/src/impls/core.rs @@ -1,5 +1,4 @@ mod array; -mod atomic; mod bool; mod cell; mod cmp; @@ -10,4 +9,5 @@ mod mem; mod non_zero; mod option; mod string; +mod sync; mod unit; diff --git a/libs/deer/src/impls/core/sync.rs b/libs/deer/src/impls/core/sync.rs new file mode 100644 index 00000000000..3a576338bf7 --- /dev/null +++ b/libs/deer/src/impls/core/sync.rs @@ -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 +where + T: Deserialize<'de>, +{ + type Reflection = T::Reflection; + + fn deserialize>(de: D) -> error_stack::Result { + T::deserialize(de).map(Self::new) + } +} diff --git a/libs/deer/src/impls/core/atomic.rs b/libs/deer/src/impls/core/sync/atomic.rs similarity index 100% rename from libs/deer/src/impls/core/atomic.rs rename to libs/deer/src/impls/core/sync/atomic.rs diff --git a/libs/deer/src/lib.rs b/libs/deer/src/lib.rs index 1ed5886f7a4..353ff4bd261 100644 --- a/libs/deer/src/lib.rs +++ b/libs/deer/src/lib.rs @@ -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)] diff --git a/libs/deer/tests/test_impls_core_sync.rs b/libs/deer/tests/test_impls_core_sync.rs new file mode 100644 index 00000000000..625b58f173c --- /dev/null +++ b/libs/deer/tests/test_impls_core_sync.rs @@ -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::()) { + assert_tokens_with_assertion(|mut received: Exclusive| { + 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::::reflection(); + let rhs = u8::reflection(); + + assert_json(lhs, rhs); +}