Skip to content

Commit

Permalink
no-std Fix #6 (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelmas authored Nov 5, 2023
1 parent 04c970a commit 2b8008a
Show file tree
Hide file tree
Showing 45 changed files with 211 additions and 86 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ jobs:
- run: rustup toolchain install nightly
- run: cargo +nightly -Zscript ./pre-publish.rs
- run: cargo test
- run: cargo test --no-default-features
- run: cargo test --test 'serde' --features serde
- run: cargo test --test 'serde' --no-default-features --features serde
build_and_test_arm:
machine:
image: ubuntu-2004:current
Expand All @@ -19,7 +21,9 @@ jobs:
- run: rustup toolchain install nightly
- run: cargo +nightly -Zscript ./pre-publish.rs
- run: cargo test
- run: cargo test --no-default-features
- run: cargo test --test 'serde' --features serde
- run: cargo test --test 'serde' --no-default-features --features serde

workflows:
version: 2
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ jobs:
cargo build
- name: Run tests
run: cargo test --verbose
- name: Run no-std tests
run: cargo test --verbose --no-default-features
- name: Run serde tests
run: cargo test --verbose --test 'serde' --features serde
- name: Run no-std+serde tests
run: cargo test --verbose --test 'serde' --no-default-features --features serde

clippy:
runs-on: ubuntu-latest
Expand Down
21 changes: 0 additions & 21 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ Any other overhead is considered a bug and should be reported.

# Features

- `serde`: implements `Serialize` and `Deserialize` for all types
- `std`: enabled by default, required by some `f32` and `f64` methods.
- `serde`: implements `Serialize` and `Deserialize` for all types.

## How it works

Expand Down
19 changes: 14 additions & 5 deletions typed_floats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include = [
"src/*.rs",
"*.md",
]
keywords = ["floats", "non-zero", "NaN"]
keywords = ["floats", "non-zero", "NaN", "no_std"]
license = "MIT OR Apache-2.0"
name = "typed_floats"
readme = "./README.md"
Expand All @@ -16,13 +16,22 @@ version = "0.3.0"
rust-version = "1.56"

[features]
default = []
default = ["std"]
serde = ["dep:serde"]
std = ["typed_floats_macros/std"]

[dependencies]
thiserror = "1.0"
typed_floats_macros = {version = "=0.3.0", path = "../typed_floats_macros"}
serde = { version = "1.0", features = ["derive"], optional = true }

[dependencies.typed_floats_macros]
version = "=0.3.0"
path = "../typed_floats_macros"

[dependencies.serde]
version = "1.0"
default-features = false
features = ["derive"]
optional = true


[dev-dependencies]
# For examples
Expand Down
50 changes: 37 additions & 13 deletions typed_floats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
#![warn(clippy::unwrap_in_result)]
#![warn(clippy::indexing_slicing)]
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

/// This macros assert that two values are close to each other.
///
Expand Down Expand Up @@ -247,6 +248,7 @@ macro_rules! assert_is_negative_zero {
}};
}

