move the PhantomData usage to the Deserializer

This commit is contained in:
Oliver Schneider 2017-02-03 15:02:40 +01:00
parent 02e6c04e9f
commit 6277079152

View File

@ -18,15 +18,17 @@ use de::{
EnumVisitor,
};
use de::value::ValueDeserializer;
/// Used from generated code to buffer the contents of the Deserializer when
/// deserializing untagged enums and internally tagged enums.
///
/// Not public API. Use serde-value instead.
#[allow(missing_docs)]
#[derive(Debug)]
pub enum Content<E> {
pub enum Content {
// Don't mind the PhantomData, just need to use E somewhere.
Bool(bool, PhantomData<E>),
Bool(bool),
U8(u8),
U16(u16),
@ -46,36 +48,26 @@ pub enum Content<E> {
Bytes(Vec<u8>),
None,
Some(Box<Content<E>>),
Some(Box<Content>),
Unit,
Newtype(Box<Content<E>>),
Seq(Vec<Content<E>>),
Map(Vec<(Content<E>, Content<E>)>),
Newtype(Box<Content>),
Seq(Vec<Content>),
Map(Vec<(Content, Content)>),
}
impl<E> Deserialize for Content<E> {
impl Deserialize for Content {
fn deserialize<D: Deserializer>(deserializer: D) -> Result<Self, D::Error> {
// Untagged and internally tagged enums are only supported in
// self-describing formats.
deserializer.deserialize(ContentVisitor::new())
deserializer.deserialize(ContentVisitor)
}
}
struct ContentVisitor<E> {
err: PhantomData<E>,
}
struct ContentVisitor;
impl<E> ContentVisitor<E> {
fn new() -> Self {
ContentVisitor {
err: PhantomData,
}
}
}
impl<E> Visitor for ContentVisitor<E> {
type Value = Content<E>;
impl Visitor for ContentVisitor {
type Value = Content;
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("any value")
@ -84,7 +76,7 @@ impl<E> Visitor for ContentVisitor<E> {
fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
where F: de::Error
{
Ok(Content::Bool(value, PhantomData))
Ok(Content::Bool(value))
}
fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
@ -231,27 +223,25 @@ impl<E> Visitor for ContentVisitor<E> {
/// This is the type of the map keys in an internally tagged enum.
///
/// Not public API.
pub enum TagOrContent<E> {
pub enum TagOrContent {
Tag,
Content(Content<E>),
Content(Content),
}
struct TagOrContentVisitor<E> {
struct TagOrContentVisitor {
name: &'static str,
err: PhantomData<E>,
}
impl<E> TagOrContentVisitor<E> {
impl TagOrContentVisitor {
fn new(name: &'static str) -> Self {
TagOrContentVisitor {
name: name,
err: PhantomData,
}
}
}
impl<E> DeserializeSeed for TagOrContentVisitor<E> {
type Value = TagOrContent<E>;
impl DeserializeSeed for TagOrContentVisitor {
type Value = TagOrContent;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
@ -262,8 +252,8 @@ impl<E> DeserializeSeed for TagOrContentVisitor<E> {
}
}
impl<E> Visitor for TagOrContentVisitor<E> {
type Value = TagOrContent<E>;
impl Visitor for TagOrContentVisitor {
type Value = TagOrContent;
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "a type tag `{}` or any other value", self.name)
@ -272,73 +262,73 @@ impl<E> Visitor for TagOrContentVisitor<E> {
fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_bool(value).map(TagOrContent::Content)
ContentVisitor.visit_bool(value).map(TagOrContent::Content)
}
fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_i8(value).map(TagOrContent::Content)
ContentVisitor.visit_i8(value).map(TagOrContent::Content)
}
fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_i16(value).map(TagOrContent::Content)
ContentVisitor.visit_i16(value).map(TagOrContent::Content)
}
fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_i32(value).map(TagOrContent::Content)
ContentVisitor.visit_i32(value).map(TagOrContent::Content)
}
fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_i64(value).map(TagOrContent::Content)
ContentVisitor.visit_i64(value).map(TagOrContent::Content)
}
fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_u8(value).map(TagOrContent::Content)
ContentVisitor.visit_u8(value).map(TagOrContent::Content)
}
fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_u16(value).map(TagOrContent::Content)
ContentVisitor.visit_u16(value).map(TagOrContent::Content)
}
fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_u32(value).map(TagOrContent::Content)
ContentVisitor.visit_u32(value).map(TagOrContent::Content)
}
fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_u64(value).map(TagOrContent::Content)
ContentVisitor.visit_u64(value).map(TagOrContent::Content)
}
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_f32(value).map(TagOrContent::Content)
ContentVisitor.visit_f32(value).map(TagOrContent::Content)
}
fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_f64(value).map(TagOrContent::Content)
ContentVisitor.visit_f64(value).map(TagOrContent::Content)
}
fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_char(value).map(TagOrContent::Content)
ContentVisitor.visit_char(value).map(TagOrContent::Content)
}
fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
@ -347,7 +337,7 @@ impl<E> Visitor for TagOrContentVisitor<E> {
if value == self.name {
Ok(TagOrContent::Tag)
} else {
ContentVisitor::new().visit_str(value).map(TagOrContent::Content)
ContentVisitor.visit_str(value).map(TagOrContent::Content)
}
}
@ -357,7 +347,7 @@ impl<E> Visitor for TagOrContentVisitor<E> {
if value == self.name {
Ok(TagOrContent::Tag)
} else {
ContentVisitor::new().visit_string(value).map(TagOrContent::Content)
ContentVisitor.visit_string(value).map(TagOrContent::Content)
}
}
@ -367,7 +357,7 @@ impl<E> Visitor for TagOrContentVisitor<E> {
if value == self.name.as_bytes() {
Ok(TagOrContent::Tag)
} else {
ContentVisitor::new().visit_bytes(value).map(TagOrContent::Content)
ContentVisitor.visit_bytes(value).map(TagOrContent::Content)
}
}
@ -377,84 +367,82 @@ impl<E> Visitor for TagOrContentVisitor<E> {
if value == self.name.as_bytes() {
Ok(TagOrContent::Tag)
} else {
ContentVisitor::new().visit_byte_buf(value).map(TagOrContent::Content)
ContentVisitor.visit_byte_buf(value).map(TagOrContent::Content)
}
}
fn visit_unit<F>(self) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_unit().map(TagOrContent::Content)
ContentVisitor.visit_unit().map(TagOrContent::Content)
}
fn visit_none<F>(self) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_none().map(TagOrContent::Content)
ContentVisitor.visit_none().map(TagOrContent::Content)
}
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
{
ContentVisitor::new().visit_some(deserializer).map(TagOrContent::Content)
ContentVisitor.visit_some(deserializer).map(TagOrContent::Content)
}
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
{
ContentVisitor::new().visit_newtype_struct(deserializer).map(TagOrContent::Content)
ContentVisitor.visit_newtype_struct(deserializer).map(TagOrContent::Content)
}
fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: SeqVisitor
{
ContentVisitor::new().visit_seq(visitor).map(TagOrContent::Content)
ContentVisitor.visit_seq(visitor).map(TagOrContent::Content)
}
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: MapVisitor
{
ContentVisitor::new().visit_map(visitor).map(TagOrContent::Content)
ContentVisitor.visit_map(visitor).map(TagOrContent::Content)
}
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: EnumVisitor
{
ContentVisitor::new().visit_enum(visitor).map(TagOrContent::Content)
ContentVisitor.visit_enum(visitor).map(TagOrContent::Content)
}
}
/// Used by generated code to deserialize an internally tagged enum.
///
/// Not public API.
pub struct TaggedContent<T, E> {
pub struct TaggedContent<T> {
pub tag: T,
pub content: Content<E>,
pub content: Content,
}
/// Not public API.
pub struct TaggedContentVisitor<T, E> {
pub struct TaggedContentVisitor<T> {
tag_name: &'static str,
tag: PhantomData<T>,
err: PhantomData<E>,
}
impl<T, E> TaggedContentVisitor<T, E> {
impl<T> TaggedContentVisitor<T> {
/// Visitor for the content of an internally tagged enum with the given tag
/// name.
pub fn new(name: &'static str) -> Self {
TaggedContentVisitor {
tag_name: name,
tag: PhantomData,
err: PhantomData,
}
}
}
impl<T, E> DeserializeSeed for TaggedContentVisitor<T, E>
impl<T> DeserializeSeed for TaggedContentVisitor<T>
where T: Deserialize
{
type Value = TaggedContent<T, E>;
type Value = TaggedContent<T>;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
@ -465,10 +453,10 @@ impl<T, E> DeserializeSeed for TaggedContentVisitor<T, E>
}
}
impl<T, E> Visitor for TaggedContentVisitor<T, E>
impl<T> Visitor for TaggedContentVisitor<T>
where T: Deserialize
{
type Value = TaggedContent<T, E>;
type Value = TaggedContent<T>;
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("any value")
@ -507,9 +495,15 @@ impl<T, E> Visitor for TaggedContentVisitor<T, E>
}
}
/// Not public API
pub struct ContentDeserializer<E> {
content: Content,
err: PhantomData<E>,
}
/// Used when deserializing an internally tagged enum because the content will
/// be used exactly once.
impl<E> Deserializer for Content<E>
impl<E> Deserializer for ContentDeserializer<E>
where E: de::Error
{
type Error = E;
@ -517,8 +511,8 @@ impl<E> Deserializer for Content<E>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
match self {
Content::Bool(v, _) => visitor.visit_bool(v),
match self.content {
Content::Bool(v) => visitor.visit_bool(v),
Content::U8(v) => visitor.visit_u8(v),
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
@ -533,8 +527,8 @@ impl<E> Deserializer for Content<E>
Content::String(v) => visitor.visit_string(v),
Content::Unit => visitor.visit_unit(),
Content::None => visitor.visit_none(),
Content::Some(v) => visitor.visit_some(*v),
Content::Newtype(v) => visitor.visit_newtype_struct(*v),
Content::Some(v) => visitor.visit_some(v.into_deserializer()),
Content::Newtype(v) => visitor.visit_newtype_struct(v.into_deserializer()),
Content::Seq(v) => {
let seq = v.into_iter();
let mut seq_visitor = de::value::SeqDeserializer::new(seq);
@ -556,9 +550,9 @@ impl<E> Deserializer for Content<E>
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
match self {
match self.content {
Content::None => visitor.visit_none(),
Content::Some(v) => visitor.visit_some(*v),
Content::Some(v) => visitor.visit_some(v.into_deserializer()),
Content::Unit => visitor.visit_unit(),
_ => visitor.visit_some(self)
}
@ -577,19 +571,28 @@ impl<E> Deserializer for Content<E>
}
}
impl<E> de::value::ValueDeserializer<E> for Content<E>
impl<E> de::value::ValueDeserializer<E> for Content
where E: de::Error
{
type Deserializer = Self;
type Deserializer = ContentDeserializer<E>;
fn into_deserializer(self) -> Self {
self
fn into_deserializer(self) -> Self::Deserializer {
ContentDeserializer {
content: self,
err: PhantomData,
}
}
}
/// Not public API.
pub struct ContentRefDeserializer<'a, E> {
content: &'a Content,
err: PhantomData<E>,
}
/// Used when deserializing an untagged enum because the content may need to be
/// used more than once.
impl<'a, E> Deserializer for &'a Content<E>
impl<'a, E> Deserializer for ContentRefDeserializer<'a, E>
where E: de::Error
{
type Error = E;
@ -597,8 +600,8 @@ impl<'a, E> Deserializer for &'a Content<E>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
match *self {
Content::Bool(v, _) => visitor.visit_bool(v),
match *self.content {
Content::Bool(v) => visitor.visit_bool(v),
Content::U8(v) => visitor.visit_u8(v),
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
@ -613,8 +616,8 @@ impl<'a, E> Deserializer for &'a Content<E>
Content::String(ref v) => visitor.visit_str(v),
Content::Unit => visitor.visit_unit(),
Content::None => visitor.visit_none(),
Content::Some(ref v) => visitor.visit_some(&**v),
Content::Newtype(ref v) => visitor.visit_newtype_struct(&**v),
Content::Some(ref v) => visitor.visit_some((&**v).into_deserializer()),
Content::Newtype(ref v) => visitor.visit_newtype_struct((&**v).into_deserializer()),
Content::Seq(ref v) => {
let seq = v.into_iter();
let mut seq_visitor = de::value::SeqDeserializer::new(seq);
@ -636,9 +639,9 @@ impl<'a, E> Deserializer for &'a Content<E>
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
match *self {
match *self.content {
Content::None => visitor.visit_none(),
Content::Some(ref v) => visitor.visit_some(&**v),
Content::Some(ref v) => visitor.visit_some((&**v).into_deserializer()),
Content::Unit => visitor.visit_unit(),
_ => visitor.visit_some(self)
}
@ -657,13 +660,16 @@ impl<'a, E> Deserializer for &'a Content<E>
}
}
impl<'a, E> de::value::ValueDeserializer<E> for &'a Content<E>
impl<'a, E> de::value::ValueDeserializer<E> for &'a Content
where E: de::Error
{
type Deserializer = Self;
type Deserializer = ContentRefDeserializer<'a, E>;
fn into_deserializer(self) -> Self {
self
fn into_deserializer(self) -> Self::Deserializer {
ContentRefDeserializer {
content: self,
err: PhantomData,
}
}
}