Skip to content

Commit

Permalink
Modify make_units! so that deriving serde traits is optional.
Browse files Browse the repository at this point in the history
Serde traits are enabled for all built in unit systems (as long as the
serde feature is enabled)
  • Loading branch information
adeschamps committed Mar 24, 2018
1 parent ba9f21b commit 45fd964
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/build/cgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Note: this system is incomplete. More derived units and constants are coming.
M: Centimeter = HECTO * CM.value_unsafe, "Meter";
),
fmt: false,
serde: true,
from: vec![
"SI",
"MKS",
Expand Down
1 change: 1 addition & 0 deletions src/build/fps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Note: this system is incomplete. More derived units and constants are coming.
constants: constants!(
),
fmt: false,
serde: true,
from: vec![
// "SI",
// "MKS",
Expand Down
1 change: 1 addition & 0 deletions src/build/mks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Note: this system is incomplete. More derived units and constants are coming.
constants: constants!(
),
fmt: false,
serde: true,
from: vec![
"SI",
"CGS",
Expand Down
4 changes: 3 additions & 1 deletion src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct System {
pub from: Vec<&'static str>,
pub refl_blacklist: Vec<&'static str>,
pub fmt: bool,
pub serde: bool,
}

impl System {
Expand Down Expand Up @@ -153,11 +154,12 @@ pub mod {} {{
write!(f, " }}
fmt = {};
serde = {};
}}
pub use self::f64consts::*;
", self.fmt)?;
", self.fmt, self.serde)?;

write!(f, "
#[test]
Expand Down
1 change: 1 addition & 0 deletions src/build/si.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ Where experimental values are used for constants, the values are obtained from t
LBF: Newton = 4.4482216152605 * N.value_unsafe, "Pound force";
),
fmt: true,
serde: true,
from: vec![
"UCUM",
],
Expand Down
1 change: 1 addition & 0 deletions src/build/ucum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ in function signatures), this is something to bear in mind.
SMOOT: Meter = 67.0 * IN_I.value_unsafe, "Smoot";
),
fmt: true,
serde: true,
from: vec![
"SI",
],
Expand Down
74 changes: 45 additions & 29 deletions src/make_units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub mod ms {
}
fmt = true;
serde = true;
}
pub use self::f64consts::*;
}
Expand Down Expand Up @@ -128,12 +129,23 @@ which allows the use of `consts::PI` in the `PI definition.
}
```
Finally, we have the `fmt` line. This line can either be `fmt = true;` or `fmt = false;`. In either
Next, we have the `fmt` line. This line can either be `fmt = true;` or `fmt = false;`. In either
case, the trait `core::fmt::Debug` is implemented for your unit system, but all of the other `fmt`
traits are implemented only if this is true. Setting it to false allows you to have custom printing for your system.
traits are implemented only if this is true. Setting it to `false` allows you to have custom
printing for your system.
```ignore
fmt = true;
```
Finally, we have the `serde` line. This line can either be `serde = true;` or `serde = false;`.
Setting it to `true` implements `serde::Deserialize` and `serde::Serialize`, but only if the `serde`
feature is enabled. Add `dimensioned = { version = "*", features = ["serde"] }` to you `Cargo.toml`
to make use of this. The default implementations are dimensionally unsafe; that is, they ignore units.
If you set it to `false`, you may still implement the serde traits yourself.
```ignore
serde = true;
}
```
Expand Down Expand Up @@ -169,6 +181,7 @@ macro_rules! make_units {
$($constant:ident: $ConstantUnit:ident = $constant_value:expr;)*
}
fmt = $to_fmt:ident;
serde = $serde:ident;
) => (
use $crate::dimcore::marker;
use $crate::{Dimensioned, Dimensionless};
Expand Down Expand Up @@ -492,33 +505,7 @@ macro_rules! make_units {

// --------------------------------------------------------------------------------
// Serde
#[cfg(feature = "serde")]
use $crate::serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "serde")]
impl<'de, V, U> Deserialize<'de> for $System<V, U>
where V: Deserialize<'de>
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
let value_unsafe = V::deserialize(deserializer)?;
Ok($System{ value_unsafe, _marker: marker::PhantomData })
}
}

#[cfg(feature = "serde")]
impl<V, U> Serialize for $System<V, U>
where V: Serialize
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
self.value_unsafe.serialize(serializer)
}
}

// --------------------------------------------------------------------------------
__make_units_internal!(@serde $serde, $System);
);
}

Expand Down Expand Up @@ -967,6 +954,35 @@ macro_rules! __make_units_internal {

(@convert_to_zero $Unit:ident) => ( Z0 );
(@convert_to_zero) => ();

(@serde true, $System:ident) => {
#[cfg(feature = "serde")]
use $crate::serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "serde")]
impl<'de, V, U> Deserialize<'de> for $System<V, U>
where V: Deserialize<'de>
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
let value_unsafe = V::deserialize(deserializer)?;
Ok($System{ value_unsafe, _marker: marker::PhantomData })
}
}

#[cfg(feature = "serde")]
impl<V, U> Serialize for $System<V, U>
where V: Serialize
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
self.value_unsafe.serialize(serializer)
}
}
};
(@serde false, $System:ident) => {};
}

/// Create a derived unit based on existing ones
Expand Down

0 comments on commit 45fd964

Please sign in to comment.