-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: work around serde issue with f32 NaNs
Serde serializes f32s of value NaN to `null`, but doesn't serialize them back from `null` to NaN. For that we need a custom deserialize. Also see: <serde-rs/json#202>
- Loading branch information
Showing
5 changed files
with
81 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::metrics::DeltaValue; | ||
use serde::Deserializer; | ||
use std::fmt::{Display, Formatter}; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
/// An `f32` which can deserialize from `null` as `NaN`. | ||
/// | ||
/// Also see: <https://github.com/serde-rs/json/issues/202> | ||
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, serde::Serialize)] | ||
pub struct NullableFloat(pub f32); | ||
|
||
impl Deref for NullableFloat { | ||
type Target = f32; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for NullableFloat { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl From<f32> for NullableFloat { | ||
fn from(value: f32) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl<'de> serde::Deserialize<'de> for NullableFloat { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let value = Option::<f32>::deserialize(deserializer)?; | ||
Ok(Self(value.unwrap_or(f32::NAN))) | ||
} | ||
} | ||
|
||
impl Display for NullableFloat { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
impl DeltaValue for NullableFloat { | ||
type Delta = NullableFloat; | ||
|
||
fn delta(self, value: Self) -> Self::Delta { | ||
NullableFloat(self.0.delta(value.0)) | ||
} | ||
|
||
fn is_delta_positive(value: Self::Delta) -> bool { | ||
f32::is_delta_positive(value.0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters