pub struct Mutator<'a, T: Debug + Clone, const N: usize> {
pub mutated: [bool; N],
pub mutator: &'a T,
}
Expand description
The Mutator
struct is used to mutate an AST in very targeted way.
The Mutator
struct is totally generic, so it can be thought of a game console: its purpose is
to mutate an AST but it doesn’t know how to do it until an
implementor
is loaded. This implementor will tell Mutator
how it should behave.
A new Mutator
can be easily created using Mutator::default()
, which returns a
Mutator<'_, EmptyMutator, 1>
. This Mutator
is useless up to this point, but thanks to the
ToMutate
trait, an implementor can be seamlessly loaded to the Mutator
, releasing its whole power.
Once configured, the mutate
method can be called to mutate the AST.
use test_builder::TestBuilder;
use rust_writer::ast::{
mutator::{Mutator, ToMutate},
implementors::ItemToTrait
};
use syn::{
visit_mut::VisitMut,
parse_quote,
TraitItem
};
TestBuilder::default()
.with_trait_ast()
.execute(|mut builder|{
let ast = builder.get_mut_ast_file("trait.rs").expect("This exists; qed;");
// Define the ItemToTrait implementor. This implementor means:
// 1. We're looking inside a trait called `MyTrait` .
// 2. We're interested in adding a type called `Type3` with trait bound `From<String>` to
// that trait.
let item_to_trait: ItemToTrait =
("MyTrait", parse_quote! {type Type3: From<String>;}).into();
// Create the Mutator and load this particular implementor
let mut mutator = Mutator::default().to_mutate(&item_to_trait);
// Mutate the AST
assert!(mutator.mutate(ast).is_ok());
});
Fields§
§mutated: [bool; N]
A Mutator
can act in different parts of the AST at the same time. This array keeps track
of which of these mutations succeeded.
mutator: &'a T
Placeholder to load an implementor.