-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: convert from #1875 * test: `PhantomData`
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ mod bool; | |
mod cmp; | ||
mod floating; | ||
mod integral; | ||
mod marker; | ||
mod mem; | ||
mod non_zero; | ||
mod option; | ||
|
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,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) | ||
} | ||
} |
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,8 @@ | ||
use core::marker::PhantomData; | ||
|
||
use deer_desert::{assert_tokens, Token}; | ||
|
||
#[test] | ||
fn phantom_data_ok() { | ||
assert_tokens::<PhantomData<u64>>(&PhantomData, &[Token::Null]); | ||
} |