summaryrefslogtreecommitdiff
path: root/gen_random
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-14 13:08:08 -0500
committerpommicket <pommicket@gmail.com>2022-12-14 13:08:08 -0500
commit5a20cffba66caa71b495736f75031f69d09ba40b (patch)
tree92834986e8f8c7d1ab8947da52e649c90caac556 /gen_random
parent3265cb676c5c87fd624f59aaf3ca89d947df8cda (diff)
more genrandom
Diffstat (limited to 'gen_random')
-rw-r--r--gen_random/Cargo.lock45
-rw-r--r--gen_random/src/lib.rs88
2 files changed, 88 insertions, 45 deletions
diff --git a/gen_random/Cargo.lock b/gen_random/Cargo.lock
index b7e5b20..ad87cc3 100644
--- a/gen_random/Cargo.lock
+++ b/gen_random/Cargo.lock
@@ -12,20 +12,10 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "gen_random"
version = "0.1.0"
dependencies = [
- "gen_random_proc_macro",
"rand",
]
[[package]]
-name = "gen_random_proc_macro"
-version = "0.1.0"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
-[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -49,24 +39,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
-name = "proc-macro2"
-version = "1.0.47"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
-dependencies = [
- "unicode-ident",
-]
-
-[[package]]
-name = "quote"
-version = "1.0.21"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
-dependencies = [
- "proc-macro2",
-]
-
-[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -97,23 +69,6 @@ dependencies = [
]
[[package]]
-name = "syn"
-version = "1.0.105"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
-dependencies = [
- "proc-macro2",
- "quote",
- "unicode-ident",
-]
-
-[[package]]
-name = "unicode-ident"
-version = "1.0.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
-
-[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/gen_random/src/lib.rs b/gen_random/src/lib.rs
index f091ec3..b9cdb4f 100644
--- a/gen_random/src/lib.rs
+++ b/gen_random/src/lib.rs
@@ -1,5 +1,8 @@
extern crate rand;
use rand::Rng;
+use std::rc::Rc;
+use std::sync::Arc;
+use std::cell::{Cell, RefCell};
pub trait GenRandom: Sized {
fn gen_random(rng: &mut impl Rng) -> Self;
@@ -9,6 +12,14 @@ pub trait GenRandom: Sized {
}
}
+pub fn gen_random_vec<T: GenRandom>(rng: &mut impl Rng, len: usize) -> Vec<T> {
+ (0..len).map(|_| T::gen_random(rng)).collect()
+}
+
+pub fn gen_thread_random_vec<T: GenRandom>(len: usize) -> Vec<T> {
+ gen_random_vec(&mut rand::thread_rng(), len)
+}
+
impl GenRandom for f32 {
fn gen_random(rng: &mut impl Rng) -> Self {
rng.gen_range(0.0..1.0)
@@ -26,3 +37,80 @@ impl<T: GenRandom> GenRandom for Box<T> {
Box::new(T::gen_random(rng))
}
}
+
+impl<T: GenRandom> GenRandom for [T; 1] {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ [T::gen_random(rng)]
+ }
+}
+
+impl<T: GenRandom> GenRandom for [T; 2] {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ [T::gen_random(rng), T::gen_random(rng)]
+ }
+}
+
+impl<T: GenRandom> GenRandom for [T; 3] {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ [T::gen_random(rng), T::gen_random(rng), T::gen_random(rng)]
+ }
+}
+
+impl<T: GenRandom> GenRandom for [T; 4] {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ [T::gen_random(rng), T::gen_random(rng), T::gen_random(rng), T::gen_random(rng)]
+ }
+}
+
+impl<T: GenRandom> GenRandom for (T, T) {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ (T::gen_random(rng), T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for (T, T, T) {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ (T::gen_random(rng), T::gen_random(rng), T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for (T, T, T, T) {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ (T::gen_random(rng), T::gen_random(rng), T::gen_random(rng), T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for Rc<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ Self::new(T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for Arc<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ Self::new(T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for Cell<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ Self::new(T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for RefCell<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ Self::new(T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for Option<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ if rng.gen_range(0..2) == 0 {
+ None
+ } else {
+ Some(T::gen_random(rng))
+ }
+ }
+}
+