Struct Session

Source
pub struct Session { /* private fields */ }
Expand description

A mutation session and its configuration.

This type allows you to configure things like setting the RNG seed, or whether to only perform shrinking mutations.

A session should be reused while a particular value, or set of values, are being repeatedly mutated.

§Example

use mutatis::Session;

// Create a new mutation session.
let mut session = Session::new()
    // Configure the RNG seed, changing which random mutations are chosen.
    .seed(0x12345678);

// Mutate a value a few times inside this session.
let mut x = 93;
for _ in 0..3 {
    session.mutate(&mut x)?;
    println!("mutated x is {x}");
}

// Example output:
//
//     mutated x is -906367562
//     mutated x is 766527557
//     mutated x is 132130383

Implementations§

Source§

impl Session

Source

pub fn new() -> Self

Create a new, default Session.

Source

pub fn seed(self, seed: u64) -> Self

Set the seed for the random number generator.

Source

pub fn shrink(self, shrink: bool) -> Self

Set whether to only perform shrinking mutations or not.

Defaults to false.

Source

pub fn mutate<T>(&mut self, value: &mut T) -> Result<()>
where T: DefaultMutate,

Mutate the given value with its default mutator and within the constraints of this Session’s configuration.

The default mutator for a type is defined by the DefaultMutate trait implementation for that type.

To use a custom mutator, rather than the default mutator, use the mutate_with method instead.

§Example
use mutatis::Session;

let mut x = Some(1234i32);

let mut session = Session::new().seed(0xaabbccdd);

for _ in 0..5 {
    session.mutate(&mut x)?;
    println!("mutated x is {x:?}");
}

// Example output:
//
//     mutated x is None
//     mutated x is Some(-688796504)
//     mutated x is None
//     mutated x is Some(-13390771)
//     mutated x is Some(1208312368)
Source

pub fn mutate_with<T>( &mut self, mutator: &mut impl Mutate<T>, value: &mut T, ) -> Result<()>

Mutate the given value with the given mutator and within the constraints of this Session’s configuration.

This is similar to the mutate method, but allows you to specify a custom mutator to use instead of the default mutator for value’s type.

§Example
use mutatis::{mutators as m, Session};

let mut res = Ok(1234i32);

// Create a custom mutator for `Result<i32, bool>` values.
let mut mutator = m::result(m::range(-10..=10), m::just(true));

let mut session = Session::new().seed(0x1984);

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

// Example output:
//
//     mutated res is Err(true)
//     mutated res is Err(true)
//     mutated res is Ok(9)
//     mutated res is Err(true)
//     mutated res is Ok(-6)

Trait Implementations§

Source§

impl Debug for Session

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Session

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.