Skip to content

Commit

Permalink
Merge #142
Browse files Browse the repository at this point in the history
142: Make libm optional r=Ogeon a=Ogeon

I decided to do this to avoid having it as an unused dependency and also to hopefully be compatible with a future release of `num_traits`, where they seem to be planning the same kind of construct, judging by rust-num/num-traits#99. A transition to using `num_trait` for both cases will hopefully be seamless for the users after this.

I think this also fixes #116.

Co-authored-by: Erik Hedvall <erikwhedvall@gmail.com>
  • Loading branch information
bors[bot] and Ogeon committed Aug 11, 2019
2 parents 6cb2cb0 + b449d5e commit c81b3cd
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 160 deletions.
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

0 comments on commit c81b3cd

Please sign in to comment.