Skip to content

Commit

Permalink
Implement serde::ser::Serialize and serde::de::Deserialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
radix committed Dec 28, 2017
1 parent 641ebe5 commit 8e936c4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ maintenance = { status = "actively-developed" }

[dependencies]
num = "0.1"
serde = { version = "1.0", optional = true }
typenum = "1.9.0"

[dev-dependencies]
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ See the [examples](examples) directory for more advanced usage:

## Features
`uom` has multiple `Cargo` features for controlling available underlying storage types, the
inclusion of the pre-built [International System of Units][si] (SI), and `no_std` functionality. The
features are described below. `f32`, `f64`, `std`, and `si` are enabled by default. Features can be
cherry-picked by using the `--no-default-features` and `--features "..."` flags when compiling `uom`
or specifying features in Cargo.toml:
inclusion of the pre-built [International System of Units][si] (SI), support for [Serde][serde], and
`no_std` functionality. The features are described below. `f32`, `f64`, `std`, and `si` are enabled
by default. Features can be cherry-picked by using the `--no-default-features` and
`--features "..."` flags when compiling `uom` or specifying features in Cargo.toml:

```toml
[dependencies]
Expand All @@ -88,12 +88,15 @@ uom = {
`rational`, `rational32`, `rational64`, `bigrational`, `f32`, `f64` -- Features to enable
underlying storage types. At least one of these features must be enabled. `f32` and `f64` are
enabled by default.
* `serde` -- Feature to enable support for serialization and deserialization of quantities with
the [serde][serde] crate.
* `si` -- Feature to include the pre-built [International System of Units][si] (SI). Enabled by
default.
* `std` -- Feature to compile with standard library support. Disabling this feature compiles `uom`
with `no_std`. Enabled by default.

[si]: http://jcgm.bipm.org/vim/en/1.16.html
[serde]: https://serde.rs/

## Design
Rather than working with [measurement units](http://jcgm.bipm.org/vim/en/1.9.html) (meter,
Expand Down
15 changes: 11 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
//!
//! ## Features
//! `uom` has multiple `Cargo` features for controlling available underlying storage types, the
//! inclusion of the pre-built [International System of Units][si] (SI), and `no_std` functionality.
//! The features are described below. `f32`, `f64`, `std`, and `si` are enabled by default. Features
//! can be cherry-picked by using the `--no-default-features` and `--features "..."` flags when
//! compiling `uom` or specifying features in Cargo.toml:
//! inclusion of the pre-built [International System of Units][si] (SI), support for [Serde][serde],
//! and `no_std` functionality. The features are described below. `f32`, `f64`, `std`, and `si` are
//! enabled by default. Features can be cherry-picked by using the `--no-default-features` and
//! `--features "..."` flags when compiling `uom` or specifying features in Cargo.toml:
//!
//! ```toml
//! [dependencies]
Expand All @@ -77,12 +77,15 @@
//! `rational`, `rational32`, `rational64`, `bigrational`, `f32`, `f64` -- Features to enable
//! underlying storage types. At least one of these features must be enabled. `f32` and `f64` are
//! enabled by default.
//! * `serde` -- Feature to enable support for serialization and deserialization of quantities with
//! the [serde][serde] crate.
//! * `si` -- Feature to include the pre-built [International System of Units][si] (SI). Enabled by
//! default.
//! * `std` -- Feature to compile with standard library support. Disabling this feature compiles
//! `uom` with `no_std`. Enabled by default.
//!
//! [si]: http://jcgm.bipm.org/vim/en/1.16.html
//! [serde]: https://serde.rs/
//!
//! ## Design
//! Rather than working with [measurement units](http://jcgm.bipm.org/vim/en/1.9.html) (meter,
Expand Down Expand Up @@ -156,6 +159,10 @@ compile_error!("A least one underlying storage type must be enabled. See the fea
#[doc(hidden)]
pub extern crate num;

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

#[doc(hidden)]
pub extern crate typenum;

Expand Down
36 changes: 36 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,42 @@ macro_rules! system {
}
}

#[cfg(feature = "serde")]
impl<D, U, V> $crate::serde::ser::Serialize for Quantity<D, U, V>
where
D: Dimension + ?Sized,
U: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V> + $crate::serde::ser::Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: $crate::serde::ser::Serializer
{
self.value.serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de, D, U, V> $crate::serde::de::Deserialize<'de> for Quantity<D, U, V>
where
D: Dimension + ?Sized,
U: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V> + $crate::serde::de::Deserialize<'de>,
{
fn deserialize<De>(deserializer: De) -> Result<Self, De::Error>
where
De: $crate::serde::de::Deserializer<'de>,
{
let value: V = $crate::serde::de::Deserialize::deserialize(deserializer)?;

Ok(Quantity {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value,
})
}
}

/// Macro to implement [`quantity`](si/struct.Quantity.html) type aliases for a specific
/// [system of units][units] and value storage type.
///
Expand Down

0 comments on commit 8e936c4

Please sign in to comment.