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 Aug 11, 2023
1 parent 05a5b7e commit 5794b96
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 @@ -174,6 +174,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 @@ -222,6 +233,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 @@ -276,6 +299,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 @@ -638,6 +705,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 @@ -744,6 +823,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 @@ -821,6 +912,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 @@ -869,6 +971,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 @@ -948,6 +1061,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 @@ -1076,6 +1202,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 @@ -1190,6 +1327,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 @@ -1489,6 +1641,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 @@ -1542,6 +1705,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 5794b96

Please sign in to comment.