Skip to content

Commit

Permalink
optimal-binary: add serde feature
Browse files Browse the repository at this point in the history
  • Loading branch information
justinlovinger committed Sep 3, 2023
1 parent 75b226b commit ba41589
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions optimal-binary/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ keywords = ["optimization", "binary"]
categories = ["science", "mathematics"]
license = "MIT"

[features]
serde = ["dep:serde"]

[dependencies]
num-traits = "0.2.16"
serde = { version = "1.0.185", features = ["derive"], optional = true }

[dev-dependencies]
24 changes: 24 additions & 0 deletions optimal-binary/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use num_traits::{pow, One, Zero};
use std::ops::{Add, Div, Mul, RangeInclusive, Sub};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Reduce innermost axis
/// to numbers within range.
/// Leftmost is least significant.
Expand All @@ -26,7 +29,13 @@ use std::ops::{Add, Div, Mul, RangeInclusive, Sub};
/// assert_eq!(ToRealLE::new(1.0..=4.0, 2).decode([false, true]), 3.);
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "T: One + Add<Output = T> + Deserialize<'de>"))
)]
pub struct ToRealLE<T> {
#[cfg_attr(feature = "serde", serde(skip))]
to_int: ToIntLE<T>,
start: T,
a: Option<T>,
Expand Down Expand Up @@ -87,7 +96,9 @@ impl<T> ToRealLE<T> {
/// assert_eq!(to_int_le.decode([false, false, true]), 4_u8);
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct ToIntLE<T> {
#[cfg_attr(feature = "serde", serde(skip))]
two: T,
}

Expand All @@ -100,6 +111,19 @@ where
}
}

#[cfg(feature = "serde")]
impl<'de, T> Deserialize<'de> for ToIntLE<T>
where
T: One + Add<Output = T>,
{
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Self::new())
}
}

impl<T> ToIntLE<T> {
pub fn new() -> Self
where
Expand Down

0 comments on commit ba41589

Please sign in to comment.