Skip to content

Commit 4ee19c6

Browse files
authored
Add rand support (#517)
* Add rand support * Rustfmt
1 parent 98d8057 commit 4ee19c6

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ jobs:
113113
command: make
114114
args: test-macros
115115

116+
- name: Run `rand` tests
117+
uses: actions-rs/cargo@v1
118+
with:
119+
command: make
120+
args: test-rand
121+
116122
check_style:
117123
name: Check file formatting and style
118124
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ bytes = { default-features = false, optional = true, version = "1.0" }
3030
diesel = { default-features = false, optional = true, version = "1.4" }
3131
num-traits = { default-features = false, features = ["i128"], version = "0.2" }
3232
postgres = { default-features = false, optional = true, version = "0.19" }
33+
rand = { default-features = false, optional = true, version = "0.8" }
3334
rocket = { default-features = false, optional = true, version = "0.5.0-rc.1" }
3435
serde = { default-features = false, optional = true, version = "1.0" }
3536
serde_json = { default-features = false, optional = true, version = "1.0" }
@@ -41,6 +42,7 @@ bytes = { default-features = false, version = "1.0" }
4142
criterion = { default-features = false, version = "0.3" }
4243
csv = "1"
4344
futures = { default-features = false, version = "0.3" }
45+
rand = { default-features = false, features = ["getrandom"], version = "0.8" }
4446
rust_decimal_macros = { path = "macros" } # This should be ok since it's just for tests
4547
serde = { default-features = false, features = ["derive"], version = "1.0" }
4648
serde_json = "1.0"

Makefile.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,7 @@ args = ["test", "--workspace", "--tests", "--features=serde-with-str", "serde",
251251
command = "cargo"
252252
args = ["test", "--workspace", "--features=borsh"]
253253

254+
[tasks.test-rand]
255+
command = "cargo"
256+
args = ["test", "--workspace", "--features=rand"]
257+

README.md

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

143+
### `rand`
144+
145+
Implements `rand::distributions::Distribution<Decimal>` to allow the creation of random instances.
146+
143147
### `rocket-traits`
144148

145149
Enable support for Rocket forms by implementing the `FromFormField` trait.

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod constants;
77
mod decimal;
88
mod error;
99
mod ops;
10+
mod rand;
1011
mod str;
1112

1213
// We purposely place this here for documentation ordering

src/rand.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![cfg(feature = "rand")]
2+
3+
use crate::Decimal;
4+
use rand::{
5+
distributions::{Distribution, Standard},
6+
Rng,
7+
};
8+
9+
impl Distribution<Decimal> for Standard {
10+
fn sample<R>(&self, rng: &mut R) -> Decimal
11+
where
12+
R: Rng + ?Sized,
13+
{
14+
Decimal::from_parts(
15+
rng.next_u32(),
16+
rng.next_u32(),
17+
rng.next_u32(),
18+
rng.gen(),
19+
rng.next_u32(),
20+
)
21+
}
22+
}
23+
24+
#[test]
25+
fn has_random_decimal_instances() {
26+
let mut rng = rand::rngs::OsRng;
27+
let random: [Decimal; 32] = rng.gen();
28+
assert!(random.windows(2).any(|slice| { slice[0] != slice[1] }));
29+
}

0 commit comments

Comments
 (0)