Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
liborty committed Nov 8, 2023
1 parent aee9994 commit 0f7377c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ran"
version = "1.1.4"
version = "1.1.5"
edition = "2021"
authors = ["Libor Spacek"]
description = """Simple, fast random numbers generation"""
Expand Down
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

## Description

The objective of this crate is to generate excellent quality random numbers fast, simply and with a minimal footprint. It is written in 100% safe Rust,
is lightweight and has no dependencies.
The objective of this crate is to generate excellent quality random numbers fast, simply and with a minimal footprint. It is written in 100% safe Rust, is lightweight and has no dependencies.

Even so, there are four different generating algorithms on offer, plus a good range of utility functions to easily generate individual numbers of various types, vectors, and vectors of vectors, all filled with random numbers.

Expand Down Expand Up @@ -89,11 +88,22 @@ When pattern extracting with the `if let` clause, the else branch can be used to

```rust
// wrapped vec of random u8 values
if let Rv::U8(vx) = ru8.ranv_in(20,1.,6.)?
if let Rv::U8(vx) = ru8.ranv_in(20,1.,6.)
{ println!("Dice roll sequence: {}", stringv(&vx)) };
```

This example illustrates the use of enum type `Rv`, used for vector of random numbers. As can be seen, its variants are extracted in the same way as from `Rnum`. Of course, Rv type object (unextracted) would print as it is.
or

```rust
// ten random binary numbers - with full error checking
let Rv::U8(vecu8) = ru8.ranv_in(10,0.,1.) else {
return rerror("type","Pattern extraction failed for bytes");
};
println!("\nBinary numbers: {}",stringv(&vecu8));

```

This example illustrates the use of enum type `Rv`, used for vector of random numbers. As can be seen, its variants are extracted in the same way as from `Rnum`. Of course, because it also has `Display` trait implemented, `Rv` type object would print as it is.

There is also enum type `Rvv` for returning vectors of vectors of random numbers:

Expand All @@ -107,7 +117,7 @@ println!(

`stringvv` is another utility function to enable display of generic vectors of vectors. We did not need to use it here since `Display` is implemented for `Rvv` type and we did not need to extract the wrapped value (vector of vectors).

The results wrapped within all three return types: `Rnum,Rv,Rvv` can all be pattern extracted as needed with `if let` or with `let .. else`
The results wrapped within all three return types: `Rnum,Rv,Rvv` can all be pattern extracted as needed using `if let` or `let .. else`

Alternatively, for convenience, they can all be extracted with supplied `get` methods. Their names follow this syntax: `get{|v|vv}end_type()`.

Expand Down
10 changes: 5 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ use std::fmt;
use std::error::Error;
use std::fmt::Debug;

/// Shorthand type for returned errors with message payload specialized to String
pub type Re = RanError<String>;

#[derive(Debug)]
/// Custom RStats Error
pub enum RanError<T> {
Expand All @@ -18,6 +15,9 @@ pub enum RanError<T> {
Other(T)
}

/// Shorthand type for returned errors with message payload specialized to String
pub type Re = RanError<String>;

impl Error for Re {}

impl fmt::Display for Re {
Expand All @@ -30,8 +30,8 @@ impl fmt::Display for Re {
}
}

/// Convenience function for building RError<String>
/// from short name and payload message, which can be either &str or String
/// Convenience function for building `RanError<String>` (ran crate custom error)
/// from short name and payload message, which can be either `&str` or `String`
pub fn rerror<T>(kind: &str, msg: impl Into<String>) -> Result<T,RanError<String>> {
match kind {
"type" => Err(RanError::Type(msg.into())),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod generators;
pub mod secondary;

pub use crate::generators::{set_seeds,get_seed};
pub use crate::error::{RanError,Re};
pub use crate::error::{RanError,rerror,Re};

/// Wrapper for enum polymorphism - supported end types
pub enum Rnum {
Expand Down
13 changes: 7 additions & 6 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(dead_code)]
#[cfg(test)]
use times::bench;
use ran::{Rnum,Rv,Re,set_seeds,generators::{ranvvu8,ranvvu16,ranvvu64,ranvvi64,ranvvf64,},secondary::stringv};
use ran::{Rnum,Rv,Re,rerror,set_seeds,generators::{ranvvu8,ranvvu16,ranvvu64,ranvvi64,ranvvf64,},secondary::stringv};

#[test]
fn rannums() -> Result<(),Re> {
Expand Down Expand Up @@ -38,14 +38,15 @@ fn rannums() -> Result<(),Re> {
println!("2 random i64s: {}", stringv(&ri.ranv(2)?.getvi64()?));

// this is expanded here just to demonstrate pattern extraction
// of the wrapped Vec<u8>, which is not normally needed for just printing it:
// println!("Dice roll: {}",ru8.ranvec_in(20,1.,6.)};
// of the wrapped Vec<u8>, which is not normally needed for just printing it
if let Rv::U8(vecu8) = ru8.ranv_in(20,1.,6.) {
println!("\nDice roll: {}",stringv(&vecu8)) };

// ten random binary numbers
if let Rv::U8(vecu8) = ru8.ranv_in(10,0.,1.) {
println!("\nBinary numbers: {}",stringv(&vecu8)) };
// or with full error checking and returning
let Rv::U8(vecu8) = ru8.ranv_in(10,0.,1.) else {
return rerror("type","Pattern extraction failed for random bytes");
};
println!("\nBinary numbers: {}",stringv(&vecu8));

// vec of vecs using ranvv_in(d,n,min,max)
println!("\n5x5 matrix of integers in range [-10,10]:\n{}",
Expand Down

0 comments on commit 0f7377c

Please sign in to comment.