-
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
Open
ChristianIvicevic
wants to merge
8
commits into
paupino:master
Choose a base branch
from
ChristianIvicevic:wasm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7b3d2b6
Add wasm support to `rust-decimal`
ChristianIvicevic b7c6343
WIP
paupino 62d812e
Merge branch 'master' of github.com:paupino/rust-decimal into wasm
paupino 79c76a6
Merge branch 'master' of github.com:paupino/rust-decimal into wasm
paupino 9a870a1
Include wasm32 in targets
paupino 635be6c
Remove explict build step for WASM
paupino 9f02353
Test refactor of test runners in makefile (incomplete)
paupino 30b9061
Use TEST_TARGET variable
paupino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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. | ||
/// | ||
/// # 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) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm just looking through how some other libraries introduce this and I think it needs to be done as a target as opposed to a simple feature. It could technically still be a feature however it'd need to be a no-op for std/core.
e.g.
There may be a requirement to also exclude some other libraries that aren't supported on wasm of course. However that'd require some investigation (e.g. what features are supported in combination with wasm)