fn mutate()

in lain/src/mutatable.rs [208:253]


    fn mutate<R: rand::Rng>(
        &mut self,
        mutator: &mut Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) {
        const CHANCE_TO_RESIZE_VEC: f64 = 0.01;

        if T::max_default_object_size() == 0 {
            return;
        }

        // we can grow the vector if we have no size constraint or the max size quota hasn't
        // been fulfilled
        let can_grow = constraints
            .map(|c| {
                c.max_size
                    .map(|s| s > 0 && s > T::max_default_object_size())
                    .unwrap_or(true)
            })
            .unwrap_or(false);

        if mutator.gen_chance(CHANCE_TO_RESIZE_VEC) {
            let resize_type = VecResizeType::new_fuzzed(mutator, None);
            if resize_type == VecResizeType::Grow && can_grow {
                grow_vec(self, mutator, constraints.and_then(|c| c.max_size));
            } else {
                shrink_vec(self, mutator);
            }
        } else {
            // Recreate the constraints so that the min/max types match
            let constraints = constraints.and_then(|c| {
                if c.max_size.is_none() {
                    None
                } else {
                    let mut new_constraints = Constraints::new();
                    new_constraints.base_object_size_accounted_for =
                        c.base_object_size_accounted_for;
                    new_constraints.max_size = c.max_size;

                    Some(new_constraints)
                }
            });

            self.as_mut_slice().mutate(mutator, constraints.as_ref());
        }
    }