Skip to content

Commit 7fcbf51

Browse files
committed
rust: serde: add no_fp_fmt_parse support
We do not have formatting for floating point in the kernel, thus simple compile out all that. Probably we should name it differently, more similar to `integer128`. Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 2e4b1b4 commit 7fcbf51

File tree

8 files changed

+33
-4
lines changed

8 files changed

+33
-4
lines changed

rust/serde/de/ignored_any.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ impl<'de> Visitor<'de> for IgnoredAny {
152152
}
153153
}
154154

155+
#[cfg(not(no_fp_fmt_parse))]
155156
#[inline]
156157
fn visit_f64<E>(self, x: f64) -> Result<Self::Value, E> {
157158
let _ = x;

rust/serde/de/impls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ impl_deserialize_num! {
350350
uint_to_self!(u32:visit_u32 u64:visit_u64);
351351
}
352352

353+
#[cfg(not(no_fp_fmt_parse))]
353354
impl_deserialize_num! {
354355
f32, deserialize_f32
355356
num_self!(f32:visit_f32);
@@ -358,6 +359,7 @@ impl_deserialize_num! {
358359
num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64);
359360
}
360361

362+
#[cfg(not(no_fp_fmt_parse))]
361363
impl_deserialize_num! {
362364
f64, deserialize_f64
363365
num_self!(f64:visit_f64);

rust/serde/de/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ pub enum Unexpected<'a> {
344344

345345
/// The input contained a floating point `f32` or `f64` that was not
346346
/// expected.
347+
#[cfg(not(no_fp_fmt_parse))]
347348
Float(f64),
348349

349350
/// The input contained a `char` that was not expected.
@@ -400,6 +401,7 @@ impl<'a> fmt::Display for Unexpected<'a> {
400401
Bool(b) => write!(formatter, "boolean `{}`", b),
401402
Unsigned(i) => write!(formatter, "integer `{}`", i),
402403
Signed(i) => write!(formatter, "integer `{}`", i),
404+
#[cfg(not(no_fp_fmt_parse))]
403405
Float(f) => write!(formatter, "floating point `{}`", f),
404406
Char(c) => write!(formatter, "character `{}`", c),
405407
Str(s) => write!(formatter, "string {:?}", s),
@@ -996,12 +998,20 @@ pub trait Deserializer<'de>: Sized {
996998
/// Hint that the `Deserialize` type is expecting a `f32` value.
997999
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
9981000
where
999-
V: Visitor<'de>;
1001+
V: Visitor<'de>
1002+
{
1003+
let _ = visitor;
1004+
Err(Error::custom("f32 is not supported"))
1005+
}
10001006

10011007
/// Hint that the `Deserialize` type is expecting a `f64` value.
10021008
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
10031009
where
1004-
V: Visitor<'de>;
1010+
V: Visitor<'de>
1011+
{
1012+
let _ = visitor;
1013+
Err(Error::custom("f64 is not supported"))
1014+
}
10051015

10061016
/// Hint that the `Deserialize` type is expecting a `char` value.
10071017
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@@ -1446,6 +1456,7 @@ pub trait Visitor<'de>: Sized {
14461456
/// The default implementation forwards to [`visit_f64`].
14471457
///
14481458
/// [`visit_f64`]: #method.visit_f64
1459+
#[cfg(not(no_fp_fmt_parse))]
14491460
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
14501461
where
14511462
E: Error,
@@ -1456,6 +1467,7 @@ pub trait Visitor<'de>: Sized {
14561467
/// The input contains an `f64`.
14571468
///
14581469
/// The default implementation fails with a type error.
1470+
#[cfg(not(no_fp_fmt_parse))]
14591471
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
14601472
where
14611473
E: Error,

rust/serde/de/value.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ primitive_deserializer!(u8, "a `u8`.", U8Deserializer, visit_u8);
298298
primitive_deserializer!(u16, "a `u16`.", U16Deserializer, visit_u16);
299299
primitive_deserializer!(u64, "a `u64`.", U64Deserializer, visit_u64);
300300
primitive_deserializer!(usize, "a `usize`.", UsizeDeserializer, visit_u64 as u64);
301+
#[cfg(not(no_fp_fmt_parse))]
301302
primitive_deserializer!(f32, "an `f32`.", F32Deserializer, visit_f32);
303+
#[cfg(not(no_fp_fmt_parse))]
302304
primitive_deserializer!(f64, "an `f64`.", F64Deserializer, visit_f64);
303305
primitive_deserializer!(char, "a `char`.", CharDeserializer, visit_char);
304306

rust/serde/private/ser.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct TaggedSerializer<S> {
4646
enum Unsupported {
4747
Boolean,
4848
Integer,
49+
#[cfg(not(no_fp_fmt_parse))]
4950
Float,
5051
Char,
5152
String,
@@ -64,6 +65,7 @@ impl Display for Unsupported {
6465
match *self {
6566
Unsupported::Boolean => formatter.write_str("a boolean"),
6667
Unsupported::Integer => formatter.write_str("an integer"),
68+
#[cfg(not(no_fp_fmt_parse))]
6769
Unsupported::Float => formatter.write_str("a float"),
6870
Unsupported::Char => formatter.write_str("a char"),
6971
Unsupported::String => formatter.write_str("a string"),
@@ -150,10 +152,12 @@ where
150152
Err(self.bad_type(Unsupported::Integer))
151153
}
152154

155+
#[cfg(not(no_fp_fmt_parse))]
153156
fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> {
154157
Err(self.bad_type(Unsupported::Float))
155158
}
156159

160+
#[cfg(not(no_fp_fmt_parse))]
157161
fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> {
158162
Err(self.bad_type(Unsupported::Float))
159163
}

rust/serde/ser/fmt.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,17 @@ impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
5555
serialize_u16: u16,
5656
serialize_u32: u32,
5757
serialize_u64: u64,
58-
serialize_f32: f32,
59-
serialize_f64: f64,
6058
serialize_char: char,
6159
serialize_str: &str,
6260
serialize_unit_struct: &'static str,
6361
}
6462

63+
#[cfg(not(no_fp_fmt_parse))]
64+
fmt_primitives! {
65+
serialize_f32: f32,
66+
serialize_f64: f64,
67+
}
68+
6569
serde_if_integer128! {
6670
fmt_primitives! {
6771
serialize_i128: i128,

rust/serde/ser/impls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ primitive_impl!(u8, serialize_u8);
2929
primitive_impl!(u16, serialize_u16);
3030
primitive_impl!(u32, serialize_u32);
3131
primitive_impl!(u64, serialize_u64);
32+
#[cfg(not(no_fp_fmt_parse))]
3233
primitive_impl!(f32, serialize_f32);
34+
#[cfg(not(no_fp_fmt_parse))]
3335
primitive_impl!(f64, serialize_f64);
3436
primitive_impl!(char, serialize_char);
3537

rust/serde/ser/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ pub trait Serializer: Sized {
644644
/// }
645645
/// }
646646
/// ```
647+
#[cfg(not(no_fp_fmt_parse))]
647648
fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error>;
648649

649650
/// Serialize an `f64` value.
@@ -662,6 +663,7 @@ pub trait Serializer: Sized {
662663
/// }
663664
/// }
664665
/// ```
666+
#[cfg(not(no_fp_fmt_parse))]
665667
fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error>;
666668

667669
/// Serialize a character.

0 commit comments

Comments
 (0)