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::marker #2379

Merged
merged 3 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 marker;
mod mem;
mod non_zero;
mod option;
Expand Down
45 changes: 45 additions & 0 deletions libs/deer/src/impls/core/marker.rs
Original file line number Diff line number Diff line change
@@ -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<T: ?Sized>(PhantomData<T>);

impl<'de, T: ?Sized> Visitor<'de> for PhantomDataVisitor<T> {
type Value = PhantomData<T>;

fn expecting(&self) -> Document {
Self::Value::reflection()
}

fn visit_none(self) -> Result<Self::Value, VisitorError> {
Ok(PhantomData)
}

fn visit_null(self) -> Result<Self::Value, VisitorError> {
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<T> {
type Reflection = PhantomDataReflection;

fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, DeserializeError> {
de.deserialize_null(PhantomDataVisitor(Self))
.change_context(DeserializeError)
}
}
8 changes: 8 additions & 0 deletions libs/deer/tests/test_impls_core_marker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use core::marker::PhantomData;

use deer_desert::{assert_tokens, Token};

#[test]
fn phantom_data_ok() {
assert_tokens::<PhantomData<u64>>(&PhantomData, &[Token::Null]);
}