Skip to content

Commit c81b3cd

Browse files
bors[bot]Ogeon
andcommitted
Merge #142
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 <[email protected]>
2 parents 6cb2cb0 + b449d5e commit c81b3cd

File tree

6 files changed

+71
-160
lines changed

6 files changed

+71
-160
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A Rust library that makes linear color calculations and conversion easy and acce
99

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

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

1414
## Cargo.toml Entries
1515

@@ -31,6 +31,7 @@ These features are enabled by default:
3131
These features are disabled by default:
3232

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

3536
### Without the standard library
3637

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

4547
## It's Never "Just RGB"

no_std_test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ default-features = false
1414
[dependencies.palette]
1515
path = "../palette"
1616
default-features = false
17-
features = ["named"]
17+
features = ["libm", "named"]

palette/Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ build = "build/main.rs"
1616
default = ["named_from_str", "std"]
1717
named_from_str = ["named", "phf", "phf_codegen", "std"]
1818
named = []
19-
std = ["approx/std", "num-traits/std"]
2019
serializing = ["serde", "std"]
2120

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

2525
[dependencies]
2626
palette_derive = {version = "0.4.1", path = "../palette_derive"}
2727
num-traits = {version = "0.2", default-features = false}
2828
approx = {version = "0.3", default-features = false}
29-
libm = "0.1"
29+
30+
[dependencies.libm]
31+
version = "0.1"
32+
optional = true
3033

3134
[dependencies.phf]
3235
version = "0.7"

palette/README.md

Lines changed: 0 additions & 152 deletions
This file was deleted.

palette/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../README.md

palette/src/float.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ pub use self::no_std_float_trait::Float;
1717

1818
#[cfg(not(feature = "std"))]
1919
mod no_std_float_trait {
20+
#[cfg(feature = "libm")]
2021
extern crate libm;
22+
#[cfg(feature = "libm")]
2123
use self::libm::{F32Ext, F64Ext};
2224

2325
use core::{f32, f64};
@@ -61,6 +63,7 @@ mod no_std_float_trait {
6163
fn round(self) -> Self;
6264
}
6365

66+
#[cfg(feature = "libm")]
6467
impl Float for f32 {
6568
fn sqrt(self) -> f32 {
6669
F32Ext::cbrt(self)
@@ -85,6 +88,7 @@ mod no_std_float_trait {
8588
}
8689
}
8790

91+
#[cfg(feature = "libm")]
8892
impl Float for f64 {
8993
fn sqrt(self) -> f64 {
9094
F64Ext::sqrt(self)
@@ -108,4 +112,57 @@ mod no_std_float_trait {
108112
F64Ext::round(self)
109113
}
110114
}
115+
116+
#[cfg(not(feature = "libm"))]
117+
impl Float for f32 {
118+
fn sqrt(self) -> f32 {
119+
panic!("need to select a float library for palette")
120+
}
121+
fn cbrt(self) -> f32 {
122+
panic!("need to select a float library for palette")
123+
}
124+
fn powf(self, other: f32) -> f32 {
125+
panic!("need to select a float library for palette")
126+
}
127+
fn sin(self) -> f32 {
128+
panic!("need to select a float library for palette")
129+
}
130+
fn cos(self) -> f32 {
131+
panic!("need to select a float library for palette")
132+
}
133+
fn atan2(self, other: f32) -> f32 {
134+
panic!("need to select a float library for palette")
135+
}
136+
fn round(self) -> f32 {
137+
panic!("need to select a float library for palette")
138+
}
139+
}
140+
141+
#[cfg(not(feature = "libm"))]
142+
impl Float for f64 {
143+
fn sqrt(self) -> f64 {
144+
panic!("need to select a float library for palette")
145+
}
146+
fn cbrt(self) -> f64 {
147+
panic!("need to select a float library for palette")
148+
}
149+
fn powf(self, other: f64) -> f64 {
150+
panic!("need to select a float library for palette")
151+
}
152+
fn sin(self) -> f64 {
153+
panic!("need to select a float library for palette")
154+
}
155+
fn cos(self) -> f64 {
156+
panic!("need to select a float library for palette")
157+
}
158+
fn atan2(self, other: f64) -> f64 {
159+
panic!("need to select a float library for palette")
160+
}
161+
fn round(self) -> f64 {
162+
panic!("need to select a float library for palette")
163+
}
164+
}
111165
}
166+
167+
#[cfg(not(any(feature = "std", feature = "libm")))]
168+
compile_error!("The palette crate needs a float library. Please enable the \"std\" or \"libm\" feature.");

scripts/test_features.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44
features=""
55

66
#Features that will always be activated
7-
required_features="strict"
7+
required_features="std strict"
88

99

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

0 commit comments

Comments
 (0)