#[warn(missing_docs)]
pub mod prelude;
pub mod rand;
pub mod error;
pub mod helper;
#[cfg(test)]
mod tests {
use super::helper::*;
use super::prelude::*;
#[derive(Debug, Clone)]
struct Foo(f32, Option<String>);
impl Probable for Foo {
fn probability(&self) -> f32 {
self.0
}
}
#[test]
fn vec() -> Result<()> {
let v: Vec<_> = refcellify(vec![
Foo(0.1, None),
Foo(0.25, None),
Foo(0.5, None),
Foo(0.15, None),
])
.collect();
let mut chosen = Vec::with_capacity(100);
let mut rng = rand::thread_rng();
for _ in 0..100 {
chosen.push(v.choose_rand(&mut rng)?);
}
dbg!(chosen);
Ok(())
}
#[test]
fn vec_mut() -> Result<()> {
let mut v: Vec<_> = refcellify(
vec![
Foo(0.1, Some("hi".into())),
Foo(0.25, Some("hello".into())),
Foo(0.5, Some("hola".into())),
Foo(0.15, Some("bonjour".into())),
]
.into_iter(),
)
.collect();
let mut rng = rand::thread_rng();
let mut chosen = Vec::with_capacity(100);
for _ in 0..100 {
let mut c = v.choose_rand_mut(&mut rng)?;
if let Some(s) = &mut c.1 {
s.push('!');
}
chosen.push(c.clone()); }
dbg!(chosen, v);
Ok(())
}
}