Skip to content

Commit

Permalink
Add serde support behind a feature flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Deschamps committed Jan 18, 2018
1 parent 1262612 commit ba9f21b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Never again should you need to specify units in a comment!"""
default = []
oibit = []
spec = []
test = ["clippy", "quickcheck", "quickcheck_macros", "approx", "oibit", "spec"]
test = ["clippy", "quickcheck", "quickcheck_macros", "approx", "oibit", "spec", "serde", "serde_test"]

[dependencies]
typenum = "1.6.0"
Expand All @@ -33,3 +33,5 @@ Never again should you need to specify units in a comment!"""
quickcheck = { version = "0.4.1", optional = true }
quickcheck_macros = { version = "0.4.1", optional = true }
approx = { version = "0.1.1", optional = true, features = ["no_std"] }
serde = { version = "1.0.0", optional = true }
serde_test = { version = "1.0.0", optional = true }
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ pub extern crate generic_array;
#[cfg(feature = "approx")]
pub extern crate approx;

#[cfg(feature = "serde")]
extern crate serde;

#[macro_use] mod make_units;
mod fmt;

Expand Down
29 changes: 29 additions & 0 deletions src/make_units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,35 @@ macro_rules! make_units {
self.value_unsafe.ulps_ne(&other.value_unsafe, epsilon.value_unsafe, max_ulps)
}
}

// --------------------------------------------------------------------------------
// 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)
}
}

// --------------------------------------------------------------------------------
);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![cfg(feature = "serde")]

extern crate dimensioned as dim;
extern crate serde;
extern crate serde_test;

use dim::si;
use serde_test::{assert_tokens, Token};

#[test]
fn serialization() {
let dist = 6.0 * si::M;
assert_tokens(&dist, &[Token::F64(6.0)]);

let speed = 2.0 * si::MPS;
assert_tokens(&speed, &[Token::F64(2.0)]);
}

0 comments on commit ba9f21b

Please # to comment.