From adac88cb38c08a7963bf1858bb0428ea30bb3d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Sun, 28 Jul 2024 18:40:52 +0200 Subject: [PATCH 1/7] feature gate 24 and 50 bit versions to enable user to save binary size --- Cargo.toml | 16 ++++++++++- src/lib.rs | 80 ++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7693c64..840814f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,18 @@ repository = "https://github.com/JSorngard/lambert_w" [dependencies] [dev-dependencies] -approx = "0.5.1" \ No newline at end of file +approx = "0.5.1" + +[features] +default = ["24", "50"] +# Enables the function versions with 50-bits of accuracy. +50 = [] +# Enables the function versions with 24-bits of accuracy. +24 = [] + +# docs.rs-specific configuration. Taken from . +[package.metadata.docs.rs] +# Document all features. +all-features = true +# Enable the docsrs configuration attribute on docs.rs +rustdoc-args = ["--cfg", "docsrs"] \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 3435938..b10f571 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,38 +9,62 @@ //! ## Examples //! //! Evaluate the principal branch of the Lambert W function to 50 bits of accuracy: -//! ``` -//! # use lambert_w::LambertW0Error; -//! use lambert_w::accurate::lambert_w_0; -//! use core::f64::consts::PI; -//! use approx::assert_abs_diff_eq; -//! -//! let w = lambert_w_0(PI)?; -//! -//! assert_abs_diff_eq!(w, 1.0736581947961492); -//! # Ok::<(), LambertW0Error>(()) -//! ``` +#![cfg_attr( + feature = "50", + doc = r##" +``` +# use lambert_w::LambertW0Error; +use lambert_w::accurate::lambert_w_0; +use core::f64::consts::PI; +use approx::assert_abs_diff_eq; + +let w = lambert_w_0(PI)?; +assert_abs_diff_eq!(w, 1.0736581947961492); +# Ok::<(), LambertW0Error>(()) +``` +"## +)] //! //! or to only 24 bits of accuracy, but with faster execution time: -//! ``` -//! # use lambert_w::LambertW0Error; -//! use lambert_w::fast::lambert_w_0; -//! use core::f64::consts::PI; -//! use approx::assert_abs_diff_eq; -//! -//! let w = lambert_w_0(PI)?; -//! -//! assert_abs_diff_eq!(w, 1.0736581947961492, epsilon = 1e-7); -//! # Ok::<(), LambertW0Error>(()) -//! ``` +#![cfg_attr( + feature = "24", + doc = r##" +``` +# use lambert_w::LambertW0Error; +use lambert_w::fast::lambert_w_0; +use core::f64::consts::PI; +use approx::assert_abs_diff_eq; + +let w = lambert_w_0(PI)?; + +assert_abs_diff_eq!(w, 1.0736581947961492, epsilon = 1e-7); +# Ok::<(), LambertW0Error>(()) +``` +"## +)] //! //! ## Speed-accuracy trade-off //! //! The 50-bit accurate versions in the [`accurate`] module are more accurate, but slightly slower, than the 24-bit accurate versions in the [`fast`] module. //! [`fast::lambert_w_0`] is around 15% faster than [`accurate::lambert_w_0`] and [`fast::lambert_w_m1`] is around 41% faster than [`accurate::lambert_w_m1`]. +//! +//! ## Feature flags +//! +//! You can disable one of these features to potentially save a little bit of binary size. +//! +//! `50` *(enabled by default)*: enables the function versions with 50-bits of accuracy. +//! +//! `24` *(enabled by default)*: enables the function versions with 24-bits of accuracy. + +#![cfg_attr(docsrs, feature(doc_auto_cfg))] + +#[cfg(not(any(feature = "50", feature = "24")))] +compile_error!("one or both of the '24' and '50' features must be enabled"); +#[cfg(feature = "50")] pub mod accurate; mod error; +#[cfg(feature = "24")] pub mod fast; pub use error::{LambertW0Error, LambertWm1Error, LambertWm1ErrorReason}; @@ -53,12 +77,13 @@ pub(crate) const X0: f64 = 0.606_530_659_712_633_4; #[cfg(test)] mod tets { - use super::{ - accurate::{lambert_w_0 as lambert_w_0_50, lambert_w_m1 as lambert_w_m1_50}, - fast::{lambert_w_0 as lambert_w_0_24, lambert_w_m1 as lambert_w_m1_24}, - }; + #[cfg(feature = "50")] + use super::accurate::{lambert_w_0 as lambert_w_0_50, lambert_w_m1 as lambert_w_m1_50}; + #[cfg(feature = "24")] + use super::fast::{lambert_w_0 as lambert_w_0_24, lambert_w_m1 as lambert_w_m1_24}; use approx::assert_abs_diff_eq; + #[cfg(feature = "50")] #[test] fn test_lambert_w_0_50() { assert_abs_diff_eq!( @@ -180,6 +205,7 @@ mod tets { ); } + #[cfg(feature = "24")] #[test] fn test_lambert_w_0_24() { assert_abs_diff_eq!( @@ -314,6 +340,7 @@ mod tets { ); } + #[cfg(feature = "50")] #[test] fn test_lambert_w_m1_50() { assert_abs_diff_eq!( @@ -380,6 +407,7 @@ mod tets { ); } + #[cfg(feature = "24")] #[test] fn test_lambert_w_m1_24() { assert_abs_diff_eq!( From 51929f4366b8432770d2eaaf97f0109ac19cdc40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Sun, 28 Jul 2024 18:42:42 +0200 Subject: [PATCH 2/7] space instead of - --- Cargo.toml | 4 ++-- src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 840814f..d049bc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,9 +17,9 @@ approx = "0.5.1" [features] default = ["24", "50"] -# Enables the function versions with 50-bits of accuracy. +# Enables the function versions with 50 bits of accuracy. 50 = [] -# Enables the function versions with 24-bits of accuracy. +# Enables the function versions with 24 bits of accuracy. 24 = [] # docs.rs-specific configuration. Taken from . diff --git a/src/lib.rs b/src/lib.rs index b10f571..578beee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,9 +52,9 @@ assert_abs_diff_eq!(w, 1.0736581947961492, epsilon = 1e-7); //! //! You can disable one of these features to potentially save a little bit of binary size. //! -//! `50` *(enabled by default)*: enables the function versions with 50-bits of accuracy. +//! `50` *(enabled by default)*: enables the function versions with 50 bits of accuracy. //! -//! `24` *(enabled by default)*: enables the function versions with 24-bits of accuracy. +//! `24` *(enabled by default)*: enables the function versions with 24 bits of accuracy. #![cfg_attr(docsrs, feature(doc_auto_cfg))] From a22f9228d34901ae1f593a838b11967d2fd7adb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Sun, 28 Jul 2024 18:43:45 +0200 Subject: [PATCH 3/7] note compile error --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 578beee..c69897f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,8 @@ assert_abs_diff_eq!(w, 1.0736581947961492, epsilon = 1e-7); //! `50` *(enabled by default)*: enables the function versions with 50 bits of accuracy. //! //! `24` *(enabled by default)*: enables the function versions with 24 bits of accuracy. +//! +//! It is a compile error to disable both features. #![cfg_attr(docsrs, feature(doc_auto_cfg))] From 26b70b98101f65f6667e9df47c9545498481d2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Sun, 28 Jul 2024 18:46:00 +0200 Subject: [PATCH 4/7] Only add in the test module if there's anything to test --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c69897f..3dc396c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,7 +77,7 @@ pub(crate) const Z0: f64 = -0.367_879_441_171_442_33; // 1/sqrt(e) pub(crate) const X0: f64 = 0.606_530_659_712_633_4; -#[cfg(test)] +#[cfg(all(test, any(feature = "24", feature = "50")))] mod tets { #[cfg(feature = "50")] use super::accurate::{lambert_w_0 as lambert_w_0_50, lambert_w_m1 as lambert_w_m1_50}; From e3b2751956f7e725044129c1b6e5233b0109b1da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Sun, 28 Jul 2024 18:52:07 +0200 Subject: [PATCH 5/7] Can we test this is CI? --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index a77ab93..e8fec27 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -41,7 +41,7 @@ jobs: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - name: test - run: cargo test --all-features --verbose + run: cargo test --all-features --verbose && cargo test --no-default-features -F 24 --verbose && cargo test --no-default-features -F 25 --verbose doc: runs-on: ubuntu-latest From 1cde45c877dc2e395352cbc6c9192e9fd2724606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Sun, 28 Jul 2024 18:52:49 +0200 Subject: [PATCH 6/7] 25 --> 50 --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e8fec27..c0a4818 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -41,7 +41,7 @@ jobs: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - name: test - run: cargo test --all-features --verbose && cargo test --no-default-features -F 24 --verbose && cargo test --no-default-features -F 25 --verbose + run: cargo test --all-features --verbose && cargo test --no-default-features -F 24 --verbose && cargo test --no-default-features -F 50 --verbose doc: runs-on: ubuntu-latest From 51ed2a6c658a7ea34ef5a4c9c0c613089b4cf9ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Sun, 28 Jul 2024 18:54:17 +0200 Subject: [PATCH 7/7] feature flags --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3dc396c..be590be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,7 @@ assert_abs_diff_eq!(w, 1.0736581947961492, epsilon = 1e-7); //! //! ## Feature flags //! -//! You can disable one of these features to potentially save a little bit of binary size. +//! You can disable one of these feature flags to potentially save a little bit of binary size. //! //! `50` *(enabled by default)*: enables the function versions with 50 bits of accuracy. //!