Skip to content

Commit

Permalink
Implement IntoDeserializer for all Deserializers in serde::de::value …
Browse files Browse the repository at this point in the history
…module

Unfortunately, blanket implementation IntoDeserializer for Deserializer is impossible
right now because this would be a breaking change. External crates may have this
such implementation (and serde_json actually have it for Value)
  • Loading branch information
Mingun committed Oct 25, 2024
1 parent 58a8d22 commit 9cda015
Showing 1 changed file with 174 additions and 0 deletions.
174 changes: 174 additions & 0 deletions serde/src/de/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ where
}
}

impl<'de, E> IntoDeserializer<'de, E> for UnitDeserializer<E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<E> Debug for UnitDeserializer<E> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.debug_struct("UnitDeserializer").finish()
Expand Down Expand Up @@ -225,6 +236,18 @@ where
}
}

#[cfg(feature = "unstable")]
impl<'de, E> IntoDeserializer<'de, E> for NeverDeserializer<E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

////////////////////////////////////////////////////////////////////////////////

macro_rules! primitive_deserializer {
Expand Down Expand Up @@ -279,6 +302,17 @@ macro_rules! primitive_deserializer {
}
}

impl<'de, E> IntoDeserializer<'de, E> for $name<E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<E> Debug for $name<E> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter
Expand Down Expand Up @@ -369,6 +403,17 @@ where
}
}

impl<'de, E> IntoDeserializer<'de, E> for U32Deserializer<E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<'de, E> de::EnumAccess<'de> for U32Deserializer<E>
where
E: de::Error,
Expand Down Expand Up @@ -458,6 +503,17 @@ where
}
}

impl<'de, 'a, E> IntoDeserializer<'de, E> for StrDeserializer<'a, E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<'de, 'a, E> de::EnumAccess<'de> for StrDeserializer<'a, E>
where
E: de::Error,
Expand Down Expand Up @@ -537,6 +593,17 @@ where
}
}

impl<'de, E> IntoDeserializer<'de, E> for BorrowedStrDeserializer<'de, E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<'de, E> de::EnumAccess<'de> for BorrowedStrDeserializer<'de, E>
where
E: de::Error,
Expand Down Expand Up @@ -640,6 +707,18 @@ where
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, E> IntoDeserializer<'de, E> for StringDeserializer<E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, E> de::EnumAccess<'de> for StringDeserializer<E>
where
Expand Down Expand Up @@ -748,6 +827,18 @@ where
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, E> IntoDeserializer<'de, E> for CowStrDeserializer<'a, E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E>
where
Expand Down Expand Up @@ -825,6 +916,17 @@ where
}
}

impl<'de, 'a, E> IntoDeserializer<'de, E> for BytesDeserializer<'a, E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<'a, E> Debug for BytesDeserializer<'a, E> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter
Expand Down Expand Up @@ -873,6 +975,17 @@ where
}
}

impl<'de, E> IntoDeserializer<'de, E> for BorrowedBytesDeserializer<'de, E>
where
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<'de, E> Debug for BorrowedBytesDeserializer<'de, E> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter
Expand Down Expand Up @@ -952,6 +1065,19 @@ where
}
}

impl<'de, I, T, E> IntoDeserializer<'de, E> for SeqDeserializer<I, E>
where
I: Iterator<Item = T>,
T: IntoDeserializer<'de, E>,
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<'de, I, T, E> de::SeqAccess<'de> for SeqDeserializer<I, E>
where
I: Iterator<Item = T>,
Expand Down Expand Up @@ -1083,6 +1209,17 @@ where
}
}

impl<'de, A> IntoDeserializer<'de, A::Error> for SeqAccessDeserializer<A>
where
A: de::SeqAccess<'de>,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

////////////////////////////////////////////////////////////////////////////////

/// A deserializer that iterates over a map.
Expand Down Expand Up @@ -1197,6 +1334,21 @@ where
}
}

impl<'de, I, E> IntoDeserializer<'de, E> for MapDeserializer<'de, I, E>
where
I: Iterator,
I::Item: private::Pair,
First<I::Item>: IntoDeserializer<'de, E>,
Second<I::Item>: IntoDeserializer<'de, E>,
E: de::Error,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<'de, I, E> de::MapAccess<'de> for MapDeserializer<'de, I, E>
where
I: Iterator,
Expand Down Expand Up @@ -1498,6 +1650,17 @@ where
}
}

impl<'de, A> IntoDeserializer<'de, A::Error> for MapAccessDeserializer<A>
where
A: de::MapAccess<'de>,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

impl<'de, A> de::EnumAccess<'de> for MapAccessDeserializer<A>
where
A: de::MapAccess<'de>,
Expand Down Expand Up @@ -1551,6 +1714,17 @@ where
}
}

impl<'de, A> IntoDeserializer<'de, A::Error> for EnumAccessDeserializer<A>
where
A: de::EnumAccess<'de>,
{
type Deserializer = Self;

fn into_deserializer(self) -> Self {
self
}
}

////////////////////////////////////////////////////////////////////////////////

mod private {
Expand Down

0 comments on commit 9cda015

Please sign in to comment.