From d7c22c011f7125a7a92713d67497b638dc1fdf7c Mon Sep 17 00:00:00 2001 From: David Stangl Date: Thu, 25 Apr 2024 15:19:31 +0200 Subject: [PATCH] Random sampling of `Time` and `Duration` --- Cargo.toml | 6 ++- Makefile | 4 +- src/lib.rs | 3 ++ src/rand.rs | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 src/rand.rs diff --git a/Cargo.toml b/Cargo.toml index 234c869..53ae613 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,14 @@ edition = "2021" version = "0.12.2" license = "MIT OR Apache-2.0" +[features] +rand = ["dep:rand"] + [dependencies] chrono = "0.4.31" derive_more = "0.99.17" lazy_static = "1.4.0" +rand = { version = "0.8.5", optional = true } regex = "1.10.2" serde = { version = "1.0.193", features = ["derive"], default-features = false } thiserror = "1.0.50" @@ -58,7 +62,7 @@ missing_const_for_fn = "warn" ### Pedantic pedantic = { level = "warn", priority = -1 } missing_errors_doc = "allow" # TODO -cast_precision_loss = "allow" +cast_precision_loss = "allow" cast_possible_truncation = "allow" cast_lossless = "allow" cast_possible_wrap = "allow" diff --git a/Makefile b/Makefile index 5c2a045..71001e1 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ tools/grcov/$(GRCOV_VERSION)/grcov: .PHONY: test test: - cargo test --workspace --all-targets + cargo test --workspace --all-targets --all-features cargo test --workspace --doc lint: @@ -77,4 +77,4 @@ coverage-html: tools/grcov/$(GRCOV_VERSION)/grcov --ignore-not-existing \ --keep-only=/src/**/*.rs \ -o ./coverage && \ - open coverage/index.html \ No newline at end of file + open coverage/index.html diff --git a/src/lib.rs b/src/lib.rs index a8269d8..47c53fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,9 @@ //! let result: f64 = duration / duration; // | Duration | / | Duration | f64 | //! ``` +#[cfg(feature = "rand")] +pub mod rand; + use core::fmt; use std::cmp::max; use std::cmp::min; diff --git a/src/rand.rs b/src/rand.rs new file mode 100644 index 0000000..818adb3 --- /dev/null +++ b/src/rand.rs @@ -0,0 +1,135 @@ +use rand::distributions::uniform::SampleBorrow; +use rand::distributions::uniform::SampleUniform; +use rand::distributions::uniform::UniformInt; +use rand::distributions::uniform::UniformSampler; +use rand::distributions::Distribution; +use rand::distributions::Standard; +use rand::Rng; + +use crate::Duration; +use crate::Time; + +impl Distribution