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

Add #[no_std] and WebAssembly support #21

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 42 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ concurrency:
cancel-in-progress: true

jobs:
wasm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Install
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

- run: cd blurhash-rs-wasm
- run: cargo test
- run: wasm-pack test --headless --chrome -- --package blurhash-rs-wasm
- run: wasm-pack test --headless --firefox -- --package blurhash-rs-wasm

build_and_test:
name: cargo test
runs-on: ubuntu-latest
Expand All @@ -24,9 +37,29 @@ jobs:
matrix:
toolchain: ["stable", "beta"]
coverage: [false]
features: [
# With std
"std,gdk-pixbuf",
"std,gdk-pixbuf,fast-linear-to-srgb",
"std,image",
"std,image,fast-linear-to-srgb",
"std,fast-linear-to-srgb",
"std",
]
include:
# Coverage on nightly
- toolchain: "nightly"
coverage: true
features: ""

# no-std build and tests
- toolchain: "nightly"
coverage: false
features: "fast-linear-to-srgb"

- toolchain: "nightly"
coverage: false
features: ""
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -47,18 +80,25 @@ jobs:

- name: Run tests with default features
uses: actions-rs/cargo@v1
if: ${{ !matrix.coverage }}
if: ${{ !matrix.coverage && (matrix.features == '' || matrix.features == 'std') }}
with:
command: test
args: --all-targets --no-fail-fast

- name: Run tests with all features
uses: actions-rs/cargo@v1
if: ${{ !matrix.coverage }}
if: ${{ !matrix.coverage && (matrix.features == '' || matrix.features == 'std') }}
with:
command: test
args: --all-targets --no-fail-fast --all-features

- name: "Run tests with features: ${{ matrix.features }}"
uses: actions-rs/cargo@v1
if: ${{ !matrix.coverage }}
with:
command: test
args: --all-targets --no-fail-fast --no-default-features --features "${{ matrix.features }}"

- name: Run doc tests
uses: actions-rs/cargo@v1
if: ${{ !matrix.coverage }}
Expand Down
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ edition = "2018"
[dependencies]
image = { version = ">= 0.23, <= 0.25", optional = true }
gdk-pixbuf = { version = ">= 0.18, <= 0.19", optional = true }
num-traits = { version = "0.2", default-features = false, features = ["libm"] }

[dev-dependencies]
image = ">= 0.23, <= 0.25"
criterion = "0.5"
proptest = "1"

[features]
default = ["fast-linear-to-srgb"]
default = ["fast-linear-to-srgb", "std"]
std = []
image = [ "dep:image" ]
gdk-pixbuf = [ "dep:gdk-pixbuf" ]
fast-linear-to-srgb = []
Expand All @@ -32,3 +34,12 @@ harness = false
[[bench]]
name = "encode"
harness = false

[workspace]
members = [
"blurhash-rs-wasm",
]

[profile.release] #.package.blurhash-rs-wasm]
# Tell `rustc` to optimize for small code size.
opt-level = "z"
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,27 @@ If this overhead is problematic, you can disable it by instead specifying the fo

```toml
[dependencies]
blurhash = { version = "0.2.3", default-features = false }
blurhash = { version = "0.2.3", default-features = false, features = ["std"] }
```

Blurhash can also run in `no_std` environments (with `alloc`) by disabling the default features:

```toml
[dependencies]
blurhash = { version = "0.2.4", default-features = false, features = ["fast-linear-to-srgb"] }
```

Blurhash requires Rust 1.82 or later when using `no_std`.

Blurhash can also be run with WebAssembly by enabling the `wasm` feature:

```toml
[dependencies]
blurhash = { version = "0.2.4", features = ["wasm"] }
```

The `wasm` feature is compatible with `no_std`.

### Encoding
```rust
use blurhash::encode;
Expand Down
6 changes: 6 additions & 0 deletions blurhash-rs-wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log
69 changes: 69 additions & 0 deletions blurhash-rs-wasm/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
language: rust
sudo: false

cache: cargo

matrix:
include:

# Builds with wasm-pack.
- rust: beta
env: RUST_BACKTRACE=1
addons:
firefox: latest
chrome: stable
before_script:
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
- (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate)
- cargo install-update -a
- curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f
script:
- cargo generate --git . --name testing
# Having a broken Cargo.toml (in that it has curlies in fields) anywhere
# in any of our parent dirs is problematic.
- mv Cargo.toml Cargo.toml.tmpl
- cd testing
- wasm-pack build
- wasm-pack test --chrome --firefox --headless

# Builds on nightly.
- rust: nightly
env: RUST_BACKTRACE=1
before_script:
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
- (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate)
- cargo install-update -a
- rustup target add wasm32-unknown-unknown
script:
- cargo generate --git . --name testing
- mv Cargo.toml Cargo.toml.tmpl
- cd testing
- cargo check
- cargo check --target wasm32-unknown-unknown
- cargo check --no-default-features
- cargo check --target wasm32-unknown-unknown --no-default-features
- cargo check --no-default-features --features console_error_panic_hook
- cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook
- cargo check --no-default-features --features "console_error_panic_hook wee_alloc"
- cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc"

# Builds on beta.
- rust: beta
env: RUST_BACKTRACE=1
before_script:
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
- (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate)
- cargo install-update -a
- rustup target add wasm32-unknown-unknown
script:
- cargo generate --git . --name testing
- mv Cargo.toml Cargo.toml.tmpl
- cd testing
- cargo check
- cargo check --target wasm32-unknown-unknown
- cargo check --no-default-features
- cargo check --target wasm32-unknown-unknown --no-default-features
- cargo check --no-default-features --features console_error_panic_hook
- cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook
# Note: no enabling the `wee_alloc` feature here because it requires
# nightly for now.
27 changes: 27 additions & 0 deletions blurhash-rs-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "blurhash-rs-wasm"
version = "0.1.0"
authors = ["Ruben De Smet <[email protected]>", "Raincal <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook", "fast-linear-to-srgb"]
fast-linear-to-srgb = ["blurhash/fast-linear-to-srgb"]

[dependencies]
wasm-bindgen = "0.2.84"
js-sys = { version = "0.3" }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }

blurhash = { path = "../", default-features = false, features = []}

[dev-dependencies]
wasm-bindgen-test = "0.3.34"
Loading
Loading