Skip to content

Commit

Permalink
fixed atomic f32 ser/des
Browse files Browse the repository at this point in the history
  • Loading branch information
bwsw committed Mar 26, 2024
1 parent 26d2b95 commit 095e05e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 52 deletions.
34 changes: 32 additions & 2 deletions savant_core/src/atomic_f32.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
use std::sync::atomic::{AtomicU32, Ordering};
use std::fmt;
use std::sync::atomic::{AtomicU32, Ordering};

#[derive(serde::Serialize, serde::Deserialize)]
pub struct AtomicF32(AtomicU32);

impl serde::Serialize for AtomicF32 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.get().serialize(serializer)
}
}

impl<'de> serde::Deserialize<'de> for AtomicF32 {
fn deserialize<D>(deserializer: D) -> Result<AtomicF32, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = f32::deserialize(deserializer)?;
Ok(AtomicF32::new(value))
}
}

impl PartialEq for AtomicF32 {
fn eq(&self, other: &Self) -> bool {
self.get() == other.get()
Expand Down Expand Up @@ -48,3 +66,15 @@ impl fmt::Debug for AtomicF32 {
write!(f, "{}", self.get())
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_ser_deser() {
let a = super::AtomicF32::new(3.14);
let serialized = serde_json::to_string(&a).unwrap();
dbg!(&serialized);
let deserialized: super::AtomicF32 = serde_json::from_str(&serialized).unwrap();
assert_eq!(a, deserialized);
}
}
51 changes: 1 addition & 50 deletions savant_core/src/primitives/bbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::EPS;
use anyhow::{bail, Result};
use geo::{Area, BooleanOps};
use lazy_static::lazy_static;
use serde::ser::SerializeStruct;
use std::f32::consts::PI;
use std::fmt;
use std::sync::atomic::{AtomicBool, Ordering};
Expand All @@ -32,7 +31,7 @@ pub enum BBoxMetricType {
IoOther,
}

#[derive(Debug)]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct RBBoxData {
pub xc: AtomicF32,
pub yc: AtomicF32,
Expand All @@ -42,45 +41,6 @@ pub struct RBBoxData {
pub has_modifications: AtomicBool,
}

// impl serde::Serialize for RBBoxData
impl serde::Serialize for RBBoxData {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct("RBBoxData", 5)?;
state.serialize_field("xc", &self.xc.get())?;
state.serialize_field("yc", &self.yc.get())?;
state.serialize_field("width", &self.width.get())?;
state.serialize_field("height", &self.height.get())?;
state.serialize_field("angle", &self.angle.get())?;
state.end()
}
}

// impl serde::Deserialize for RBBoxData
impl<'de> serde::Deserialize<'de> for RBBoxData {
fn deserialize<D>(deserializer: D) -> Result<RBBoxData, D::Error>
where
D: serde::Deserializer<'de>,
{
let data = <serde_json::Value as serde::Deserialize>::deserialize(deserializer)?;
let xc = data["xc"].as_f64().unwrap() as f32;
let yc = data["yc"].as_f64().unwrap() as f32;
let width = data["width"].as_f64().unwrap() as f32;
let height = data["height"].as_f64().unwrap() as f32;
let angle = data["angle"].as_f64().unwrap() as f32;
Ok(RBBoxData {
xc: xc.into(),
yc: yc.into(),
width: width.into(),
height: height.into(),
angle: angle.into(),
has_modifications: false.into(),
})
}
}

impl RBBoxData {
pub fn new(xc: f32, yc: f32, width: f32, height: f32, angle: Option<f32>) -> Self {
Self {
Expand Down Expand Up @@ -646,15 +606,6 @@ mod tests {
use crate::primitives::{RBBox, RBBoxData};
use crate::round_2_digits;

#[test]
fn test_json() {
let bbox = RBBox::new(0.0, 0.0, 100.0, 100.0, Some(45.0));
let json = bbox.json();
let bbox2: RBBox = serde_json::from_str(&json).unwrap();
dbg!(&bbox.json());
assert_eq!(bbox, bbox2);
}

#[test]
fn test_scale_no_angle() {
let bbox = RBBox::new(0.0, 0.0, 100.0, 100.0, None);
Expand Down

0 comments on commit 095e05e

Please sign in to comment.