mirror of
https://github.com/serde-rs/serde.git
synced 2025-10-02 15:25:38 +00:00
Merge pull request #840 from serde-rs/privimpl
Make the built-in visitors private
This commit is contained in:
commit
887985523e
104
serde/src/de/ignored_any.rs
Normal file
104
serde/src/de/ignored_any.rs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
use core::fmt;
|
||||||
|
|
||||||
|
use de::{Deserialize, Deserializer, Visitor, SeqVisitor, MapVisitor, Error};
|
||||||
|
|
||||||
|
/// A target for deserializers that want to ignore data. Implements Deserialize
|
||||||
|
/// and silently eats data given to it.
|
||||||
|
pub struct IgnoredAny;
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for IgnoredAny {
|
||||||
|
#[inline]
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error>
|
||||||
|
where D: Deserializer<'de>
|
||||||
|
{
|
||||||
|
struct IgnoredAnyVisitor;
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for IgnoredAnyVisitor {
|
||||||
|
type Value = IgnoredAny;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
formatter.write_str("anything at all")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_bool<E>(self, _: bool) -> Result<IgnoredAny, E> {
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_i64<E>(self, _: i64) -> Result<IgnoredAny, E> {
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_u64<E>(self, _: u64) -> Result<IgnoredAny, E> {
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_f64<E>(self, _: f64) -> Result<IgnoredAny, E> {
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_str<E>(self, _: &str) -> Result<IgnoredAny, E>
|
||||||
|
where E: Error
|
||||||
|
{
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_none<E>(self) -> Result<IgnoredAny, E> {
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_some<D>(self, _: D) -> Result<IgnoredAny, D::Error>
|
||||||
|
where D: Deserializer<'de>
|
||||||
|
{
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_newtype_struct<D>(self, _: D) -> Result<IgnoredAny, D::Error>
|
||||||
|
where D: Deserializer<'de>
|
||||||
|
{
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_unit<E>(self) -> Result<IgnoredAny, E> {
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_seq<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
||||||
|
where V: SeqVisitor<'de>
|
||||||
|
{
|
||||||
|
while let Some(_) = try!(visitor.visit::<IgnoredAny>()) {
|
||||||
|
// Gobble
|
||||||
|
}
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_map<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
||||||
|
where V: MapVisitor<'de>
|
||||||
|
{
|
||||||
|
while let Some((_, _)) = try!(visitor.visit::<IgnoredAny, IgnoredAny>()) {
|
||||||
|
// Gobble
|
||||||
|
}
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_bytes<E>(self, _: &[u8]) -> Result<IgnoredAny, E>
|
||||||
|
where E: Error
|
||||||
|
{
|
||||||
|
Ok(IgnoredAny)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_ignored_any(IgnoredAnyVisitor)
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,3 @@
|
|||||||
//! This module contains `Deserialize` and `Visitor` implementations.
|
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
#[cfg(all(feature = "collections", not(feature = "std")))]
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
@ -65,8 +63,7 @@ use bytes::ByteBuf;
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// A visitor that produces a `()`.
|
struct UnitVisitor;
|
||||||
pub struct UnitVisitor;
|
|
||||||
|
|
||||||
impl<'de> Visitor<'de> for UnitVisitor {
|
impl<'de> Visitor<'de> for UnitVisitor {
|
||||||
type Value = ();
|
type Value = ();
|
||||||
@ -92,8 +89,7 @@ impl<'de> Deserialize<'de> for () {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// A visitor that produces a `bool`.
|
struct BoolVisitor;
|
||||||
pub struct BoolVisitor;
|
|
||||||
|
|
||||||
impl<'de> Visitor<'de> for BoolVisitor {
|
impl<'de> Visitor<'de> for BoolVisitor {
|
||||||
type Value = bool;
|
type Value = bool;
|
||||||
@ -408,8 +404,7 @@ impl<'de, T> Deserialize<'de> for Option<T>
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// A visitor that produces a `PhantomData`.
|
struct PhantomDataVisitor<T> {
|
||||||
pub struct PhantomDataVisitor<T> {
|
|
||||||
marker: PhantomData<T>,
|
marker: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -448,14 +443,12 @@ macro_rules! seq_impl {
|
|||||||
$with_capacity:expr,
|
$with_capacity:expr,
|
||||||
$insert:expr
|
$insert:expr
|
||||||
) => {
|
) => {
|
||||||
/// A visitor that produces a sequence.
|
struct $visitor_ty<T $(, $typaram)*> {
|
||||||
pub struct $visitor_ty<T $(, $typaram)*> {
|
|
||||||
marker: PhantomData<$ty<T $(, $typaram)*>>,
|
marker: PhantomData<$ty<T $(, $typaram)*>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T $(, $typaram),*> $visitor_ty<T $(, $typaram)*> {
|
impl<T $(, $typaram),*> $visitor_ty<T $(, $typaram)*> {
|
||||||
/// Construct a new sequence visitor.
|
fn new() -> Self {
|
||||||
pub fn new() -> Self {
|
|
||||||
$visitor_ty {
|
$visitor_ty {
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
@ -561,7 +554,7 @@ struct ArrayVisitor<A> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<A> ArrayVisitor<A> {
|
impl<A> ArrayVisitor<A> {
|
||||||
pub fn new() -> Self {
|
fn new() -> Self {
|
||||||
ArrayVisitor { marker: PhantomData }
|
ArrayVisitor { marker: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -673,14 +666,12 @@ array_impls! {
|
|||||||
macro_rules! tuple_impls {
|
macro_rules! tuple_impls {
|
||||||
($($len:expr => $visitor:ident => ($($n:tt $name:ident)+))+) => {
|
($($len:expr => $visitor:ident => ($($n:tt $name:ident)+))+) => {
|
||||||
$(
|
$(
|
||||||
/// Construct a tuple visitor.
|
struct $visitor<$($name,)+> {
|
||||||
pub struct $visitor<$($name,)+> {
|
|
||||||
marker: PhantomData<($($name,)+)>,
|
marker: PhantomData<($($name,)+)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<$($name,)+> $visitor<$($name,)+> {
|
impl<$($name,)+> $visitor<$($name,)+> {
|
||||||
/// Construct a `TupleVisitor*<T>`.
|
fn new() -> Self {
|
||||||
pub fn new() -> Self {
|
|
||||||
$visitor { marker: PhantomData }
|
$visitor { marker: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -749,14 +740,12 @@ macro_rules! map_impl {
|
|||||||
$ctor:expr,
|
$ctor:expr,
|
||||||
$with_capacity:expr
|
$with_capacity:expr
|
||||||
) => {
|
) => {
|
||||||
/// A visitor that produces a map.
|
struct $visitor_ty<K, V $(, $typaram)*> {
|
||||||
pub struct $visitor_ty<K, V $(, $typaram)*> {
|
|
||||||
marker: PhantomData<$ty<K, V $(, $typaram)*>>,
|
marker: PhantomData<$ty<K, V $(, $typaram)*>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K, V $(, $typaram)*> $visitor_ty<K, V $(, $typaram)*> {
|
impl<K, V $(, $typaram)*> $visitor_ty<K, V $(, $typaram)*> {
|
||||||
/// Construct a `MapVisitor*<T>`.
|
fn new() -> Self {
|
||||||
pub fn new() -> Self {
|
|
||||||
$visitor_ty {
|
$visitor_ty {
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
@ -1498,107 +1487,3 @@ impl<'de, T, E> Deserialize<'de> for Result<T, E>
|
|||||||
deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData))
|
deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
/// A target for deserializers that want to ignore data. Implements
|
|
||||||
/// Deserialize and silently eats data given to it.
|
|
||||||
pub struct IgnoredAny;
|
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for IgnoredAny {
|
|
||||||
#[inline]
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error>
|
|
||||||
where D: Deserializer<'de>
|
|
||||||
{
|
|
||||||
struct IgnoredAnyVisitor;
|
|
||||||
|
|
||||||
impl<'de> Visitor<'de> for IgnoredAnyVisitor {
|
|
||||||
type Value = IgnoredAny;
|
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
formatter.write_str("anything at all")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_bool<E>(self, _: bool) -> Result<IgnoredAny, E> {
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_i64<E>(self, _: i64) -> Result<IgnoredAny, E> {
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_u64<E>(self, _: u64) -> Result<IgnoredAny, E> {
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_f64<E>(self, _: f64) -> Result<IgnoredAny, E> {
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_str<E>(self, _: &str) -> Result<IgnoredAny, E>
|
|
||||||
where E: Error
|
|
||||||
{
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_none<E>(self) -> Result<IgnoredAny, E> {
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_some<D>(self, _: D) -> Result<IgnoredAny, D::Error>
|
|
||||||
where D: Deserializer<'de>
|
|
||||||
{
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_newtype_struct<D>(self, _: D) -> Result<IgnoredAny, D::Error>
|
|
||||||
where D: Deserializer<'de>
|
|
||||||
{
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_unit<E>(self) -> Result<IgnoredAny, E> {
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_seq<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
|
||||||
where V: SeqVisitor<'de>
|
|
||||||
{
|
|
||||||
while let Some(_) = try!(visitor.visit::<IgnoredAny>()) {
|
|
||||||
// Gobble
|
|
||||||
}
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_map<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
|
|
||||||
where V: MapVisitor<'de>
|
|
||||||
{
|
|
||||||
while let Some((_, _)) = try!(visitor.visit::<IgnoredAny, IgnoredAny>()) {
|
|
||||||
// Gobble
|
|
||||||
}
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_bytes<E>(self, _: &[u8]) -> Result<IgnoredAny, E>
|
|
||||||
where E: Error
|
|
||||||
{
|
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO maybe not necessary with impl specialization
|
|
||||||
deserializer.deserialize_ignored_any(IgnoredAnyVisitor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -107,10 +107,11 @@ use core::marker::PhantomData;
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub mod impls;
|
|
||||||
pub mod value;
|
pub mod value;
|
||||||
|
|
||||||
mod from_primitive;
|
mod from_primitive;
|
||||||
|
mod ignored_any;
|
||||||
|
mod impls;
|
||||||
|
|
||||||
// Helpers used by generated code. Not public API.
|
// Helpers used by generated code. Not public API.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
@ -118,6 +119,8 @@ pub mod private;
|
|||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
mod content;
|
mod content;
|
||||||
|
|
||||||
|
pub use self::ignored_any::IgnoredAny;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// The `Error` trait allows `Deserialize` implementations to create descriptive
|
/// The `Error` trait allows `Deserialize` implementations to create descriptive
|
||||||
|
@ -1288,7 +1288,7 @@ fn deserialize_map(ident: &syn::Ident,
|
|||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
_ => { let _ = try!(_serde::de::MapVisitor::visit_value::<_serde::de::impls::IgnoredAny>(&mut __visitor)); }
|
_ => { let _ = try!(_serde::de::MapVisitor::visit_value::<_serde::de::IgnoredAny>(&mut __visitor)); }
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user