use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Copy, Default, PartialEq, PartialOrd, Debug)]
#[serde(transparent)]
pub struct Float(f64);
impl Float {
pub const fn new(value: f64) -> Self {
Self(value)
}
pub const fn into_inner(self) -> f64 {
self.0
}
}
impl From<f64> for Float {
fn from(value: f64) -> Self {
Self::new(value)
}
}
impl From<Float> for f64 {
fn from(value: Float) -> Self {
value.into_inner()
}
}
impl std::ops::Deref for Float {
type Target = f64;
fn deref(&self) -> &f64 {
&self.0
}
}
impl std::ops::DerefMut for Float {
fn deref_mut(&mut self) -> &mut f64 {
&mut self.0
}
}
#[cfg(test)]
mod tests {
use crate::{decode, encode};
use super::*;
#[test]
fn wire_shape_is_identical_to_a_bare_f64() {
assert_eq!(encode(&Float::new(7.5)).unwrap(), encode(&7.5_f64).unwrap());
assert_eq!(
decode::<f64>(&encode(&Float::new(7.5)).unwrap()).unwrap(),
7.5
);
assert_eq!(
decode::<Float>(&encode(&7.5_f64).unwrap()).unwrap(),
Float::new(7.5)
);
}
}