From 2d047941d9ce129b1ba87324426f2bf1f52f07ae Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Tue, 6 Oct 2020 19:57:08 -0500 Subject: [PATCH 1/4] Add serde support to concrete units --- Cargo.lock | 4 ++++ Cargo.toml | 1 + README.md | 4 ++++ src/duration.rs | 1 + src/instant.rs | 1 + src/rate.rs | 1 + 6 files changed, 12 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index dea8ab5..719d879 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -269,6 +269,7 @@ dependencies = [ "criterion", "crossbeam-utils", "num", + "serde", "test-case", "version-sync", ] @@ -747,6 +748,9 @@ name = "serde" version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" +dependencies = [ + "serde_derive", +] [[package]] name = "serde_cbor" diff --git a/Cargo.toml b/Cargo.toml index bfc7fd2..e7aef4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ members = ["examples"] [dependencies] num = { version = "0.3.0", default-features = false } +serde = { version = "1.0.0", default-features = false, features = ["derive"], optional = true } [dev-dependencies] crossbeam-utils = "0.7.2" diff --git a/README.md b/README.md index 9f66cf5..5471db9 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,10 @@ Hertz::::try_from(Generic::new(2_000_u32, Fraction::new(1,1_000))) == Ok(He - Thorough documentation with examples - Example for the nRF52_DK board +## Features + +- `serde`: Enables `serde::Deserialize` and `serde::Serialize` implementations for concrete units. + ## Notes Some parts of this crate were derived from various sources: - [`RTIC`](https://github.com/rtic-rs/cortex-m-rtic) diff --git a/src/duration.rs b/src/duration.rs index a8b2abf..2639518 100644 --- a/src/duration.rs +++ b/src/duration.rs @@ -484,6 +484,7 @@ pub mod units { ( $name:ident, ($numer:expr, $denom:expr) ) => { /// A duration unit type #[derive(Copy, Clone, Eq, Ord, Hash, Debug, Default)] + #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct $name(pub T); impl $name { diff --git a/src/instant.rs b/src/instant.rs index ddccfc3..50725c8 100644 --- a/src/instant.rs +++ b/src/instant.rs @@ -46,6 +46,7 @@ use num::traits::{WrappingAdd, WrappingSub}; /// Instant::::new(23); /// ``` #[derive(Debug)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Instant { ticks: Clock::T, } diff --git a/src/rate.rs b/src/rate.rs index b855c6b..a8a6e0b 100644 --- a/src/rate.rs +++ b/src/rate.rs @@ -393,6 +393,7 @@ pub mod units { ( $name:ident, ($numer:expr, $denom:expr), $desc:literal ) => { #[doc = $desc] #[derive(Copy, Clone, Eq, Ord, Hash, Debug, Default)] + #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct $name(pub T); impl $name { From 8c39f48f1962f7f774c6f0e81e82cb0c7b71c85c Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Tue, 6 Oct 2020 20:07:15 -0500 Subject: [PATCH 2/4] Add serde to CI --- .github/workflows/ci.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7e2866e..a91e404 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,19 +37,19 @@ jobs: uses: actions-rs/cargo@v1 with: command: build - args: --lib + args: --lib --all-features - name: Test Library uses: actions-rs/cargo@v1 with: command: test - args: --lib -- --test-threads=1 + args: --lib --all-features -- --test-threads=1 - name: Test Documentation uses: actions-rs/cargo@v1 with: command: test - args: --doc + args: --doc --all-features - name: Clippy uses: actions-rs/cargo@v1 @@ -60,6 +60,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: doc + args: --all-features - name: Build examples uses: marcopolo/cargo@master From e0f5c55114aff13046760223a9b0510c51b9e0c8 Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Wed, 7 Oct 2020 12:49:27 -0500 Subject: [PATCH 3/4] Fix division bounds --- src/instant.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/instant.rs b/src/instant.rs index 50725c8..46af828 100644 --- a/src/instant.rs +++ b/src/instant.rs @@ -177,7 +177,7 @@ impl Instant { pub fn checked_add(self, duration: Dur) -> Option where Dur: FixedPoint, - Clock::T: TryFrom, + Clock::T: TryFrom + core::ops::Div, { let add_ticks: Clock::T = duration.into_ticks(Clock::SCALING_FACTOR).ok()?; if add_ticks <= (::max_value() / 2.into()) { @@ -215,7 +215,7 @@ impl Instant { pub fn checked_sub(self, duration: Dur) -> Option where Dur: FixedPoint, - Clock::T: TryFrom, + Clock::T: TryFrom + core::ops::Div, { let sub_ticks: Clock::T = duration.into_ticks(Clock::SCALING_FACTOR).ok()?; if sub_ticks <= (::max_value() / 2.into()) { @@ -267,7 +267,11 @@ impl PartialOrd for Instant { } } -impl Ord for Instant { +impl Ord for Instant +where + Clock: crate::Clock, + Clock::T: ops::Div, +{ fn cmp(&self, other: &Self) -> Ordering { self.ticks .wrapping_sub(&other.ticks) From c2b1700015f66ec89bce1aad8934ddd22e0aaa22 Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Tue, 27 Apr 2021 18:48:23 -0500 Subject: [PATCH 4/4] Enable resolver 2 to fix std/no_std dependencies This was stabilized in Cargo 1.51. Leaving the example crate for now to support older stable versions. --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index e7aef4a..153874a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ repository = "https://github.com/FluenTech/embedded-time/" license = "MIT OR Apache-2.0" exclude = ["/.github/"] autoexamples = false +resolver = "2" # The examples must be their own package due to conflicts with the criterion crate. # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html