#[cfg(feature = "std")]
/// This trait is used to specify the return type of the [`Hypot::hypot()`] function.
pub trait Hypot<T> {
/// The resulting type after applying [`Hypot::hypot()`].
Expand Down Expand Up @@ -325,6 +327,7 @@ pub trait Max<T> {
fn max(self, rhs: T) -> Self::Output;
}

#[cfg(feature = "std")]
/// This trait is used to specify the return type of the [`Copysign::copysign()`] function.
pub trait Copysign<T> {
/// The resulting type after applying [`Copysign::copysign()`].
Expand Down Expand Up @@ -359,6 +362,7 @@ pub trait Copysign<T> {
fn copysign(self, rhs: T) -> Self::Output;
}

#[cfg(feature = "std")]
/// This trait is used to specify the return type of the [`DivEuclid::div_euclid()`] function.
pub trait DivEuclid<T> {
/// The resulting type after applying [`DivEuclid::div_euclid()`].
Expand Down Expand Up @@ -389,6 +393,7 @@ pub trait DivEuclid<T> {
fn div_euclid(self, rhs: T) -> Self::Output;
}

#[cfg(feature = "std")]
/// This trait is used to specify the return type of the [`Atan2::atan2()`] function.
pub trait Atan2<T> {
/// The resulting type after applying [`Atan2::atan2()`].
Expand Down Expand Up @@ -418,14 +423,15 @@ pub trait Atan2<T> {
/// let x2 = -3.0_f64;
/// let y2 = 3.0_f64;
///
/// assert_relative_eq!(y1.atan2(x1), -std::f64::consts::FRAC_PI_4);
/// assert_relative_eq!(y2.atan2(x2), 3.0 * std::f64::consts::FRAC_PI_4);
/// assert_relative_eq!(y1.atan2(x1), -core::f64::consts::FRAC_PI_4);
/// assert_relative_eq!(y2.atan2(x2), 3.0 * core::f64::consts::FRAC_PI_4);
/// ```
///
/// See [`f64::atan2()`] for more details.
fn atan2(self, rhs: T) -> Self::Output;
}

#[cfg(feature = "std")]
/// This trait is used to specify the return type of the [`Powf::powf()`] function.
pub trait Powf<T> {
/// The resulting type after applying [`Powf::powf()`].
Expand All @@ -440,44 +446,62 @@ typed_floats_macros::generate_docs!(
);

/// An error that can occur when converting from a string into a `TypedFloat`
#[derive(Error, Debug)]
#[derive(Debug)]
pub enum FromStrError {
/// The string did not contain a valid float number
#[error("{0:?}")]
ParseFloatError(core::num::ParseFloatError),
/// The string contained a valid float number but it didn't fit in the target type
#[error("{0:?}")]
InvalidNumber(InvalidNumber),
}

impl core::fmt::Display for FromStrError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
FromStrError::ParseFloatError(e) => write!(f, "{e}"),
FromStrError::InvalidNumber(e) => write!(f, "{e}"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for FromStrError {}

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

use core::num::{NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8};
use core::num::{NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8};

use thiserror::Error;

/// An error that can occur when converting into a typed float
#[derive(Error, Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
pub enum InvalidNumber {
/// Any variant of `Nan`
#[error("Number is NaN")]
NaN,
/// `+0.0` or `-0.0`
#[error("Number is zero")]
Zero,
/// Any negative number, including `-0.0` and `-inf`
#[error("Number is negative")]
Negative,
/// Any positive number, including `+0.0` and `+inf`
#[error("Number is positive")]
Positive,
/// `+inf` or `-inf`
#[error("Number is infinite")]
Infinite,
}

impl core::fmt::Display for InvalidNumber {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
InvalidNumber::NaN => write!(f, "Number is NaN"),
InvalidNumber::Zero => write!(f, "Number is zero"),
InvalidNumber::Negative => write!(f, "Number is negative"),
InvalidNumber::Positive => write!(f, "Number is positive"),
InvalidNumber::Infinite => write!(f, "Number is infinite"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidNumber {}

typed_floats_macros::generate_floats!();

macro_rules! add_const {
Expand Down
2 changes: 2 additions & 0 deletions typed_floats/tests/abs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(abs);
2 changes: 2 additions & 0 deletions typed_floats/tests/acos.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(acos);
2 changes: 2 additions & 0 deletions typed_floats/tests/acosh.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(acosh);
2 changes: 2 additions & 0 deletions typed_floats/tests/asin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(asin);
2 changes: 2 additions & 0 deletions typed_floats/tests/asinh.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(asinh);
2 changes: 2 additions & 0 deletions typed_floats/tests/atan.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(atan);
2 changes: 2 additions & 0 deletions typed_floats/tests/atan2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self_rhs!(atan2);
2 changes: 2 additions & 0 deletions typed_floats/tests/atanh.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(atanh);
2 changes: 2 additions & 0 deletions typed_floats/tests/cbrt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(cbrt);
2 changes: 2 additions & 0 deletions typed_floats/tests/ceil.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(ceil);
2 changes: 2 additions & 0 deletions typed_floats/tests/copysign.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self_rhs!(copysign);
2 changes: 2 additions & 0 deletions typed_floats/tests/cos.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(cos);
2 changes: 2 additions & 0 deletions typed_floats/tests/cosh.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(cosh);
2 changes: 2 additions & 0 deletions typed_floats/tests/div_euclid.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self_rhs!(div_euclid);
2 changes: 2 additions & 0 deletions typed_floats/tests/exp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(exp);
2 changes: 2 additions & 0 deletions typed_floats/tests/exp2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(exp2);
2 changes: 2 additions & 0 deletions typed_floats/tests/exp_m1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(exp_m1);
2 changes: 2 additions & 0 deletions typed_floats/tests/floor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(floor);
2 changes: 2 additions & 0 deletions typed_floats/tests/fract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(fract);
16 changes: 16 additions & 0 deletions typed_floats/tests/hypot.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self_rhs!(hypot);

#[test]
fn test_hypot() {
let a = 3.0f64;
let b = 4.0f64;
let c = 5.0f64;

let a = NonNaN::try_from(a).unwrap();
let b = NonNaN::try_from(b).unwrap();
let c = Positive::try_from(c).unwrap();

let result = a.hypot(b);
assert_eq!(result, c);
}
2 changes: 2 additions & 0 deletions typed_floats/tests/ln.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(ln);
2 changes: 2 additions & 0 deletions typed_floats/tests/ln_1p.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(ln_1p);
2 changes: 2 additions & 0 deletions typed_floats/tests/log10.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use typed_floats::*;

typed_floats_macros::generate_tests_self!(log10);
Loading

0 comments on commit 2b8008a

Please sign in to comment.