Available on crate feature
fold
only.Expand description
Syntax tree traversal to transform the nodes of an owned syntax tree.
Each method of the Fold
trait is a hook that can be overridden to
customize the behavior when transforming 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 Fold {
/* ... */
fn fold_expr_binary(&mut self, node: ExprBinary) -> ExprBinary {
fold_expr_binary(self, node)
}
/* ... */
}
pub fn fold_expr_binary<V>(v: &mut V, node: ExprBinary) -> ExprBinary
where
V: Fold + ?Sized,
{
ExprBinary {
attrs: node
.attrs
.into_iter()
.map(|attr| v.fold_attribute(attr))
.collect(),
left: Box::new(v.fold_expr(*node.left)),
op: v.fold_bin_op(node.op),
right: Box::new(v.fold_expr(*node.right)),
}
}
/* ... */
§Example
This fold inserts parentheses to fully parenthesizes any expression.
// [dependencies]
// quote = "1.0"
// syn = { version = "2.0", features = ["fold", "full"] }
use quote::quote;
use syn::fold::{fold_expr, Fold};
use syn::{token, Expr, ExprParen};
struct ParenthesizeEveryExpr;
impl Fold for ParenthesizeEveryExpr {
fn fold_expr(&mut self, expr: Expr) -> Expr {
Expr::Paren(ExprParen {
attrs: Vec::new(),
expr: Box::new(fold_expr(self, expr)),
paren_token: token::Paren::default(),
})
}
}
fn main() {
let code = quote! { a() + b(1) * c.d };
let expr: Expr = syn::parse2(code).unwrap();
let parenthesized = ParenthesizeEveryExpr.fold_expr(expr);
println!("{}", quote!(#parenthesized));
// Output: (((a)()) + (((b)((1))) * ((c).d)))
}
Traits§
- Syntax tree traversal to transform the nodes of an owned syntax tree.
Functions§
- fold_abi
derive
orfull
- fold_angle_bracketed_generic_arguments
derive
orfull
- fold_arm
full
- fold_assoc_const
derive
orfull
- fold_assoc_type
derive
orfull
- fold_attr_style
derive
orfull
- fold_attribute
derive
orfull
- fold_bare_fn_arg
derive
orfull
- fold_bare_variadic
derive
orfull
- fold_bin_op
derive
orfull
- fold_block
full
- fold_bound_lifetimes
derive
orfull
- fold_const_param
derive
orfull
- fold_constraint
derive
orfull
- fold_data
derive
- fold_data_enum
derive
- fold_data_struct
derive
- fold_data_union
derive
- fold_derive_input
derive
- fold_expr
derive
orfull
- fold_expr_array
full
- fold_expr_assign
full
- fold_expr_async
full
- fold_expr_await
full
- fold_expr_binary
derive
orfull
- fold_expr_block
full
- fold_expr_break
full
- fold_expr_call
derive
orfull
- fold_expr_cast
derive
orfull
- fold_expr_const
full
- fold_expr_field
derive
orfull
- fold_expr_group
derive
orfull
- fold_expr_if
full
- fold_expr_index
derive
orfull
- fold_expr_infer
full
- fold_expr_let
full
- fold_expr_lit
derive
orfull
- fold_expr_loop
full
- fold_expr_macro
derive
orfull
- fold_expr_match
full
- fold_expr_method_call
derive
orfull
- fold_expr_paren
derive
orfull
- fold_expr_path
derive
orfull
- fold_expr_range
full
- fold_expr_reference
derive
orfull
- fold_expr_repeat
full
- fold_expr_return
full
- fold_expr_struct
derive
orfull
- fold_expr_try
full
- fold_expr_tuple
full
- fold_expr_unary
derive
orfull
- fold_expr_unsafe
full
- fold_expr_while
full
- fold_expr_yield
full
- fold_field
derive
orfull
- fold_field_mutability
derive
orfull
- fold_field_pat
full
- fold_field_value
derive
orfull
- fold_fields
derive
orfull
- fold_fields_named
derive
orfull
- fold_fields_unnamed
derive
orfull
- fold_file
full
- fold_fn_arg
full
- fold_generic_argument
derive
orfull
- fold_generic_param
derive
orfull
- fold_generics
derive
orfull
- fold_impl_item
full
- fold_index
derive
orfull
- fold_item
full
- fold_item_const
full
- fold_item_enum
full
- fold_item_fn
full
- fold_item_impl
full
- fold_item_macro
full
- fold_item_mod
full
- fold_item_static
full
- fold_item_struct
full
- fold_item_trait
full
- fold_item_type
full
- fold_item_union
full
- fold_item_use
full
- fold_label
full
- fold_lifetime_param
derive
orfull
- fold_local
full
- fold_local_init
full
- fold_macro
derive
orfull
- fold_macro_delimiter
derive
orfull
- fold_member
derive
orfull
- fold_meta
derive
orfull
- fold_meta_list
derive
orfull
- fold_meta_name_value
derive
orfull
- fold_parenthesized_generic_arguments
derive
orfull
- fold_pat
full
- fold_pat_ident
full
- fold_pat_or
full
- fold_pat_paren
full
- fold_pat_rest
full
- fold_pat_slice
full
- fold_pat_struct
full
- fold_pat_tuple
full
- fold_pat_type
full
- fold_pat_wild
full
- fold_path
derive
orfull
- fold_path_arguments
derive
orfull
- fold_path_segment
derive
orfull
- fold_predicate_lifetime
derive
orfull
- fold_predicate_type
derive
orfull
- fold_qself
derive
orfull
- fold_receiver
full
- fold_return_type
derive
orfull
- fold_signature
full
- fold_stmt
full
- fold_stmt_macro
full
- fold_trait_bound
derive
orfull
- fold_trait_bound_modifier
derive
orfull
- fold_trait_item
full
- fold_type
derive
orfull
- fold_type_array
derive
orfull
- fold_type_bare_fn
derive
orfull
- fold_type_group
derive
orfull
- fold_type_impl_trait
derive
orfull
- fold_type_infer
derive
orfull
- fold_type_macro
derive
orfull
- fold_type_never
derive
orfull
- fold_type_param
derive
orfull
- fold_type_param_bound
derive
orfull
- fold_type_paren
derive
orfull
- fold_type_path
derive
orfull
- fold_type_ptr
derive
orfull
- fold_type_reference
derive
orfull
- fold_type_slice
derive
orfull
- fold_type_trait_object
derive
orfull
- fold_type_tuple
derive
orfull
- fold_un_op
derive
orfull
- fold_use_glob
full
- fold_use_group
full
- fold_use_name
full
- fold_use_path
full
- fold_use_rename
full
- fold_use_tree
full
- fold_variadic
full
- fold_variant
derive
orfull
- fold_vis_restricted
derive
orfull
- fold_visibility
derive
orfull
- fold_where_clause
derive
orfull
- fold_where_predicate
derive
orfull