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

Introduce wasm_bindgen support and expose a toNumber method for use in JavaScript #614

Closed
Closed
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rocket = { default-features = false, optional = true, version = "0.5.0-rc.3" }
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" }
wasm-bindgen = { default-features = false, optional = true, version = "0.2" }

[dev-dependencies]
bincode = { default-features = false, version = "1.0" }
Expand Down Expand Up @@ -81,6 +82,7 @@ serde-with-float = ["serde"]
serde-with-str = ["serde"]
std = ["arrayvec/std", "borsh?/std", "bytes?/std", "rand?/std", "rkyv?/std", "serde?/std", "serde_json?/std"]
tokio-pg = ["db-tokio-postgres"] # Backwards compatability
wasm-bindgen = ["dep:wasm-bindgen"]

[[bench]]
harness = false
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ assert_eq!(total, dec!(27.26));
* [rocket-traits](#rocket-traits)
* [rust-fuzz](#rust-fuzz)
* [std](#std)
* [wasm-bindgen](#wasm-bindgen)

**Database**

Expand Down Expand Up @@ -297,6 +298,12 @@ pub struct OptionArbitraryExample {
Enable `std` library support. This is enabled by default, however in the future will be opt in. For now, to support `no_std`
libraries, this crate can be compiled with `--no-default-features`.

### `wasm-bindgen`

Enable [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) support which makes `Decimal` compatible with the
`wasm_bindgen` attribute macro and exposes `fromNumber()` and `toNumber()` methods to convert between `Decimal` and
the primitive `number` type across boundaries.

## Building

Please refer to the [Build document](BUILD.md) for more information on building and testing Rust Decimal.
Expand Down
3 changes: 3 additions & 0 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use num_traits::float::FloatCore;
use num_traits::{FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};
#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::wasm_bindgen;

/// The smallest value that can be represented by this decimal type.
const MIN: Decimal = Decimal {
Expand Down Expand Up @@ -121,6 +123,7 @@ pub struct UnpackedDecimal {
archive_attr(derive(Clone, Copy, Debug))
)]
#[cfg_attr(feature = "rkyv-safe", archive(check_bytes))]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct Decimal {
// Bits 0-15: unused
// Bits 16-23: Contains "e", a value between 0-28 that indicates the scale
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ mod serde;
)
))]
pub mod serde;
#[cfg(feature = "wasm-bindgen")]
pub mod wasm;

pub use decimal::{Decimal, RoundingStrategy};
pub use error::Error;
Expand Down
21 changes: 21 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use num_traits::{FromPrimitive, ToPrimitive};
use wasm_bindgen::prelude::wasm_bindgen;

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.
#[wasm_bindgen(js_name = toNumber)]
#[must_use]
pub fn to_number(&self) -> f64 {
self.to_f64().unwrap_or(f64::NAN)
}
}
Loading