Available on crate feature
visit-mut
only.Expand description
Syntax tree traversal to mutate an exclusive borrow of a syntax tree in place.
Each method of the VisitMut
trait is a hook that can be overridden
to customize the behavior when mutating the corresponding type of node.
By default, every method recursively visits the substructure of the
input by invoking the right visitor method of each of its fields.
pub trait VisitMut {
/* ... */
fn visit_expr_binary_mut(&mut self, node: &mut ExprBinary) {
visit_expr_binary_mut(self, node);
}
/* ... */
}
pub fn visit_expr_binary_mut<V>(v: &mut V, node: &mut ExprBinary)
where
V: VisitMut + ?Sized,
{
for attr in &mut node.attrs {
v.visit_attribute_mut(attr);
}
v.visit_expr_mut(&mut *node.left);
v.visit_bin_op_mut(&mut node.op);
v.visit_expr_mut(&mut *node.right);
}
/* ... */
§Example
This mut visitor replace occurrences of u256 suffixed integer literals
like 999u256
with a macro invocation bigint::u256!(999)
.
// [dependencies]
// quote = "1.0"
// syn = { version = "2.0", features = ["full", "visit-mut"] }
use quote::quote;
use syn::visit_mut::{self, VisitMut};
use syn::{parse_quote, Expr, File, Lit, LitInt};
struct BigintReplace;
impl VisitMut for BigintReplace {
fn visit_expr_mut(&mut self, node: &mut Expr) {
if let Expr::Lit(expr) = &node {
if let Lit::Int(int) = &expr.lit {
if int.suffix() == "u256" {
let digits = int.base10_digits();
let unsuffixed: LitInt = syn::parse_str(digits).unwrap();
*node = parse_quote!(bigint::u256!(#unsuffixed));
return;
}
}
}
// Delegate to the default impl to visit nested expressions.
visit_mut::visit_expr_mut(self, node);
}
}
fn main() {
let code = quote! {
fn main() {
let _ = 999u256;
}
};
let mut syntax_tree: File = syn::parse2(code).unwrap();
BigintReplace.visit_file_mut(&mut syntax_tree);
println!("{}", quote!(#syntax_tree));
}
Traits§
- Syntax tree traversal to mutate an exclusive borrow of a syntax tree in place.
Functions§
- visit_
abi_ mut derive
orfull
- visit_
angle_ bracketed_ generic_ arguments_ mut derive
orfull
- visit_
arm_ mut full
- visit_
assoc_ const_ mut derive
orfull
- visit_
assoc_ type_ mut derive
orfull
- visit_
attr_ style_ mut derive
orfull
- visit_
attribute_ mut derive
orfull
- visit_
bare_ fn_ arg_ mut derive
orfull
- visit_
bare_ variadic_ mut derive
orfull
- visit_
bin_ op_ mut derive
orfull
- visit_
block_ mut full
- visit_
bound_ lifetimes_ mut derive
orfull
- visit_
const_ param_ mut derive
orfull
- visit_
constraint_ mut derive
orfull
- visit_
data_ enum_ mut derive
- visit_
data_ mut derive
- visit_
data_ struct_ mut derive
- visit_
data_ union_ mut derive
- visit_
derive_ input_ mut derive
- visit_
expr_ binary_ mut derive
orfull
- visit_
expr_ call_ mut derive
orfull
- visit_
expr_ cast_ mut derive
orfull
- visit_
expr_ field_ mut derive
orfull
- visit_
expr_ group_ mut derive
orfull
- visit_
expr_ index_ mut derive
orfull
- visit_
expr_ lit_ mut derive
orfull
- visit_
expr_ macro_ mut derive
orfull
- visit_
expr_ method_ call_ mut derive
orfull
- visit_
expr_ mut derive
orfull
- visit_
expr_ paren_ mut derive
orfull
- visit_
expr_ path_ mut derive
orfull
- visit_
expr_ reference_ mut derive
orfull
- visit_
expr_ struct_ mut derive
orfull
- visit_
expr_ tuple_ mut derive
orfull
- visit_
expr_ unary_ mut derive
orfull
- visit_
field_ mut derive
orfull
- visit_
field_ mutability_ mut derive
orfull
- visit_
field_ value_ mut derive
orfull
- visit_
fields_ mut derive
orfull
- visit_
fields_ named_ mut derive
orfull
- visit_
fields_ unnamed_ mut derive
orfull
- visit_
file_ mut full
- visit_
fn_ arg_ mut full
- visit_
generic_ argument_ mut derive
orfull
- visit_
generic_ param_ mut derive
orfull
- visit_
generics_ mut derive
orfull
- visit_
index_ mut derive
orfull
- visit_
item_ mut full
- visit_
label_ mut full
- visit_
lifetime_ param_ mut derive
orfull
- visit_
local_ mut full
- visit_
macro_ delimiter_ mut derive
orfull
- visit_
macro_ mut derive
orfull
- visit_
member_ mut derive
orfull
- visit_
meta_ list_ mut derive
orfull
- visit_
meta_ mut derive
orfull
- visit_
meta_ name_ value_ mut derive
orfull
- visit_
parenthesized_ generic_ arguments_ mut derive
orfull
- visit_
pat_ mut full
- visit_
pat_ or_ mut full
- visit_
path_ arguments_ mut derive
orfull
- visit_
path_ mut derive
orfull
- visit_
path_ segment_ mut derive
orfull
- visit_
predicate_ lifetime_ mut derive
orfull
- visit_
predicate_ type_ mut derive
orfull
- visit_
qself_ mut derive
orfull
- visit_
return_ type_ mut derive
orfull
- visit_
stmt_ mut full
- visit_
trait_ bound_ modifier_ mut derive
orfull
- visit_
trait_ bound_ mut derive
orfull
- visit_
type_ array_ mut derive
orfull
- visit_
type_ bare_ fn_ mut derive
orfull
- visit_
type_ group_ mut derive
orfull
- visit_
type_ impl_ trait_ mut derive
orfull
- visit_
type_ infer_ mut derive
orfull
- visit_
type_ macro_ mut derive
orfull
- visit_
type_ mut derive
orfull
- visit_
type_ never_ mut derive
orfull
- visit_
type_ param_ bound_ mut derive
orfull
- visit_
type_ param_ mut derive
orfull
- visit_
type_ paren_ mut derive
orfull
- visit_
type_ path_ mut derive
orfull
- visit_
type_ ptr_ mut derive
orfull
- visit_
type_ reference_ mut derive
orfull
- visit_
type_ slice_ mut derive
orfull
- visit_
type_ trait_ object_ mut derive
orfull
- visit_
type_ tuple_ mut derive
orfull
- visit_
un_ op_ mut derive
orfull
- visit_
variant_ mut derive
orfull
- visit_
vis_ restricted_ mut derive
orfull
- visit_
visibility_ mut derive
orfull
- visit_
where_ clause_ mut derive
orfull
- visit_
where_ predicate_ mut derive
orfull