140 lines
15 KiB
HTML
140 lines
15 KiB
HTML
<!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="This crate provides helper types for matching against enum variants, and extracting bindings to each of the fields in the deriving Struct or Enum in a generic way."><title>synstructure - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="synstructure" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Crate synstructure</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../synstructure/index.html">synstructure</a><span class="version">0.13.2</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#example-walkfields" title="Example: `WalkFields`">Example: <code>WalkFields</code></a><ul><li><a href="#trait-implementation" title="Trait Implementation">Trait Implementation</a></li><li><a href="#custom-derive" title="Custom Derive">Custom Derive</a></li></ul></li><li><a href="#example-interest" title="Example: `Interest`">Example: <code>Interest</code></a><ul><li><a href="#trait-implementation-1" title="Trait Implementation">Trait Implementation</a></li><li><a href="#custom-derive-1" title="Custom Derive">Custom Derive</a></li></ul></li></ul><h3><a href="#macros">Crate Items</a></h3><ul class="block"><li><a href="#macros" title="Macros">Macros</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#enums" title="Enums">Enums</a></li><li><a href="#traits" title="Traits">Traits</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>synstructure</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/synstructure/lib.rs.html#1-2559">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>This crate provides helper types for matching against enum variants, and
|
||
extracting bindings to each of the fields in the deriving Struct or Enum in
|
||
a generic way.</p>
|
||
<p>If you are writing a <code>#[derive]</code> which needs to perform some operation on
|
||
every field, then you have come to the right place!</p>
|
||
<h2 id="example-walkfields"><a class="doc-anchor" href="#example-walkfields">§</a>Example: <code>WalkFields</code></h2><h4 id="trait-implementation"><a class="doc-anchor" href="#trait-implementation">§</a>Trait Implementation</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub trait </span>WalkFields: std::any::Any {
|
||
<span class="kw">fn </span>walk_fields(<span class="kw-2">&</span><span class="self">self</span>, walk: <span class="kw-2">&mut </span>FnMut(<span class="kw-2">&</span>WalkFields));
|
||
}
|
||
<span class="kw">impl </span>WalkFields <span class="kw">for </span>i32 {
|
||
<span class="kw">fn </span>walk_fields(<span class="kw-2">&</span><span class="self">self</span>, _walk: <span class="kw-2">&mut </span>FnMut(<span class="kw-2">&</span>WalkFields)) {}
|
||
}</code></pre></div><h4 id="custom-derive"><a class="doc-anchor" href="#custom-derive">§</a>Custom Derive</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>walkfields_derive(s: synstructure::Structure) -> proc_macro2::TokenStream {
|
||
<span class="kw">let </span>body = s.each(|bi| <span class="macro">quote!</span>{
|
||
walk(#bi)
|
||
});
|
||
|
||
s.gen_impl(<span class="macro">quote!</span> {
|
||
<span class="kw">extern crate </span>synstructure_test_traits;
|
||
|
||
<span class="kw">gen impl </span>synstructure_test_traits::WalkFields <span class="kw">for </span>@<span class="self">Self </span>{
|
||
<span class="kw">fn </span>walk_fields(<span class="kw-2">&</span><span class="self">self</span>, walk: <span class="kw-2">&mut </span>FnMut(<span class="kw-2">&</span>synstructure_test_traits::WalkFields)) {
|
||
<span class="kw">match </span><span class="kw-2">*</span><span class="self">self </span>{ #body }
|
||
}
|
||
}
|
||
})
|
||
}
|
||
<span class="macro">synstructure::decl_derive!</span>([WalkFields] => walkfields_derive);
|
||
|
||
<span class="comment">/*
|
||
* Test Case
|
||
*/
|
||
</span><span class="kw">fn </span>main() {
|
||
<span class="macro">synstructure::test_derive!</span> {
|
||
walkfields_derive {
|
||
<span class="kw">enum </span>A<T> {
|
||
B(i32, T),
|
||
C(i32),
|
||
}
|
||
}
|
||
expands to {
|
||
<span class="kw">const _</span>: () = {
|
||
<span class="kw">extern crate </span>synstructure_test_traits;
|
||
<span class="kw">impl</span><T> synstructure_test_traits::WalkFields <span class="kw">for </span>A<T>
|
||
<span class="kw">where </span>T: synstructure_test_traits::WalkFields
|
||
{
|
||
<span class="kw">fn </span>walk_fields(<span class="kw-2">&</span><span class="self">self</span>, walk: <span class="kw-2">&mut </span>FnMut(<span class="kw-2">&</span>synstructure_test_traits::WalkFields)) {
|
||
<span class="kw">match </span><span class="kw-2">*</span><span class="self">self </span>{
|
||
A::B(<span class="kw-2">ref </span>__binding_0, <span class="kw-2">ref </span>__binding_1,) => {
|
||
{ walk(__binding_0) }
|
||
{ walk(__binding_1) }
|
||
}
|
||
A::C(<span class="kw-2">ref </span>__binding_0,) => {
|
||
{ walk(__binding_0) }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
};
|
||
}
|
||
}
|
||
}</code></pre></div><h2 id="example-interest"><a class="doc-anchor" href="#example-interest">§</a>Example: <code>Interest</code></h2><h4 id="trait-implementation-1"><a class="doc-anchor" href="#trait-implementation-1">§</a>Trait Implementation</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub trait </span>Interest {
|
||
<span class="kw">fn </span>interesting(<span class="kw-2">&</span><span class="self">self</span>) -> bool;
|
||
}
|
||
<span class="kw">impl </span>Interest <span class="kw">for </span>i32 {
|
||
<span class="kw">fn </span>interesting(<span class="kw-2">&</span><span class="self">self</span>) -> bool { <span class="kw-2">*</span><span class="self">self </span>> <span class="number">0 </span>}
|
||
}</code></pre></div><h4 id="custom-derive-1"><a class="doc-anchor" href="#custom-derive-1">§</a>Custom Derive</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>interest_derive(<span class="kw-2">mut </span>s: synstructure::Structure) -> proc_macro2::TokenStream {
|
||
<span class="kw">let </span>body = s.fold(<span class="bool-val">false</span>, |acc, bi| <span class="macro">quote!</span>{
|
||
#acc || synstructure_test_traits::Interest::interesting(#bi)
|
||
});
|
||
|
||
s.gen_impl(<span class="macro">quote!</span> {
|
||
<span class="kw">extern crate </span>synstructure_test_traits;
|
||
<span class="kw">gen impl </span>synstructure_test_traits::Interest <span class="kw">for </span>@<span class="self">Self </span>{
|
||
<span class="kw">fn </span>interesting(<span class="kw-2">&</span><span class="self">self</span>) -> bool {
|
||
<span class="kw">match </span><span class="kw-2">*</span><span class="self">self </span>{
|
||
#body
|
||
}
|
||
}
|
||
}
|
||
})
|
||
}
|
||
<span class="macro">synstructure::decl_derive!</span>([Interest] => interest_derive);
|
||
|
||
<span class="comment">/*
|
||
* Test Case
|
||
*/
|
||
</span><span class="kw">fn </span>main() {
|
||
<span class="macro">synstructure::test_derive!</span>{
|
||
interest_derive {
|
||
<span class="kw">enum </span>A<T> {
|
||
B(i32, T),
|
||
C(i32),
|
||
}
|
||
}
|
||
expands to {
|
||
<span class="kw">const _</span>: () = {
|
||
<span class="kw">extern crate </span>synstructure_test_traits;
|
||
<span class="kw">impl</span><T> synstructure_test_traits::Interest <span class="kw">for </span>A<T>
|
||
<span class="kw">where </span>T: synstructure_test_traits::Interest
|
||
{
|
||
<span class="kw">fn </span>interesting(<span class="kw-2">&</span><span class="self">self</span>) -> bool {
|
||
<span class="kw">match </span><span class="kw-2">*</span><span class="self">self </span>{
|
||
A::B(<span class="kw-2">ref </span>__binding_0, <span class="kw-2">ref </span>__binding_1,) => {
|
||
<span class="bool-val">false </span>||
|
||
synstructure_test_traits::Interest::interesting(__binding_0) ||
|
||
synstructure_test_traits::Interest::interesting(__binding_1)
|
||
}
|
||
A::C(<span class="kw-2">ref </span>__binding_0,) => {
|
||
<span class="bool-val">false </span>||
|
||
synstructure_test_traits::Interest::interesting(__binding_0)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
};
|
||
}
|
||
}
|
||
}</code></pre></div>
|
||
<p>For more example usage, consider investigating the <code>abomonation_derive</code> crate,
|
||
which makes use of this crate, and is fairly simple.</p>
|
||
</div></details><h2 id="macros" class="section-header">Macros<a href="#macros" class="anchor">§</a></h2><dl class="item-table"><dt><a class="macro" href="macro.decl_attribute.html" title="macro synstructure::decl_attribute">decl_<wbr>attribute</a></dt><dd>The <code>decl_attribute!</code> macro declares a custom attribute wrapper. It will
|
||
parse the incoming <code>TokenStream</code> into a <code>synstructure::Structure</code> object,
|
||
and pass it into the inner function.</dd><dt><a class="macro" href="macro.decl_derive.html" title="macro synstructure::decl_derive">decl_<wbr>derive</a></dt><dd>The <code>decl_derive!</code> macro declares a custom derive wrapper. It will parse the
|
||
incoming <code>TokenStream</code> into a <code>synstructure::Structure</code> object, and pass it
|
||
into the inner function.</dd><dt><a class="macro" href="macro.test_derive.html" title="macro synstructure::test_derive">test_<wbr>derive</a></dt><dd>Run a test on a custom derive. This macro expands both the original struct
|
||
and the expansion to ensure that they compile correctly, and confirms that
|
||
feeding the original struct into the named derive will produce the written
|
||
output.</dd></dl><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.BindingInfo.html" title="struct synstructure::BindingInfo">Binding<wbr>Info</a></dt><dd>Information about a specific binding. This contains both an <code>Ident</code>
|
||
reference to the given field, and the syn <code>&'a Field</code> descriptor for that
|
||
field.</dd><dt><a class="struct" href="struct.Structure.html" title="struct synstructure::Structure">Structure</a></dt><dd>A wrapper around a <code>syn::DeriveInput</code> which provides utilities for creating
|
||
custom derive trait implementations.</dd><dt><a class="struct" href="struct.VariantAst.html" title="struct synstructure::VariantAst">Variant<wbr>Ast</a></dt><dd>This type is similar to <code>syn</code>’s <code>Variant</code> type, however each of the fields
|
||
are references rather than owned. When this is used as the AST for a real
|
||
variant, this struct simply borrows the fields of the <code>syn::Variant</code>,
|
||
however this type may also be used as the sole variant for a struct.</dd><dt><a class="struct" href="struct.VariantInfo.html" title="struct synstructure::VariantInfo">Variant<wbr>Info</a></dt><dd>A wrapper around a <code>syn::DeriveInput</code>’s variant which provides utilities
|
||
for destructuring <code>Variant</code>s with <code>match</code> expressions.</dd></dl><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><dl class="item-table"><dt><a class="enum" href="enum.AddBounds.html" title="enum synstructure::AddBounds">AddBounds</a></dt><dd>Changes how bounds are added</dd><dt><a class="enum" href="enum.BindStyle.html" title="enum synstructure::BindStyle">Bind<wbr>Style</a></dt><dd>The type of binding to use when generating a pattern.</dd></dl><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><dl class="item-table"><dt><a class="trait" href="trait.MacroResult.html" title="trait synstructure::MacroResult">Macro<wbr>Result</a></dt><dd>Helper trait describing values which may be returned by macro implementation
|
||
methods used by this crate’s macros.</dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.unpretty_print.html" title="fn synstructure::unpretty_print">unpretty_<wbr>print</a></dt><dd>Dumps an unpretty version of a tokenstream. Takes any type which implements
|
||
<code>Display</code>.</dd></dl></section></div></main></body></html> |