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
deriveorfull - fold_angle_bracketed_generic_arguments
deriveorfull - fold_arm
full - fold_assoc_const
deriveorfull - fold_assoc_type
deriveorfull - fold_attr_style
deriveorfull - fold_attribute
deriveorfull - fold_bare_fn_arg
deriveorfull - fold_bare_variadic
deriveorfull - fold_bin_op
deriveorfull - fold_block
full - fold_bound_lifetimes
deriveorfull - fold_const_param
deriveorfull - fold_constraint
deriveorfull - fold_data
derive - fold_data_enum
derive - fold_data_struct
derive - fold_data_union
derive - fold_derive_input
derive - fold_expr
deriveorfull - fold_expr_array
full - fold_expr_assign
full - fold_expr_async
full - fold_expr_await
full - fold_expr_binary
deriveorfull - fold_expr_block
full - fold_expr_break
full - fold_expr_call
deriveorfull - fold_expr_cast
deriveorfull - fold_expr_const
full - fold_expr_field
deriveorfull - fold_expr_group
deriveorfull - fold_expr_if
full - fold_expr_index
deriveorfull - fold_expr_infer
full - fold_expr_let
full - fold_expr_lit
deriveorfull - fold_expr_loop
full - fold_expr_macro
deriveorfull - fold_expr_match
full - fold_expr_method_call
deriveorfull - fold_expr_paren
deriveorfull - fold_expr_path
deriveorfull - fold_expr_range
full - fold_expr_reference
deriveorfull - fold_expr_repeat
full - fold_expr_return
full - fold_expr_struct
deriveorfull - fold_expr_try
full - fold_expr_tuple
full - fold_expr_unary
deriveorfull - fold_expr_unsafe
full - fold_expr_while
full - fold_expr_yield
full - fold_field
deriveorfull - fold_field_mutability
deriveorfull - fold_field_pat
full - fold_field_value
deriveorfull - fold_fields
deriveorfull - fold_fields_named
deriveorfull - fold_fields_unnamed
deriveorfull - fold_file
full - fold_fn_arg
full - fold_generic_argument
deriveorfull - fold_generic_param
deriveorfull - fold_generics
deriveorfull - fold_impl_item
full - fold_index
deriveorfull - 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
deriveorfull - fold_local
full - fold_local_init
full - fold_macro
deriveorfull - fold_macro_delimiter
deriveorfull - fold_member
deriveorfull - fold_meta
deriveorfull - fold_meta_list
deriveorfull - fold_meta_name_value
deriveorfull - fold_parenthesized_generic_arguments
deriveorfull - 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
deriveorfull - fold_path_arguments
deriveorfull - fold_path_segment
deriveorfull - fold_predicate_lifetime
deriveorfull - fold_predicate_type
deriveorfull - fold_qself
deriveorfull - fold_receiver
full - fold_return_type
deriveorfull - fold_signature
full - fold_stmt
full - fold_stmt_macro
full - fold_trait_bound
deriveorfull - fold_trait_bound_modifier
deriveorfull - fold_trait_item
full - fold_type
deriveorfull - fold_type_array
deriveorfull - fold_type_bare_fn
deriveorfull - fold_type_group
deriveorfull - fold_type_impl_trait
deriveorfull - fold_type_infer
deriveorfull - fold_type_macro
deriveorfull - fold_type_never
deriveorfull - fold_type_param
deriveorfull - fold_type_param_bound
deriveorfull - fold_type_paren
deriveorfull - fold_type_path
deriveorfull - fold_type_ptr
deriveorfull - fold_type_reference
deriveorfull - fold_type_slice
deriveorfull - fold_type_trait_object
deriveorfull - fold_type_tuple
deriveorfull - fold_un_op
deriveorfull - 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
deriveorfull - fold_vis_restricted
deriveorfull - fold_visibility
deriveorfull - fold_where_clause
deriveorfull - fold_where_predicate
deriveorfull