-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: init cell * feat: pull in code from #1875 * feat: test cells * test: unsafe variants * test: reflection passthrough * fix: lint * Update test_impls_core_cell.rs
- Loading branch information
Showing
6 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod array; | ||
mod atomic; | ||
mod bool; | ||
mod cell; | ||
mod cmp; | ||
mod floating; | ||
mod integral; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use core::cell::{Cell, RefCell, UnsafeCell}; | ||
#[cfg(nightly)] | ||
use core::cell::{OnceCell, SyncUnsafeCell}; | ||
|
||
use crate::{error::DeserializeError, Deserialize, Deserializer, Document, Reflection, Schema}; | ||
|
||
macro_rules! impl_cell { | ||
($(#[$attr:meta])* $cell:ident) => { | ||
$(#[$attr])* | ||
impl<T> Reflection for $cell<T> | ||
where | ||
T: Reflection, | ||
{ | ||
fn schema(doc: &mut Document) -> Schema { | ||
T::schema(doc) | ||
} | ||
} | ||
|
||
$(#[$attr])* | ||
impl<'de, T> Deserialize<'de> for $cell<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::from) | ||
} | ||
} | ||
}; | ||
|
||
($($(#[$attr:meta])* $cell:ident),+ $(,)?) => { | ||
$(impl_cell!($(#[$attr])* $cell);)* | ||
}; | ||
} | ||
|
||
impl_cell![ | ||
#[cfg(nightly)] | ||
OnceCell, | ||
#[cfg(nightly)] | ||
SyncUnsafeCell, | ||
Cell, | ||
RefCell, | ||
UnsafeCell | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#![cfg_attr(nightly, feature(sync_unsafe_cell))] | ||
use core::cell::{Cell, RefCell, UnsafeCell}; | ||
#[cfg(nightly)] | ||
use core::cell::{OnceCell, SyncUnsafeCell}; | ||
|
||
use deer::{Deserialize, Number}; | ||
use deer_desert::{assert_tokens, assert_tokens_with_assertion, Token}; | ||
use proptest::prelude::*; | ||
use serde::Serialize; | ||
use similar_asserts::assert_serde_eq; | ||
|
||
#[cfg(not(miri))] | ||
proptest! { | ||
#[test] | ||
fn cell_ok(value in any::<u8>()) { | ||
let expected = Cell::new(value); | ||
|
||
assert_tokens(&expected, &[Token::Number(Number::from(value))]); | ||
} | ||
|
||
#[test] | ||
fn ref_cell_ok(value in any::<u8>()) { | ||
let expected = RefCell::new(value); | ||
|
||
assert_tokens(&expected, &[Token::Number(Number::from(value))]); | ||
} | ||
|
||
#[test] | ||
fn unsafe_cell_ok(value in any::<u8>()) { | ||
// need to use the underlying function, because `UnsafeCell` does not expose PartialEq | ||
assert_tokens_with_assertion(|mut received: UnsafeCell<u8>| { | ||
assert_eq!(*received.get_mut(), value); | ||
}, &[Token::Number( | ||
Number::from(value), | ||
)]); | ||
} | ||
|
||
|
||
#[cfg(nightly)] | ||
#[test] | ||
fn once_cell_ok(value in any::<u8>()) { | ||
let expected: OnceCell<_> = value.into(); | ||
|
||
assert_tokens(&expected, &[Token::Number(Number::from(value))]); | ||
} | ||
|
||
#[cfg(nightly)] | ||
#[test] | ||
fn sync_unsafe_cell_ok(value in any::<u8>()) { | ||
// need to use the underlying function, because `SyncUnsafeCell` does not expose PartialEq | ||
assert_tokens_with_assertion(|mut received: SyncUnsafeCell<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 cell_reflection_same() { | ||
let lhs = Cell::<u8>::reflection(); | ||
let rhs = u8::reflection(); | ||
|
||
assert_json(lhs, rhs); | ||
} | ||
|
||
#[test] | ||
fn ref_cell_reflection_same() { | ||
let lhs = RefCell::<u8>::reflection(); | ||
let rhs = u8::reflection(); | ||
|
||
assert_json(lhs, rhs); | ||
} | ||
|
||
#[test] | ||
fn unsafe_cell_reflection_same() { | ||
let lhs = UnsafeCell::<u8>::reflection(); | ||
let rhs = u8::reflection(); | ||
|
||
assert_json(lhs, rhs); | ||
} | ||
|
||
#[cfg(nightly)] | ||
#[test] | ||
fn once_cell_reflection_same() { | ||
let lhs = OnceCell::<u8>::reflection(); | ||
let rhs = u8::reflection(); | ||
|
||
assert_json(lhs, rhs); | ||
} | ||
|
||
#[cfg(nightly)] | ||
#[test] | ||
fn sync_unsafe_cell_reflection_same() { | ||
let lhs = SyncUnsafeCell::<u8>::reflection(); | ||
let rhs = u8::reflection(); | ||
|
||
assert_json(lhs, rhs); | ||
} |