Resolves #183 - Serialize and Deserialize for PhantomData

This commit is contained in:
David Rajchenbach-Teller 2016-02-23 21:43:50 +01:00
parent b6965ecde8
commit 4bba342e42
2 changed files with 40 additions and 0 deletions

View File

@ -315,6 +315,34 @@ impl<T> Deserialize for Option<T> where T: Deserialize {
///////////////////////////////////////////////////////////////////////////////
/// A visitor that produces a `PhantomData`.
pub struct PhantomDataVisitor<T> {
marker: PhantomData<T>,
}
impl<
T: Deserialize,
> Visitor for PhantomDataVisitor<T> {
type Value = PhantomData<T>;
#[inline]
fn visit_unit<E>(&mut self) -> Result<PhantomData<T>, E>
where E: Error,
{
Ok(PhantomData)
}
}
impl<T> Deserialize for PhantomData<T> where T: Deserialize {
fn deserialize<D>(deserializer: &mut D) -> Result<PhantomData<T>, D::Error>
where D: Deserializer,
{
deserializer.visit_unit(PhantomDataVisitor {marker: PhantomData})
}
}
///////////////////////////////////////////////////////////////////////////////
macro_rules! seq_impl {
(
$ty:ty,

View File

@ -22,6 +22,7 @@ use std::ops;
use std::path;
use std::rc::Rc;
use std::sync::Arc;
use std::marker::PhantomData;
#[cfg(feature = "nightly")]
use core::nonzero::{NonZero, Zeroable};
@ -119,6 +120,17 @@ impl<T> SeqVisitor for Option<T> where T: Serialize {
///////////////////////////////////////////////////////////////////////////////
impl<T> Serialize for PhantomData<T> {
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
serializer.visit_unit()
}
}
///////////////////////////////////////////////////////////////////////////////
/// A `serde::Visitor` for sequence iterators.
///
/// # Examples