Skip to content

Commit

Permalink
fj: make fj build with the "serialization" feature
Browse files Browse the repository at this point in the history
This required 3 changes:
1. Import serde::{Serialize, Deserialize}.
2. Implement {Serialize, Deserialize} for Sketch.
3. Enable the "derive" feature for serde.
  • Loading branch information
kamirr committed Jun 11, 2022
1 parent b3bf6b4 commit 46603a5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/fj/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ default = []
serialization = ["serde"]

[dependencies]
serde = { version = "1.0.7", optional = true }
serde = { version = "1.0.7", features = ["derive"], optional = true }

[dependencies.fj-proc]
path = "../../crates/fj-proc"
2 changes: 2 additions & 0 deletions crates/fj/src/angle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
use std::f64::consts::{PI, TAU};

// One gon in radians
Expand Down
2 changes: 2 additions & 0 deletions crates/fj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ mod shape_3d;

pub use self::{angle::*, shape_2d::*, shape_3d::*};
pub use fj_proc::*;
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};

/// A shape
#[derive(Clone, Debug)]
Expand Down
49 changes: 48 additions & 1 deletion crates/fj/src/shape_2d.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "serialization")]
use serde::{de, ser, Deserialize, Serialize};
use std::mem;
use std::sync::atomic;

Expand Down Expand Up @@ -130,7 +132,6 @@ impl From<Difference2d> for Shape2d {
/// that the edges are non-overlapping. If you create a `Sketch` with
/// overlapping edges, you're on your own.
#[derive(Debug)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Sketch {
// The fields are the raw parts of a `Vec`. `Sketch` needs to be FFI-safe,
Expand Down Expand Up @@ -249,6 +250,52 @@ impl Drop for Sketch {
}
}

/// An owned, non-repr-C Sketch
///
/// De/serializing a non-trivial structure with raw pointers is a hassle.
/// This structure is a simple, owned intermediate form that can use the derive
/// macros provided by serde. The implementation of the Serialize and Deserialize
/// traits for Sketch use this type as a stepping stone.
///
/// Note that constructing this requires cloning the points behind Sketch. If
/// de/serialization turns out to be a bottleneck, a more complete implementation
/// will be required.
#[cfg(feature = "serialization")]
#[derive(Serialize, Deserialize)]
#[serde(rename = "Sketch")]
struct SerdeSketch {
points: Vec<[f64; 2]>,
color: [u8; 4],
}

#[cfg(feature = "serialization")]
impl ser::Serialize for Sketch {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
let serde_sketch = SerdeSketch {
points: self.to_points(),
color: self.color,
};

serde_sketch.serialize(serializer)
}
}

#[cfg(feature = "serialization")]
impl<'de> de::Deserialize<'de> for Sketch {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
SerdeSketch::deserialize(deserializer).map(|serde_sketch| {
Sketch::from_points(serde_sketch.points)
.with_color(serde_sketch.color)
})
}
}

impl From<Sketch> for Shape {
fn from(shape: Sketch) -> Self {
Self::Shape2d(shape.into())
Expand Down
2 changes: 2 additions & 0 deletions crates/fj/src/shape_3d.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{Angle, Shape, Shape2d};
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};

/// A 3-dimensional shape
#[derive(Clone, Debug)]
Expand Down

0 comments on commit 46603a5

Please sign in to comment.