Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions serde/src/de/ignored_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ impl<'de> Visitor<'de> for IgnoredAny {
Ok(IgnoredAny)
}

#[inline]
#[cfg(feature = "unstable")]
fn visit_f128<E>(self, x: f128) -> Result<Self::Value, E> {
let _ = x;
Ok(IgnoredAny)
}

#[inline]
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
Expand Down
20 changes: 20 additions & 0 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,15 @@ impl_deserialize_num! {
uint_to_self!(u32:visit_u32 u64:visit_u64);
}

#[cfg(feature = "unstable")]
impl_deserialize_num! {
f16, deserialize_f16
num_self!(f16:visit_f16);
num_as_copysign_self!(f32:visit_f32 f64:visit_f64);
num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
}

impl_deserialize_num! {
f32, deserialize_f32
num_self!(f32:visit_f32);
Expand Down Expand Up @@ -517,6 +526,17 @@ macro_rules! num_128 {
};
}

#[cfg(feature = "unstable")]
impl_deserialize_num! {
f128, deserialize_f128
num_self!(f128:visit_f128);
num_as_copysign_self!(f32:visit_f32 f64:visit_f64);
num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64);
num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
num_128!(u128:visit_u128);
num_128!(i128:visit_i128);
}

impl_deserialize_num! {
i128, NonZeroI128, deserialize_i128
num_self!(i128:visit_i128);
Expand Down
59 changes: 59 additions & 0 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,18 @@ pub trait Deserializer<'de>: Sized {
Err(Error::custom("u128 is not supported"))
}

/// Hint that the `Deserialize` type is expecting a `f16` value.
///
/// The default behavior unconditionally returns an error.
#[cfg(feature = "unstable")]
fn deserialize_f16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
let _ = visitor;
Err(Error::custom("f16 is not supported"))
}

/// Hint that the `Deserialize` type is expecting a `f32` value.
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
Expand All @@ -1006,6 +1018,18 @@ pub trait Deserializer<'de>: Sized {
where
V: Visitor<'de>;

/// Hint that the `Deserialize` type is expecting a `f128` value.
///
/// The default behavior unconditionally returns an error.
#[cfg(feature = "unstable")]
fn deserialize_f128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
let _ = visitor;
Err(Error::custom("f128 is not supported"))
}

/// Hint that the `Deserialize` type is expecting a `char` value.
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
Expand Down Expand Up @@ -1439,6 +1463,19 @@ pub trait Visitor<'de>: Sized {
))
}

/// The input contains a `f16`.
///
/// The default implementation forwards to [`visit_f64`].
///
/// [`visit_f64`]: #method.visit_f64
#[cfg(feature = "unstable")]
fn visit_f16<E>(self, v: f16) -> Result<Self::Value, E>
where
E: Error,
{
self.visit_f64(v as f64)
}

/// The input contains an `f32`.
///
/// The default implementation forwards to [`visit_f64`].
Expand All @@ -1461,6 +1498,28 @@ pub trait Visitor<'de>: Sized {
Err(Error::invalid_type(Unexpected::Float(v), &self))
}

/// The input contains a `f128`.
///
/// The default implementation fails with a type error.
#[cfg(feature = "unstable")]
fn visit_f128<E>(self, v: f128) -> Result<Self::Value, E>
where
E: Error,
{
let mut buf = [0u8; 64];
let mut writer = crate::format::Buf::new(&mut buf);
fmt::Write::write_fmt(
&mut writer,
format_args!("float `{:x}` as f128", v.to_bits()),
)
.unwrap();

Err(Error::invalid_type(
Unexpected::Other(writer.as_str()),
&self,
))
}

/// The input contains a `char`.
///
/// The default implementation forwards to [`visit_str`] as a one-character
Expand Down
76 changes: 75 additions & 1 deletion serde/src/de/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ where
map struct enum identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
Expand Down Expand Up @@ -230,7 +235,7 @@ where
}

forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f16 f32 f64 f128 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
Expand Down Expand Up @@ -294,6 +299,11 @@ macro_rules! primitive_deserializer {
tuple tuple_struct map struct enum identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
Expand Down Expand Up @@ -336,8 +346,12 @@ primitive_deserializer!(u16, "a `u16`.", U16Deserializer, visit_u16);
primitive_deserializer!(u64, "a `u64`.", U64Deserializer, visit_u64);
primitive_deserializer!(u128, "a `u128`.", U128Deserializer, visit_u128);
primitive_deserializer!(usize, "a `usize`.", UsizeDeserializer, visit_u64 as u64);
#[cfg(feature = "unstable")]
primitive_deserializer!(f16, "an `f16`.", F16Deserializer, visit_f16);
primitive_deserializer!(f32, "an `f32`.", F32Deserializer, visit_f32);
primitive_deserializer!(f64, "an `f64`.", F64Deserializer, visit_f64);
#[cfg(feature = "unstable")]
primitive_deserializer!(f128, "an `f128`.", F128Deserializer, visit_f128);
primitive_deserializer!(char, "a `char`.", CharDeserializer, visit_char);

/// A deserializer holding a `u32`.
Expand Down Expand Up @@ -381,6 +395,11 @@ where
tuple_struct map struct identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
Expand Down Expand Up @@ -501,6 +520,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

impl<'de, 'a, E> IntoDeserializer<'de, E> for StrDeserializer<'a, E>
Expand Down Expand Up @@ -591,6 +615,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

impl<'de, E> IntoDeserializer<'de, E> for BorrowedStrDeserializer<'de, E>
Expand Down Expand Up @@ -705,6 +734,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
Expand Down Expand Up @@ -825,6 +859,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
Expand Down Expand Up @@ -914,6 +953,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

impl<'de, 'a, E> IntoDeserializer<'de, E> for BytesDeserializer<'a, E>
Expand Down Expand Up @@ -973,6 +1017,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

impl<'de, E> IntoDeserializer<'de, E> for BorrowedBytesDeserializer<'de, E>
Expand Down Expand Up @@ -1063,6 +1112,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

impl<'de, I, T, E> IntoDeserializer<'de, E> for SeqDeserializer<I, E>
Expand Down Expand Up @@ -1207,6 +1261,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

impl<'de, A> IntoDeserializer<'de, A::Error> for SeqAccessDeserializer<A>
Expand Down Expand Up @@ -1332,6 +1391,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct tuple_struct map
struct enum identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

impl<'de, I, E> IntoDeserializer<'de, E> for MapDeserializer<'de, I, E>
Expand Down Expand Up @@ -1487,6 +1551,11 @@ where
struct enum identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
Expand Down Expand Up @@ -1648,6 +1717,11 @@ where
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct identifier ignored_any
}

#[cfg(feature = "unstable")]
forward_to_deserialize_any! {
f16 f128
}
}

impl<'de, A> IntoDeserializer<'de, A::Error> for MapAccessDeserializer<A>
Expand Down
15 changes: 10 additions & 5 deletions serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@
// discussion of these features please refer to this issue:
//
// https://github.com/serde-rs/serde/issues/812
#![cfg_attr(feature = "unstable", feature(never_type))]
#![allow(unknown_lints, bare_trait_objects, deprecated, mismatched_lifetime_syntaxes)]
#![cfg_attr(feature = "unstable", feature(never_type, f16, f128))]
#![allow(
unknown_lints,
bare_trait_objects,
deprecated,
mismatched_lifetime_syntaxes
)]
// Ignored clippy and clippy_pedantic lints
#![allow(
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
Expand Down Expand Up @@ -304,12 +309,12 @@ macro_rules! tri {
#[macro_use]
mod macros;

#[macro_use]
mod integer128;

pub mod de;
pub mod ser;

#[macro_use]
mod integer128;

mod format;

#[doc(inline)]
Expand Down
8 changes: 8 additions & 0 deletions serde/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
/// [`Deserializer::deserialize_any`]: crate::Deserializer::deserialize_any
#[macro_export(local_inner_macros)]
macro_rules! forward_to_deserialize_any {
// there should be just a special case called all that implements all of this,
// this is a pain to update
(<$visitor:ident: Visitor<$lifetime:tt>> $($func:ident)*) => {
$(forward_to_deserialize_any_helper!{$func<$lifetime, $visitor>})*
};
Expand Down Expand Up @@ -171,12 +173,18 @@ macro_rules! forward_to_deserialize_any_helper {
(u128<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_u128<$l, $v>()}
};
(f16<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_f16<$l, $v>()}
};
(f32<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_f32<$l, $v>()}
};
(f64<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_f64<$l, $v>()}
};
(f128<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_f128<$l, $v>()}
};
(char<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_char<$l, $v>()}
};
Expand Down
Loading
Loading