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

Make libm optional #142

Merged
merged 2 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A Rust library that makes linear color calculations and conversion easy and acce

[Released](https://docs.rs/palette/0.4.1/palette/)

[Master branch](https://ogeon.github.io/docs/palette/master/palette/index.html).
[Master branch](https://ogeon.github.io/docs/palette/master/palette/index.html)

## Cargo.toml Entries

Expand All @@ -31,6 +31,7 @@ These features are enabled by default:
These features are disabled by default:

* `"serializing"` - Enables color serializing and deserializing using `serde`.
* `"libm"` - Makes it use the `libm` floating point math library. It's only for when the `"std"` feature is disabled.

### Without the standard library

Expand All @@ -40,6 +41,7 @@ Here is an example `Cargo.toml` entry for using palette on `#![no_std]`:
[dependencies.palette]
version = "0.4"
default-features = false
features = ["libm"] # Makes it use libm instead of std for float math
```

## It's Never "Just RGB"
Expand Down
2 changes: 1 addition & 1 deletion no_std_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ default-features = false
[dependencies.palette]
path = "../palette"
default-features = false
features = ["named"]
features = ["libm", "named"]
11 changes: 7 additions & 4 deletions palette/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ build = "build/main.rs"
default = ["named_from_str", "std"]
named_from_str = ["named", "phf", "phf_codegen", "std"]
named = []
std = ["approx/std", "num-traits/std"]
serializing = ["serde", "std"]

#internal
strict = []
#ignore in feature test
std = ["approx/std", "num-traits/std"]
strict = [] # Only for CI internal testing

[dependencies]
palette_derive = {version = "0.4.1", path = "../palette_derive"}
num-traits = {version = "0.2", default-features = false}
approx = {version = "0.3", default-features = false}
libm = "0.1"

[dependencies.libm]
version = "0.1"
optional = true

[dependencies.phf]
version = "0.7"
Expand Down
152 changes: 0 additions & 152 deletions palette/README.md

This file was deleted.

1 change: 1 addition & 0 deletions palette/README.md
57 changes: 57 additions & 0 deletions palette/src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub use self::no_std_float_trait::Float;

#[cfg(not(feature = "std"))]
mod no_std_float_trait {
#[cfg(feature = "libm")]
extern crate libm;
#[cfg(feature = "libm")]
use self::libm::{F32Ext, F64Ext};

use core::{f32, f64};
Expand Down Expand Up @@ -61,6 +63,7 @@ mod no_std_float_trait {
fn round(self) -> Self;
}

#[cfg(feature = "libm")]
impl Float for f32 {
fn sqrt(self) -> f32 {
F32Ext::cbrt(self)
Expand All @@ -85,6 +88,7 @@ mod no_std_float_trait {
}
}

#[cfg(feature = "libm")]
impl Float for f64 {
fn sqrt(self) -> f64 {
F64Ext::sqrt(self)
Expand All @@ -108,4 +112,57 @@ mod no_std_float_trait {
F64Ext::round(self)
}
}

#[cfg(not(feature = "libm"))]
impl Float for f32 {
fn sqrt(self) -> f32 {
panic!("need to select a float library for palette")
}
fn cbrt(self) -> f32 {
panic!("need to select a float library for palette")
}
fn powf(self, other: f32) -> f32 {
panic!("need to select a float library for palette")
}
fn sin(self) -> f32 {
panic!("need to select a float library for palette")
}
fn cos(self) -> f32 {
panic!("need to select a float library for palette")
}
fn atan2(self, other: f32) -> f32 {
panic!("need to select a float library for palette")
}
fn round(self) -> f32 {
panic!("need to select a float library for palette")
}
}

#[cfg(not(feature = "libm"))]
impl Float for f64 {
fn sqrt(self) -> f64 {
panic!("need to select a float library for palette")
}
fn cbrt(self) -> f64 {
panic!("need to select a float library for palette")
}
fn powf(self, other: f64) -> f64 {
panic!("need to select a float library for palette")
}
fn sin(self) -> f64 {
panic!("need to select a float library for palette")
}
fn cos(self) -> f64 {
panic!("need to select a float library for palette")
}
fn atan2(self, other: f64) -> f64 {
panic!("need to select a float library for palette")
}
fn round(self) -> f64 {
panic!("need to select a float library for palette")
}
}
}

#[cfg(not(any(feature = "std", feature = "libm")))]
compile_error!("The palette crate needs a float library. Please enable the \"std\" or \"libm\" feature.");
4 changes: 2 additions & 2 deletions scripts/test_features.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
features=""

#Features that will always be activated
required_features="strict"
required_features="std strict"


#Find features
Expand All @@ -14,7 +14,7 @@ current_dependency=""
while read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "[features]" ]]; then
walking_features=true
elif [[ $walking_features == true ]] && [[ "$line" == "#internal" ]]; then
elif [[ $walking_features == true ]] && [[ "$line" == "#ignore in feature test" ]]; then
walking_features=false
elif [[ $walking_features == true ]] && echo "$line" | grep -E "^\[.*\]" > /dev/null; then
walking_features=false
Expand Down