From 41c213a618d4de47c76ed477b2949d738a595d9f Mon Sep 17 00:00:00 2001 From: Bilal Mahmoud Date: Sun, 9 Apr 2023 18:51:38 +0200 Subject: [PATCH 1/2] chore: convert from #1875 --- libs/deer/src/impls/core.rs | 1 + libs/deer/src/impls/core/marker.rs | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 libs/deer/src/impls/core/marker.rs diff --git a/libs/deer/src/impls/core.rs b/libs/deer/src/impls/core.rs index 4e08ee1bf35..538ffa6c6d1 100644 --- a/libs/deer/src/impls/core.rs +++ b/libs/deer/src/impls/core.rs @@ -3,6 +3,7 @@ mod atomic; mod bool; mod floating; mod integral; +mod marker; mod non_zero; mod string; mod unit; diff --git a/libs/deer/src/impls/core/marker.rs b/libs/deer/src/impls/core/marker.rs new file mode 100644 index 00000000000..24abd0a6129 --- /dev/null +++ b/libs/deer/src/impls/core/marker.rs @@ -0,0 +1,45 @@ +use core::marker::PhantomData; + +use error_stack::{Result, ResultExt}; + +use crate::{ + error::{DeserializeError, VisitorError}, + Deserialize, Deserializer, Document, Reflection, Schema, Visitor, +}; + +struct PhantomDataVisitor(PhantomData); + +impl<'de, T: ?Sized> Visitor<'de> for PhantomDataVisitor { + type Value = PhantomData; + + fn expecting(&self) -> Document { + Self::Value::reflection() + } + + fn visit_none(self) -> Result { + Ok(PhantomData) + } + + fn visit_null(self) -> Result { + Ok(PhantomData) + } +} + +pub struct PhantomDataReflection; + +impl Reflection for PhantomDataReflection { + fn schema(_: &mut Document) -> Schema { + // TODO: this is also optional (none) + // currently we're unable to express that constraint (something for 0.2) + Schema::new("null") + } +} + +impl<'de, T: ?Sized> Deserialize<'de> for PhantomData { + type Reflection = PhantomDataReflection; + + fn deserialize>(de: D) -> Result { + de.deserialize_null(PhantomDataVisitor(PhantomData)) + .change_context(DeserializeError) + } +} From a871ffeee6930b338978b030e9a785093c215e37 Mon Sep 17 00:00:00 2001 From: Bilal Mahmoud Date: Sun, 9 Apr 2023 18:56:58 +0200 Subject: [PATCH 2/2] test: `PhantomData` --- libs/deer/src/impls/core/marker.rs | 2 +- libs/deer/tests/test_impls_core_marker.rs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 libs/deer/tests/test_impls_core_marker.rs diff --git a/libs/deer/src/impls/core/marker.rs b/libs/deer/src/impls/core/marker.rs index 24abd0a6129..ebd68f9f72b 100644 --- a/libs/deer/src/impls/core/marker.rs +++ b/libs/deer/src/impls/core/marker.rs @@ -39,7 +39,7 @@ impl<'de, T: ?Sized> Deserialize<'de> for PhantomData { type Reflection = PhantomDataReflection; fn deserialize>(de: D) -> Result { - de.deserialize_null(PhantomDataVisitor(PhantomData)) + de.deserialize_null(PhantomDataVisitor(Self)) .change_context(DeserializeError) } } diff --git a/libs/deer/tests/test_impls_core_marker.rs b/libs/deer/tests/test_impls_core_marker.rs new file mode 100644 index 00000000000..fb1c8076c1e --- /dev/null +++ b/libs/deer/tests/test_impls_core_marker.rs @@ -0,0 +1,8 @@ +use core::marker::PhantomData; + +use deer_desert::{assert_tokens, Token}; + +#[test] +fn phantom_data_ok() { + assert_tokens::>(&PhantomData, &[Token::Null]); +}