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§
- Fold
- 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_
captured_ param full - 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_ closure full - fold_
expr_ const full - fold_
expr_ continue full - fold_
expr_ field deriveorfull - fold_
expr_ for_ loop full - 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_ raw_ addr full - fold_
expr_ reference deriveorfull - fold_
expr_ repeat full - fold_
expr_ return full - fold_
expr_ struct deriveorfull - fold_
expr_ try full - fold_
expr_ try_ block full - fold_
expr_ tuple deriveorfull - 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_
foreign_ item full - fold_
foreign_ item_ fn full - fold_
foreign_ item_ macro full - fold_
foreign_ item_ static full - fold_
foreign_ item_ type full - fold_
generic_ argument deriveorfull - fold_
generic_ param deriveorfull - fold_
generics deriveorfull - fold_
ident - fold_
impl_ item full - fold_
impl_ item_ const full - fold_
impl_ item_ fn full - fold_
impl_ item_ macro full - fold_
impl_ item_ type full - fold_
impl_ restriction full - fold_
index deriveorfull - fold_
item full - fold_
item_ const full - fold_
item_ enum full - fold_
item_ extern_ crate full - fold_
item_ fn full - fold_
item_ foreign_ mod 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_ trait_ alias full - fold_
item_ type full - fold_
item_ union full - fold_
item_ use full - fold_
label full - fold_
lifetime - fold_
lifetime_ param deriveorfull - fold_
lit - fold_
lit_ bool - fold_
lit_ byte - fold_
lit_ byte_ str - fold_
lit_ char - fold_
lit_ cstr - fold_
lit_ float - fold_
lit_ int - fold_
lit_ str - 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_ reference full - fold_
pat_ rest full - fold_
pat_ slice full - fold_
pat_ struct full - fold_
pat_ tuple full - fold_
pat_ tuple_ struct full - fold_
pat_ type full - fold_
pat_ wild full - fold_
path deriveorfull - fold_
path_ arguments deriveorfull - fold_
path_ segment deriveorfull - fold_
pointer_ mutability full - fold_
precise_ capture full - fold_
predicate_ lifetime deriveorfull - fold_
predicate_ type deriveorfull - fold_
qself deriveorfull - fold_
range_ limits full - fold_
receiver full - fold_
return_ type deriveorfull - fold_
signature full - fold_
span - fold_
static_ mutability full - fold_
stmt full - fold_
stmt_ macro full - fold_
trait_ bound deriveorfull - fold_
trait_ bound_ modifier deriveorfull - fold_
trait_ item full - fold_
trait_ item_ const full - fold_
trait_ item_ fn full - fold_
trait_ item_ macro full - fold_
trait_ item_ type 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