Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serde support to concrete units #77

Merged
merged 4 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,6 +60,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: doc
args: --all-features

- name: Build examples
uses: marcopolo/cargo@master
Expand Down
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ Hertz::<u64>::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)
Expand Down
1 change: 1 addition & 0 deletions src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: TimeInt = u32>(pub T);

impl<T: TimeInt> $name<T> {
Expand Down
11 changes: 8 additions & 3 deletions src/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use num::traits::{WrappingAdd, WrappingSub};
/// Instant::<SomeClock>::new(23);
/// ```
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Instant<Clock: crate::Clock> {
ticks: Clock::T,
}
Expand Down Expand Up @@ -176,7 +177,7 @@ impl<Clock: crate::Clock> Instant<Clock> {
pub fn checked_add<Dur: Duration>(self, duration: Dur) -> Option<Self>
where
Dur: FixedPoint,
Clock::T: TryFrom<Dur::T>,
Clock::T: TryFrom<Dur::T> + core::ops::Div<Output = Clock::T>,
{
let add_ticks: Clock::T = duration.into_ticks(Clock::SCALING_FACTOR).ok()?;
if add_ticks <= (<Clock::T as num::Bounded>::max_value() / 2.into()) {
Expand Down Expand Up @@ -214,7 +215,7 @@ impl<Clock: crate::Clock> Instant<Clock> {
pub fn checked_sub<Dur: Duration>(self, duration: Dur) -> Option<Self>
where
Dur: FixedPoint,
Clock::T: TryFrom<Dur::T>,
Clock::T: TryFrom<Dur::T> + core::ops::Div<Output = Clock::T>,
{
let sub_ticks: Clock::T = duration.into_ticks(Clock::SCALING_FACTOR).ok()?;
if sub_ticks <= (<Clock::T as num::Bounded>::max_value() / 2.into()) {
Expand Down Expand Up @@ -266,7 +267,11 @@ impl<Clock: crate::Clock> PartialOrd for Instant<Clock> {
}
}

impl<Clock: crate::Clock> Ord for Instant<Clock> {
impl<Clock> Ord for Instant<Clock>
where
Clock: crate::Clock,
Clock::T: ops::Div<Output = Clock::T>,
{
fn cmp(&self, other: &Self) -> Ordering {
self.ticks
.wrapping_sub(&other.ticks)
Expand Down
1 change: 1 addition & 0 deletions src/rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: TimeInt = u32>(pub T);

impl<T: TimeInt> $name<T> {
Expand Down