-
Notifications
You must be signed in to change notification settings - Fork 186
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 lightweight wasm support to rust-decimal
#650
base: master
Are you sure you want to change the base?
Changes from 6 commits
7b3d2b6
b7c6343
62d812e
79c76a6
9a870a1
635be6c
9f02353
30b9061
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
[package] | ||
authors = ["Paul Mason <[email protected]>"] | ||
build = "build.rs" | ||
categories = ["science","mathematics","data-structures"] | ||
categories = ["science", "mathematics", "data-structures"] | ||
description = "Decimal number implementation written in pure Rust suitable for financial and fixed-precision calculations." | ||
documentation = "https://docs.rs/rust_decimal/" | ||
edition = "2021" | ||
exclude = [ "tests/generated/*" ] | ||
keywords = ["decimal","financial","fixed","precision","number"] | ||
exclude = ["tests/generated/*"] | ||
keywords = ["decimal", "financial", "fixed", "precision", "number"] | ||
license = "MIT" | ||
name = "rust_decimal" | ||
readme = "./README.md" | ||
|
@@ -17,6 +17,7 @@ version = "1.35.0" | |
[package.metadata.docs.rs] | ||
all-features = true | ||
rustdoc-args = ["--cfg", "docsrs"] | ||
targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"] | ||
|
||
[dependencies] | ||
arbitrary = { default-features = false, optional = true, version = "1.0" } | ||
|
@@ -37,19 +38,27 @@ serde = { default-features = false, optional = true, version = "1.0" } | |
serde_json = { default-features = false, optional = true, version = "1.0" } | ||
tokio-postgres = { default-features = false, optional = true, version = "0.7" } | ||
|
||
[target.'cfg(target_arch = "wasm32")'.dependencies] | ||
wasm-bindgen = { default-features = false, version = "0.2" } | ||
Comment on lines
+41
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good idea to declare these things as optional even with the cfg guard, including In their case it may have been due to it being a default-on feature; I admit I havent checkout out this branch and run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call out and thank you for raising this @robjtede Admittedly, I'm still getting up to speed on this so it's good to find out what not to do also! |
||
|
||
[dev-dependencies] | ||
bincode = { default-features = false, version = "1.0" } | ||
bytes = { default-features = false, version = "1.0" } | ||
criterion = { default-features = false, version = "0.5" } | ||
csv = "1" | ||
futures = { default-features = false, version = "0.3" } | ||
rand = { default-features = false, features = ["getrandom"], version = "0.8" } | ||
rust_decimal_macros = { default-features = false, version = "1.33" } | ||
serde = { default-features = false, features = ["derive"], version = "1.0" } | ||
serde_json = "1.0" | ||
tokio = { default-features = false, features = ["macros", "rt-multi-thread", "test-util"], version = "1.0" } | ||
version-sync = { default-features = false, features = ["html_root_url_updated", "markdown_deps_updated"], version = "0.9" } | ||
|
||
[target.'cfg(target_arch = "wasm32")'.dev-dependencies] | ||
wasm-bindgen = { default-features = false, version = "0.2" } | ||
|
||
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] | ||
bincode = { default-features = false, version = "1.0" } | ||
bytes = { default-features = false, version = "1.0" } | ||
futures = { default-features = false, version = "0.3" } | ||
rand = { default-features = false, features = ["getrandom"], version = "0.8" } | ||
postgres = { default-features = false, version = "0.19" } | ||
tokio = { default-features = false, features = ["macros", "rt-multi-thread", "test-util"], version = "1.0" } | ||
tokio-postgres = { default-features = false, version = "0.7" } | ||
|
||
[features] | ||
|
@@ -77,15 +86,15 @@ rkyv = ["dep:rkyv"] | |
rkyv-safe = ["rkyv/validation"] | ||
rocket-traits = ["dep:rocket"] | ||
rust-fuzz = ["dep:arbitrary"] | ||
serde = ["dep:serde"] | ||
serde = ["dep:serde", "wasm-bindgen/serde"] | ||
serde-arbitrary-precision = ["serde-with-arbitrary-precision"] | ||
serde-bincode = ["serde-str"] # Backwards compatability | ||
serde-float = ["serde-with-float"] | ||
serde-str = ["serde-with-str"] | ||
serde-with-arbitrary-precision = ["serde", "serde_json/arbitrary_precision", "serde_json/std"] | ||
serde-with-float = ["serde"] | ||
serde-with-str = ["serde"] | ||
std = ["arrayvec/std", "borsh?/std", "bytes?/std", "rand?/std", "rkyv?/std", "serde?/std", "serde_json?/std"] | ||
std = ["arrayvec/std", "wasm-bindgen/std", "borsh?/std", "bytes?/std", "rand?/std", "rkyv?/std", "serde?/std", "serde_json?/std"] | ||
tokio-pg = ["db-tokio-postgres"] # Backwards compatability | ||
|
||
[[bench]] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use num_traits::{FromPrimitive, ToPrimitive}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use crate::Decimal; | ||
|
||
#[wasm_bindgen] | ||
impl Decimal { | ||
/// Returns a new `Decimal` object instance by converting a primitive number. | ||
#[wasm_bindgen(js_name = fromNumber)] | ||
#[must_use] | ||
pub fn from_number(value: f64) -> Option<Decimal> { | ||
Decimal::from_f64(value) | ||
} | ||
|
||
/// Returns the value of this `Decimal` converted to a primitive number. | ||
/// | ||
/// # Caution | ||
/// At the time of writing this implementation the conversion from `Decimal` to `f64` cannot | ||
/// fail. To prevent undefined behavior in case the underlying implementation changes `f64::NAN` | ||
/// is returned as a stable fallback value. | ||
#[wasm_bindgen(js_name = toNumber)] | ||
#[must_use] | ||
pub fn to_number(&self) -> f64 { | ||
self.to_f64().unwrap_or(f64::NAN) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: We need to make the tests adhere to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(So the pass in the CI is not accurate)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After looking a bit further into this, I think this is the wrong approach. I believe a wasm target needs to be handled completely separately.