Skip to main content

from_fn

Function from_fn 

Source
pub fn from_fn<F, T>(func: F) -> FromFn<F, T>
Expand description

Create a mutator from a function.

The function is given a Context and an &mut T value, and must return a mutatis::Result<()>.

ยงExample

use mutatis::{mutators as m, Context, Mutate, Session};

let mut mutator = m::from_fn(|ctx: &mut Context, pair: &mut (u32, u32)| {
    pair.0 = ctx.rng().gen_u32();
    pair.1 = if ctx.rng().gen_bool() {
        pair.0.wrapping_add(1)
    } else {
        pair.0.wrapping_sub(1)
    };
    Ok(())
});
let mut session = Session::new();

let mut value = (0, 0);

for _ in 0..5 {
    session.mutate_with(&mut mutator, &mut value)?;
    println!("{value:?}");
}

// Example output:
 //
//     (1886093101, 1886093102)
//     (3852925062, 3852925063)
//     (1697131274, 1697131275)
//     (4193528377, 4193528378)
//     (3958122412, 3958122411)