From 374ab91dcdfaa81f2ee4d8f98d117c755aad0af7 Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Tue, 13 Dec 2022 19:40:05 +0100 Subject: [PATCH 01/11] no_std compat --- Cargo.toml | 5 +++++ src/lib.rs | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 727e42c..bf9efd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [package] +edition = "2021" name = "color_quant" license = "MIT" version = "1.1.0" @@ -6,3 +7,7 @@ authors = ["nwin "] readme = "README.md" description = "Color quantization library to reduce n colors to 256 colors." repository = "https://github.com/image-rs/color_quant.git" + +[dependencies] +# libm: "Uncomment if you wish to use `Float` and `Real` without `std`" +num-traits = { version = "0.2", default-features = false, features = ["libm"] } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 13c08cb..1635ba8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,10 +69,20 @@ that this copyright notice remain intact. //! let color_map = nq.color_map_rgba(); //! ``` +// #![no_std] +#![cfg_attr(not(feature = "std"), no_std)] + +#[macro_use] +extern crate alloc; + +#[allow(unused_imports)] +use num_traits::float::FloatCore; + mod math; use crate::math::clamp; -use std::cmp::{max, min}; +use alloc::vec::Vec; +use core::cmp::{max, min}; const CHANNELS: usize = 4; @@ -279,7 +289,7 @@ impl NeuQuant { /// for frequently chosen neurons, freq[i] is high and bias[i] is negative /// bias[i] = gamma*((1/self.netsize)-freq[i]) fn contest(&mut self, b: f64, g: f64, r: f64, a: f64) -> i32 { - use std::f64; + use core::f64; let mut bestd = f64::MAX; let mut bestbiasd: f64 = bestd; @@ -411,7 +421,7 @@ impl NeuQuant { q = self.colormap[smallpos]; // swap p (i) and q (smallpos) entries if i != smallpos { - ::std::mem::swap(&mut p, &mut q); + ::core::mem::swap(&mut p, &mut q); self.colormap[i] = p; self.colormap[smallpos] = q; } @@ -433,7 +443,7 @@ impl NeuQuant { } /// Search for best matching color fn search_netindex(&self, b: u8, g: u8, r: u8, a: u8) -> usize { - let mut best_dist = std::i32::MAX; + let mut best_dist = core::i32::MAX; let first_guess = self.netindex[g as usize]; let mut best_pos = first_guess; let mut i = best_pos; From a0bd229a1785a4057feabd91d3cfc938077abc41 Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Mon, 19 Dec 2022 14:38:49 +0100 Subject: [PATCH 02/11] cargo: feature-gate "libm" Same principle as nalgebra,simba,etc --- Cargo.toml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bf9efd3..24efbba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,7 @@ repository = "https://github.com/image-rs/color_quant.git" [dependencies] # libm: "Uncomment if you wish to use `Float` and `Real` without `std`" -num-traits = { version = "0.2", default-features = false, features = ["libm"] } \ No newline at end of file +num-traits = { version = "0.2", default-features = false } + +[features] +libm = ["num-traits/libm"] \ No newline at end of file From 9496fb436d37b9a8fb3656a24611352766a47799 Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Mon, 19 Dec 2022 16:13:51 +0100 Subject: [PATCH 03/11] cargo: remove feature "libm" ie revert previous commit --- Cargo.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 24efbba..14a9307 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,4 @@ description = "Color quantization library to reduce n colors to 256 colors." repository = "https://github.com/image-rs/color_quant.git" [dependencies] -# libm: "Uncomment if you wish to use `Float` and `Real` without `std`" num-traits = { version = "0.2", default-features = false } - -[features] -libm = ["num-traits/libm"] \ No newline at end of file From 4bf3c4bf4f307891bf53145c3165016d38200fde Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Mon, 19 Dec 2022 16:21:25 +0100 Subject: [PATCH 04/11] cargo: depends on "num-traits" only for no_std --- Cargo.toml | 7 ++++++- src/lib.rs | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 14a9307..9017dc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,9 @@ description = "Color quantization library to reduce n colors to 256 colors." repository = "https://github.com/image-rs/color_quant.git" [dependencies] -num-traits = { version = "0.2", default-features = false } +num-traits = { version = "0.2", default-features = false, optional = true } + +[features] +default = ["std"] +std = [] +alloc = ["num-traits"] \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 1635ba8..12e36ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,6 +75,7 @@ that this copyright notice remain intact. #[macro_use] extern crate alloc; +#[cfg(not(feature = "std"))] #[allow(unused_imports)] use num_traits::float::FloatCore; From f5193af328bf1abae7984bef856abb1fba462d1f Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Mon, 13 Feb 2023 15:47:59 +0100 Subject: [PATCH 05/11] cleanup: fix "edition" To keep compat with Rust 1.34 --- Cargo.toml | 3 ++- src/lib.rs | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9017dc2..53f75b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [package] -edition = "2021" +# NOTE: 2018 is needed else "maybe a missing crate `core`" +edition = "2018" name = "color_quant" license = "MIT" version = "1.1.0" diff --git a/src/lib.rs b/src/lib.rs index 12e36ae..9ad2f67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,7 +69,6 @@ that this copyright notice remain intact. //! let color_map = nq.color_map_rgba(); //! ``` -// #![no_std] #![cfg_attr(not(feature = "std"), no_std)] #[macro_use] From 8eeefc8b2fc2eae875b4c56c5b57763c11066db4 Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Mon, 13 Feb 2023 16:06:48 +0100 Subject: [PATCH 06/11] ci: matrix: test "default" and "alloc" features --- .github/workflows/rust.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index abc9f37..c52260f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,17 +10,17 @@ jobs: strategy: matrix: rust: ["1.34.2", stable, beta, nightly] - command: [build, test] + features: ["default", "alloc"] steps: - uses: actions/checkout@v2 - run: rustup default ${{ matrix.rust }} - name: build run: > - cargo build --verbose + cargo build --no-default-features --features=${{ matrix.features }} --verbose - name: test if: ${{ matrix.rust != '1.34.2' }} run: > - cargo test --tests --benches + cargo test --no-default-features --features=${{ matrix.features }} --tests --benches # TODO: add criterion benchmarks? rustfmt: runs-on: ubuntu-latest From 0bdf6cbaa6daa39322a5c3eac51fa775a6d0deaf Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Thu, 2 Mar 2023 12:24:06 +0100 Subject: [PATCH 07/11] rename feature "alloc" -> "num-traits" - update #[cfg] logic in lib.rs - ci: add "no-std-check" with "features=num-traits" --- .github/workflows/rust.yml | 19 ++++++++++++++++--- Cargo.toml | 2 +- src/lib.rs | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c52260f..1108880 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -9,8 +9,9 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: ["1.34.2", stable, beta, nightly] - features: ["default", "alloc"] + # alloc crate requires Rust 1.36 + rust: ["1.36", stable, beta, nightly] + features: ["default", "num-traits"] steps: - uses: actions/checkout@v2 - run: rustup default ${{ matrix.rust }} @@ -18,7 +19,7 @@ jobs: run: > cargo build --no-default-features --features=${{ matrix.features }} --verbose - name: test - if: ${{ matrix.rust != '1.34.2' }} + if: ${{ matrix.rust != '1.36' }} run: > cargo test --no-default-features --features=${{ matrix.features }} --tests --benches # TODO: add criterion benchmarks? @@ -36,3 +37,15 @@ jobs: with: command: fmt args: -- --check + + no_std_check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + - name: build + run: > + cargo no-std-check --no-default-features --features=num-traits diff --git a/Cargo.toml b/Cargo.toml index 53f75b3..0e09b1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,4 @@ num-traits = { version = "0.2", default-features = false, optional = true } [features] default = ["std"] std = [] -alloc = ["num-traits"] \ No newline at end of file +num-traits = ["dep:num-traits"] \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 9ad2f67..6aa2bed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,7 +74,7 @@ that this copyright notice remain intact. #[macro_use] extern crate alloc; -#[cfg(not(feature = "std"))] +#[cfg(all(feature = "num-traits", not(feature = "std")))] #[allow(unused_imports)] use num_traits::float::FloatCore; From 21adadf1273070bef91849210f9addbcd764c31a Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Thu, 2 Mar 2023 13:54:21 +0100 Subject: [PATCH 08/11] cargo: bump min Rust to 1.56 --- .github/workflows/rust.yml | 3 +-- Cargo.toml | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 1108880..830506f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,7 +10,7 @@ jobs: strategy: matrix: # alloc crate requires Rust 1.36 - rust: ["1.36", stable, beta, nightly] + rust: ["1.56", stable, beta, nightly] features: ["default", "num-traits"] steps: - uses: actions/checkout@v2 @@ -19,7 +19,6 @@ jobs: run: > cargo build --no-default-features --features=${{ matrix.features }} --verbose - name: test - if: ${{ matrix.rust != '1.36' }} run: > cargo test --no-default-features --features=${{ matrix.features }} --tests --benches # TODO: add criterion benchmarks? diff --git a/Cargo.toml b/Cargo.toml index 0e09b1e..c2d1bd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] # NOTE: 2018 is needed else "maybe a missing crate `core`" -edition = "2018" +edition = "2021" name = "color_quant" license = "MIT" version = "1.1.0" @@ -8,6 +8,7 @@ authors = ["nwin "] readme = "README.md" description = "Color quantization library to reduce n colors to 256 colors." repository = "https://github.com/image-rs/color_quant.git" +rust-version = "1.56" [dependencies] num-traits = { version = "0.2", default-features = false, optional = true } From ee797f26a1338a1b25531df1bfa3aad73e25dde8 Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Thu, 2 Mar 2023 14:14:13 +0100 Subject: [PATCH 09/11] cargo: remove "dep:" prefix --- Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c2d1bd3..0880d7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,4 @@ num-traits = { version = "0.2", default-features = false, optional = true } [features] default = ["std"] -std = [] -num-traits = ["dep:num-traits"] \ No newline at end of file +std = [] \ No newline at end of file From ecae1305e8e44666e8b38da5b5a0c0c0409c43c7 Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Thu, 2 Mar 2023 14:17:07 +0100 Subject: [PATCH 10/11] add README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 0644ad6..85b7b08 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,7 @@ quantization algorithm by Anthony Dekker. let indixes: Vec = data.chunks(4).map(|pix| nq.index_of(pix) as u8).collect(); let color_map = nq.color_map_rgba(); +#### no_std + +You should be able to use this crate in `no_std` environment e.g.: +`color_quant = { version = "1.1", default-features = false, features = ['num-traits'] }` \ No newline at end of file From fbf3d237f92828524e55c34d0792db9d7f4ee6dc Mon Sep 17 00:00:00 2001 From: Nathan Prat Date: Thu, 2 Mar 2023 15:25:40 +0100 Subject: [PATCH 11/11] ci: add missing install "cargo-no-std-check" --- .github/workflows/rust.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 830506f..c7d9381 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -46,5 +46,6 @@ jobs: toolchain: nightly override: true - name: build - run: > - cargo no-std-check --no-default-features --features=num-traits + run: | + cargo install cargo-no-std-check + cargo no-std-check --no-default-features --features=num-traits