move the PhantomData usage to the Deserializer
This commit is contained in:
parent
02e6c04e9f
commit
6277079152
@ -18,15 +18,17 @@ use de::{
|
|||||||
EnumVisitor,
|
EnumVisitor,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use de::value::ValueDeserializer;
|
||||||
|
|
||||||
/// Used from generated code to buffer the contents of the Deserializer when
|
/// Used from generated code to buffer the contents of the Deserializer when
|
||||||
/// deserializing untagged enums and internally tagged enums.
|
/// deserializing untagged enums and internally tagged enums.
|
||||||
///
|
///
|
||||||
/// Not public API. Use serde-value instead.
|
/// Not public API. Use serde-value instead.
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Content<E> {
|
pub enum Content {
|
||||||
// Don't mind the PhantomData, just need to use E somewhere.
|
// Don't mind the PhantomData, just need to use E somewhere.
|
||||||
Bool(bool, PhantomData<E>),
|
Bool(bool),
|
||||||
|
|
||||||
U8(u8),
|
U8(u8),
|
||||||
U16(u16),
|
U16(u16),
|
||||||
@ -46,36 +48,26 @@ pub enum Content<E> {
|
|||||||
Bytes(Vec<u8>),
|
Bytes(Vec<u8>),
|
||||||
|
|
||||||
None,
|
None,
|
||||||
Some(Box<Content<E>>),
|
Some(Box<Content>),
|
||||||
|
|
||||||
Unit,
|
Unit,
|
||||||
Newtype(Box<Content<E>>),
|
Newtype(Box<Content>),
|
||||||
Seq(Vec<Content<E>>),
|
Seq(Vec<Content>),
|
||||||
Map(Vec<(Content<E>, Content<E>)>),
|
Map(Vec<(Content, Content)>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E> Deserialize for Content<E> {
|
impl Deserialize for Content {
|
||||||
fn deserialize<D: Deserializer>(deserializer: D) -> Result<Self, D::Error> {
|
fn deserialize<D: Deserializer>(deserializer: D) -> Result<Self, D::Error> {
|
||||||
// Untagged and internally tagged enums are only supported in
|
// Untagged and internally tagged enums are only supported in
|
||||||
// self-describing formats.
|
// self-describing formats.
|
||||||
deserializer.deserialize(ContentVisitor::new())
|
deserializer.deserialize(ContentVisitor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ContentVisitor<E> {
|
struct ContentVisitor;
|
||||||
err: PhantomData<E>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<E> ContentVisitor<E> {
|
impl Visitor for ContentVisitor {
|
||||||
fn new() -> Self {
|
type Value = Content;
|
||||||
ContentVisitor {
|
|
||||||
err: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<E> Visitor for ContentVisitor<E> {
|
|
||||||
type Value = Content<E>;
|
|
||||||
|
|
||||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.write_str("any value")
|
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>
|
fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
where F: de::Error
|
||||||
{
|
{
|
||||||
Ok(Content::Bool(value, PhantomData))
|
Ok(Content::Bool(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
|
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.
|
/// This is the type of the map keys in an internally tagged enum.
|
||||||
///
|
///
|
||||||
/// Not public API.
|
/// Not public API.
|
||||||
pub enum TagOrContent<E> {
|
pub enum TagOrContent {
|
||||||
Tag,
|
Tag,
|
||||||
Content(Content<E>),
|
Content(Content),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TagOrContentVisitor<E> {
|
struct TagOrContentVisitor {
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
err: PhantomData<E>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E> TagOrContentVisitor<E> {
|
impl TagOrContentVisitor {
|
||||||
fn new(name: &'static str) -> Self {
|
fn new(name: &'static str) -> Self {
|
||||||
TagOrContentVisitor {
|
TagOrContentVisitor {
|
||||||
name: name,
|
name: name,
|
||||||
err: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E> DeserializeSeed for TagOrContentVisitor<E> {
|
impl DeserializeSeed for TagOrContentVisitor {
|
||||||
type Value = TagOrContent<E>;
|
type Value = TagOrContent;
|
||||||
|
|
||||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||||
where D: Deserializer
|
where D: Deserializer
|
||||||
@ -262,8 +252,8 @@ impl<E> DeserializeSeed for TagOrContentVisitor<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E> Visitor for TagOrContentVisitor<E> {
|
impl Visitor for TagOrContentVisitor {
|
||||||
type Value = TagOrContent<E>;
|
type Value = TagOrContent;
|
||||||
|
|
||||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(fmt, "a type tag `{}` or any other value", self.name)
|
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>
|
fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
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 {
|
if value == self.name {
|
||||||
Ok(TagOrContent::Tag)
|
Ok(TagOrContent::Tag)
|
||||||
} else {
|
} 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 {
|
if value == self.name {
|
||||||
Ok(TagOrContent::Tag)
|
Ok(TagOrContent::Tag)
|
||||||
} else {
|
} 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() {
|
if value == self.name.as_bytes() {
|
||||||
Ok(TagOrContent::Tag)
|
Ok(TagOrContent::Tag)
|
||||||
} else {
|
} 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() {
|
if value == self.name.as_bytes() {
|
||||||
Ok(TagOrContent::Tag)
|
Ok(TagOrContent::Tag)
|
||||||
} else {
|
} 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>
|
fn visit_unit<F>(self) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_none<F>(self) -> Result<Self::Value, F>
|
||||||
where F: de::Error
|
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>
|
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||||
where D: Deserializer
|
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>
|
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||||
where D: Deserializer
|
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>
|
fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
||||||
where V: SeqVisitor
|
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>
|
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
||||||
where V: MapVisitor
|
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>
|
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
|
||||||
where V: EnumVisitor
|
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.
|
/// Used by generated code to deserialize an internally tagged enum.
|
||||||
///
|
///
|
||||||
/// Not public API.
|
/// Not public API.
|
||||||
pub struct TaggedContent<T, E> {
|
pub struct TaggedContent<T> {
|
||||||
pub tag: T,
|
pub tag: T,
|
||||||
pub content: Content<E>,
|
pub content: Content,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Not public API.
|
/// Not public API.
|
||||||
pub struct TaggedContentVisitor<T, E> {
|
pub struct TaggedContentVisitor<T> {
|
||||||
tag_name: &'static str,
|
tag_name: &'static str,
|
||||||
tag: PhantomData<T>,
|
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
|
/// Visitor for the content of an internally tagged enum with the given tag
|
||||||
/// name.
|
/// name.
|
||||||
pub fn new(name: &'static str) -> Self {
|
pub fn new(name: &'static str) -> Self {
|
||||||
TaggedContentVisitor {
|
TaggedContentVisitor {
|
||||||
tag_name: name,
|
tag_name: name,
|
||||||
tag: PhantomData,
|
tag: PhantomData,
|
||||||
err: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, E> DeserializeSeed for TaggedContentVisitor<T, E>
|
impl<T> DeserializeSeed for TaggedContentVisitor<T>
|
||||||
where T: Deserialize
|
where T: Deserialize
|
||||||
{
|
{
|
||||||
type Value = TaggedContent<T, E>;
|
type Value = TaggedContent<T>;
|
||||||
|
|
||||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||||
where D: Deserializer
|
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
|
where T: Deserialize
|
||||||
{
|
{
|
||||||
type Value = TaggedContent<T, E>;
|
type Value = TaggedContent<T>;
|
||||||
|
|
||||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.write_str("any value")
|
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
|
/// Used when deserializing an internally tagged enum because the content will
|
||||||
/// be used exactly once.
|
/// be used exactly once.
|
||||||
impl<E> Deserializer for Content<E>
|
impl<E> Deserializer for ContentDeserializer<E>
|
||||||
where E: de::Error
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
@ -517,8 +511,8 @@ impl<E> Deserializer for Content<E>
|
|||||||
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match self {
|
match self.content {
|
||||||
Content::Bool(v, _) => visitor.visit_bool(v),
|
Content::Bool(v) => visitor.visit_bool(v),
|
||||||
Content::U8(v) => visitor.visit_u8(v),
|
Content::U8(v) => visitor.visit_u8(v),
|
||||||
Content::U16(v) => visitor.visit_u16(v),
|
Content::U16(v) => visitor.visit_u16(v),
|
||||||
Content::U32(v) => visitor.visit_u32(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::String(v) => visitor.visit_string(v),
|
||||||
Content::Unit => visitor.visit_unit(),
|
Content::Unit => visitor.visit_unit(),
|
||||||
Content::None => visitor.visit_none(),
|
Content::None => visitor.visit_none(),
|
||||||
Content::Some(v) => visitor.visit_some(*v),
|
Content::Some(v) => visitor.visit_some(v.into_deserializer()),
|
||||||
Content::Newtype(v) => visitor.visit_newtype_struct(*v),
|
Content::Newtype(v) => visitor.visit_newtype_struct(v.into_deserializer()),
|
||||||
Content::Seq(v) => {
|
Content::Seq(v) => {
|
||||||
let seq = v.into_iter();
|
let seq = v.into_iter();
|
||||||
let mut seq_visitor = de::value::SeqDeserializer::new(seq);
|
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>
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match self {
|
match self.content {
|
||||||
Content::None => visitor.visit_none(),
|
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(),
|
Content::Unit => visitor.visit_unit(),
|
||||||
_ => visitor.visit_some(self)
|
_ => 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
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = Self;
|
type Deserializer = ContentDeserializer<E>;
|
||||||
|
|
||||||
fn into_deserializer(self) -> Self {
|
fn into_deserializer(self) -> Self::Deserializer {
|
||||||
self
|
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 when deserializing an untagged enum because the content may need to be
|
||||||
/// used more than once.
|
/// used more than once.
|
||||||
impl<'a, E> Deserializer for &'a Content<E>
|
impl<'a, E> Deserializer for ContentRefDeserializer<'a, E>
|
||||||
where E: de::Error
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Error = E;
|
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>
|
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match *self {
|
match *self.content {
|
||||||
Content::Bool(v, _) => visitor.visit_bool(v),
|
Content::Bool(v) => visitor.visit_bool(v),
|
||||||
Content::U8(v) => visitor.visit_u8(v),
|
Content::U8(v) => visitor.visit_u8(v),
|
||||||
Content::U16(v) => visitor.visit_u16(v),
|
Content::U16(v) => visitor.visit_u16(v),
|
||||||
Content::U32(v) => visitor.visit_u32(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::String(ref v) => visitor.visit_str(v),
|
||||||
Content::Unit => visitor.visit_unit(),
|
Content::Unit => visitor.visit_unit(),
|
||||||
Content::None => visitor.visit_none(),
|
Content::None => visitor.visit_none(),
|
||||||
Content::Some(ref v) => visitor.visit_some(&**v),
|
Content::Some(ref v) => visitor.visit_some((&**v).into_deserializer()),
|
||||||
Content::Newtype(ref v) => visitor.visit_newtype_struct(&**v),
|
Content::Newtype(ref v) => visitor.visit_newtype_struct((&**v).into_deserializer()),
|
||||||
Content::Seq(ref v) => {
|
Content::Seq(ref v) => {
|
||||||
let seq = v.into_iter();
|
let seq = v.into_iter();
|
||||||
let mut seq_visitor = de::value::SeqDeserializer::new(seq);
|
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>
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor
|
where V: Visitor
|
||||||
{
|
{
|
||||||
match *self {
|
match *self.content {
|
||||||
Content::None => visitor.visit_none(),
|
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(),
|
Content::Unit => visitor.visit_unit(),
|
||||||
_ => visitor.visit_some(self)
|
_ => 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
|
where E: de::Error
|
||||||
{
|
{
|
||||||
type Deserializer = Self;
|
type Deserializer = ContentRefDeserializer<'a, E>;
|
||||||
|
|
||||||
fn into_deserializer(self) -> Self {
|
fn into_deserializer(self) -> Self::Deserializer {
|
||||||
self
|
ContentRefDeserializer {
|
||||||
|
content: self,
|
||||||
|
err: PhantomData,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user