Skip to content

Commit

Permalink
Add rand support (#517)
Browse files Browse the repository at this point in the history
* Add rand support
* Rustfmt
  • Loading branch information
c410-f3r committed May 28, 2022
1 parent 98d8057 commit 4ee19c6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ jobs:
command: make
args: test-macros

- name: Run `rand` tests
uses: actions-rs/cargo@v1
with:
command: make
args: test-rand

check_style:
name: Check file formatting and style
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ bytes = { default-features = false, optional = true, version = "1.0" }
diesel = { default-features = false, optional = true, version = "1.4" }
num-traits = { default-features = false, features = ["i128"], version = "0.2" }
postgres = { default-features = false, optional = true, version = "0.19" }
rand = { default-features = false, optional = true, version = "0.8" }
rocket = { default-features = false, optional = true, version = "0.5.0-rc.1" }
serde = { default-features = false, optional = true, version = "1.0" }
serde_json = { default-features = false, optional = true, version = "1.0" }
Expand All @@ -41,6 +42,7 @@ bytes = { default-features = false, version = "1.0" }
criterion = { default-features = false, version = "0.3" }
csv = "1"
futures = { default-features = false, version = "0.3" }
rand = { default-features = false, features = ["getrandom"], version = "0.8" }
rust_decimal_macros = { path = "macros" } # This should be ok since it's just for tests
serde = { default-features = false, features = ["derive"], version = "1.0" }
serde_json = "1.0"
Expand Down
4 changes: 4 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,7 @@ args = ["test", "--workspace", "--tests", "--features=serde-with-str", "serde",
command = "cargo"
args = ["test", "--workspace", "--features=borsh"]

[tasks.test-rand]
command = "cargo"
args = ["test", "--workspace", "--features=rand"]

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ Please note that `ln` and `log10` will panic on invalid input with `checked_ln`
to curb against this. When the `maths` feature was first developed the library would return `0` on invalid input. To re-enable this
non-panicking behavior, please use the feature: `maths-nopanic`.

### `rand`

Implements `rand::distributions::Distribution<Decimal>` to allow the creation of random instances.

### `rocket-traits`

Enable support for Rocket forms by implementing the `FromFormField` trait.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod constants;
mod decimal;
mod error;
mod ops;
mod rand;
mod str;

// We purposely place this here for documentation ordering
Expand Down
29 changes: 29 additions & 0 deletions src/rand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![cfg(feature = "rand")]

use crate::Decimal;
use rand::{
distributions::{Distribution, Standard},
Rng,
};

impl Distribution<Decimal> for Standard {
fn sample<R>(&self, rng: &mut R) -> Decimal
where
R: Rng + ?Sized,
{
Decimal::from_parts(
rng.next_u32(),
rng.next_u32(),
rng.next_u32(),
rng.gen(),
rng.next_u32(),
)
}
}

#[test]
fn has_random_decimal_instances() {
let mut rng = rand::rngs::OsRng;
let random: [Decimal; 32] = rng.gen();
assert!(random.windows(2).any(|slice| { slice[0] != slice[1] }));
}

0 comments on commit 4ee19c6

Please sign in to comment.