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_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