edlang/syn/fold/index.html

66 lines
32 KiB
HTML
Raw Normal View History

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Syntax tree traversal to transform the nodes of an owned syntax tree."><title>syn::fold - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="syn" data-themes="" data-resource-suffix="" data-rustdoc-version="1.80.0 (051478957 2024-07-21)" data-channel="1.80.0" data-search-js="search-d52510db62a78183.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../syn/index.html">syn</a><span class="version">2.0.72</span></h2></div><h2 class="location"><a href="#">Module fold</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li></ul></section><h2><a href="../index.html">In crate syn</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Module <a href="../index.html">syn</a>::<wbr><a class="mod" href="#">fold</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../../src/syn/gen/fold.rs.html#4-3779">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Syntax tree traversal to transform the nodes of an owned syntax tree.</p>
<p>Each method of the <a href="trait.Fold.html" title="trait syn::fold::Fold"><code>Fold</code></a> 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.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub trait </span>Fold {
<span class="comment">/* ... */
</span><span class="kw">fn </span>fold_expr_binary(<span class="kw-2">&amp;mut </span><span class="self">self</span>, node: ExprBinary) -&gt; ExprBinary {
fold_expr_binary(<span class="self">self</span>, node)
}
<span class="comment">/* ... */
</span>}
<span class="kw">pub fn </span>fold_expr_binary&lt;V&gt;(v: <span class="kw-2">&amp;mut </span>V, node: ExprBinary) -&gt; ExprBinary
<span class="kw">where
</span>V: Fold + <span class="question-mark">?</span>Sized,
{
ExprBinary {
attrs: node
.attrs
.into_iter()
.map(|attr| v.fold_attribute(attr))
.collect(),
left: Box::new(v.fold_expr(<span class="kw-2">*</span>node.left)),
op: v.fold_bin_op(node.op),
right: Box::new(v.fold_expr(<span class="kw-2">*</span>node.right)),
}
}
<span class="comment">/* ... */</span></code></pre></div>
<br>
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
<p>This fold inserts parentheses to fully parenthesizes any expression.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// [dependencies]
// quote = "1.0"
// syn = { version = "2.0", features = ["fold", "full"] }
</span><span class="kw">use </span>quote::quote;
<span class="kw">use </span>syn::fold::{fold_expr, Fold};
<span class="kw">use </span>syn::{token, Expr, ExprParen};
<span class="kw">struct </span>ParenthesizeEveryExpr;
<span class="kw">impl </span>Fold <span class="kw">for </span>ParenthesizeEveryExpr {
<span class="kw">fn </span>fold_expr(<span class="kw-2">&amp;mut </span><span class="self">self</span>, expr: Expr) -&gt; Expr {
Expr::Paren(ExprParen {
attrs: Vec::new(),
expr: Box::new(fold_expr(<span class="self">self</span>, expr)),
paren_token: token::Paren::default(),
})
}
}
<span class="kw">fn </span>main() {
<span class="kw">let </span>code = <span class="macro">quote!</span> { a() + b(<span class="number">1</span>) * c.d };
<span class="kw">let </span>expr: Expr = syn::parse2(code).unwrap();
<span class="kw">let </span>parenthesized = ParenthesizeEveryExpr.fold_expr(expr);
<span class="macro">println!</span>(<span class="string">"{}"</span>, <span class="macro">quote!</span>(#parenthesized));
<span class="comment">// Output: (((a)()) + (((b)((1))) * ((c).d)))
</span>}</code></pre></div>
</div></details><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.Fold.html" title="trait syn::fold::Fold">Fold</a></div><div class="desc docblock-short">Syntax tree traversal to transform the nodes of an owned syntax tree.</div></li></ul><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="fn" href="fn.fold_abi.html" title="fn syn::fold::fold_abi">fold_abi</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_angle_bracketed_generic_arguments.html" title="fn syn::fold::fold_angle_bracketed_generic_arguments">fold_angle_bracketed_generic_arguments</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_arm.html" title="fn syn::fold::fold_arm">fold_arm</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_assoc_const.html" title="fn syn::fold::fold_assoc_const">fold_assoc_const</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_assoc_type.html" title="fn syn::fold::fold_assoc_type">fold_assoc_type</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_attr_style.html" title="fn syn::fold::fold_attr_style">fold_attr_style</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_attribute.html" title="fn syn::fold::fold_attribute">fold_attribute</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_bare_fn_arg.html" title="fn syn::fold::fold_bare_fn_arg">fold_bare_fn_arg</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_bare_variadic.html" title="fn syn::fold::fold_bare_variadic">fold_bare_variadic</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_bin_op.html" title="fn syn::fold::fold_bin_op">fold_bin_op</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_block.html" title="fn syn::fold::fold_block">fold_block</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_bound_lifetimes.html" title="fn syn::fold::fold_bound_lifetimes">fold_bound_lifetimes</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_const_param.html" title="fn syn::fold::fold_const_param">fold_const_param</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_constraint.html" title="fn syn::fold::fold_constraint">fold_constraint</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_data.html" title="fn syn::fold::fold_data">fold_data</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_data_enum.html" title="fn syn::fold::fold_data_enum">fold_data_enum</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_data_struct.html" title="fn syn::fold::fold_data_struct">fold_data_struct</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_data_union.html" title="fn syn::fold::fold_data_union">fold_data_union</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_derive_input.html" title="fn syn::fold::fold_derive_input">fold_derive_input</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_expr.html" title="fn syn::fold::fold_expr">fold_expr</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_expr_array.html" title="fn syn::fold::fold_expr_array">fold_expr_array</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_expr_assign.html" title="fn syn::fold::fold_expr_assign">fold_expr_assign</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_expr_async.html" title="fn syn::fold::fold_expr_async">fold_expr_async</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_expr_await.html" title="fn syn::fold::fold_expr_await">fold_expr_await</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_expr_binary.html" title="fn syn::fold::fold_expr_binary">fold_expr_binary</a></div></li><li><div class="item-name"><a class="fn" href="fn.fold_expr_block.html" title="fn syn::fold::fold_expr_block">f