mentat/synstructure/index.html
2018-08-22 17:04:13 +00:00

369 lines
No EOL
24 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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="API documentation for the Rust `synstructure` crate.">
<meta name="keywords" content="rust, rustlang, rust-lang, synstructure">
<title>synstructure - Rust</title>
<link rel="stylesheet" type="text/css" href="../normalize.css">
<link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle">
<link rel="stylesheet" type="text/css" href="../dark.css">
<link rel="stylesheet" type="text/css" href="../main.css" id="themeStyle">
<script src="../storage.js"></script>
</head>
<body class="rustdoc mod">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<nav class="sidebar">
<div class="sidebar-menu">&#9776;</div>
<p class='location'>Crate synstructure</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'synstructure', ty: 'mod', relpath: '../'};</script></div>
</nav>
<div class="theme-picker">
<button id="theme-picker" aria-label="Pick another theme!">
<img src="../brush.svg" width="18" alt="Pick another theme!">
</button>
<div id="theme-choices"></div>
</div>
<script src="../theme.js"></script>
<nav class="sub">
<form class="search-form js-only">
<div class="search-container">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>synstructure</a></span><span class='out-of-band'><span id='render-detail'>
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
[<span class='inner'>&#x2212;</span>]
</a>
</span><a class='srclink' href='../src/synstructure/lib.rs.html#1-2236' title='goto source code'>[src]</a></span></h1>
<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>
<h1 id="example-walkfields" class="section-header"><a href="#example-walkfields">Example: <code>WalkFields</code></a></h1><h3 id="trait-implementation" class="section-header"><a href="#trait-implementation">Trait Implementation</a></h3>
<pre class="rust rust-example-rendered">
<span class="kw">pub</span> <span class="kw">trait</span> <span class="ident">WalkFields</span>: <span class="ident">std</span>::<span class="ident">any</span>::<span class="ident">Any</span> {
<span class="kw">fn</span> <span class="ident">walk_fields</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">walk</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">FnMut</span>(<span class="kw-2">&amp;</span><span class="ident">WalkFields</span>));
}
<span class="kw">impl</span> <span class="ident">WalkFields</span> <span class="kw">for</span> <span class="ident">i32</span> {
<span class="kw">fn</span> <span class="ident">walk_fields</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">_walk</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">FnMut</span>(<span class="kw-2">&amp;</span><span class="ident">WalkFields</span>)) {}
}</pre>
<h3 id="custom-derive" class="section-header"><a href="#custom-derive">Custom Derive</a></h3>
<pre class="rust rust-example-rendered">
<span class="attribute">#[<span class="ident">macro_use</span>]</span>
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">synstructure</span>;
<span class="attribute">#[<span class="ident">macro_use</span>]</span>
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">quote</span>;
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">proc_macro2</span>;
<span class="kw">fn</span> <span class="ident">walkfields_derive</span>(<span class="ident">s</span>: <span class="ident">synstructure</span>::<span class="ident">Structure</span>) <span class="op">-&gt;</span> <span class="ident">proc_macro2</span>::<span class="ident">TokenStream</span> {
<span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">each</span>(<span class="op">|</span><span class="ident">bi</span><span class="op">|</span> <span class="macro">quote</span><span class="macro">!</span>{
<span class="ident">walk</span>(#<span class="ident">bi</span>)
});
<span class="ident">s</span>.<span class="ident">gen_impl</span>(<span class="macro">quote</span><span class="macro">!</span> {
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">synstructure_test_traits</span>;
<span class="ident">gen</span> <span class="kw">impl</span> <span class="ident">synstructure_test_traits</span>::<span class="ident">WalkFields</span> <span class="kw">for</span> @<span class="self">Self</span> {
<span class="kw">fn</span> <span class="ident">walk_fields</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">walk</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">FnMut</span>(<span class="kw-2">&amp;</span><span class="ident">synstructure_test_traits</span>::<span class="ident">WalkFields</span>)) {
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> { #<span class="ident">body</span> }
}
}
})
}
<span class="macro">decl_derive</span><span class="macro">!</span>([<span class="ident">WalkFields</span>] <span class="op">=&gt;</span> <span class="ident">walkfields_derive</span>);
<span class="comment">/*
* Test Case
*/</span>
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="macro">test_derive</span><span class="macro">!</span> {
<span class="ident">walkfields_derive</span> {
<span class="kw">enum</span> <span class="ident">A</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">B</span>(<span class="ident">i32</span>, <span class="ident">T</span>),
<span class="ident">C</span>(<span class="ident">i32</span>),
}
}
<span class="ident">expands</span> <span class="ident">to</span> {
<span class="attribute">#[<span class="ident">allow</span>(<span class="ident">non_upper_case_globals</span>)]</span>
<span class="kw">const</span> <span class="ident">_DERIVE_synstructure_test_traits_WalkFields_FOR_A</span>: () <span class="op">=</span> {
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">synstructure_test_traits</span>;
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">synstructure_test_traits</span>::<span class="ident">WalkFields</span> <span class="kw">for</span> <span class="ident">A</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>
<span class="kw">where</span> <span class="ident">T</span>: <span class="ident">synstructure_test_traits</span>::<span class="ident">WalkFields</span>
{
<span class="kw">fn</span> <span class="ident">walk_fields</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">walk</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">FnMut</span>(<span class="kw-2">&amp;</span><span class="ident">synstructure_test_traits</span>::<span class="ident">WalkFields</span>)) {
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="ident">A</span>::<span class="ident">B</span>(<span class="kw-2">ref</span> <span class="ident">__binding_0</span>, <span class="kw-2">ref</span> <span class="ident">__binding_1</span>,) <span class="op">=&gt;</span> {
{ <span class="ident">walk</span>(<span class="ident">__binding_0</span>) }
{ <span class="ident">walk</span>(<span class="ident">__binding_1</span>) }
}
<span class="ident">A</span>::<span class="ident">C</span>(<span class="kw-2">ref</span> <span class="ident">__binding_0</span>,) <span class="op">=&gt;</span> {
{ <span class="ident">walk</span>(<span class="ident">__binding_0</span>) }
}
}
}
}
};
}
}
}</pre>
<h1 id="example-interest" class="section-header"><a href="#example-interest">Example: <code>Interest</code></a></h1><h3 id="trait-implementation-1" class="section-header"><a href="#trait-implementation-1">Trait Implementation</a></h3>
<pre class="rust rust-example-rendered">
<span class="kw">pub</span> <span class="kw">trait</span> <span class="ident">Interest</span> {
<span class="kw">fn</span> <span class="ident">interesting</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="ident">bool</span>;
}
<span class="kw">impl</span> <span class="ident">Interest</span> <span class="kw">for</span> <span class="ident">i32</span> {
<span class="kw">fn</span> <span class="ident">interesting</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="ident">bool</span> { <span class="kw-2">*</span><span class="self">self</span> <span class="op">&gt;</span> <span class="number">0</span> }
}</pre>
<h3 id="custom-derive-1" class="section-header"><a href="#custom-derive-1">Custom Derive</a></h3>
<pre class="rust rust-example-rendered">
<span class="attribute">#[<span class="ident">macro_use</span>]</span>
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">synstructure</span>;
<span class="attribute">#[<span class="ident">macro_use</span>]</span>
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">quote</span>;
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">proc_macro2</span>;
<span class="kw">fn</span> <span class="ident">interest_derive</span>(<span class="kw-2">mut</span> <span class="ident">s</span>: <span class="ident">synstructure</span>::<span class="ident">Structure</span>) <span class="op">-&gt;</span> <span class="ident">proc_macro2</span>::<span class="ident">TokenStream</span> {
<span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">fold</span>(<span class="bool-val">false</span>, <span class="op">|</span><span class="ident">acc</span>, <span class="ident">bi</span><span class="op">|</span> <span class="macro">quote</span><span class="macro">!</span>{
#<span class="ident">acc</span> <span class="op">||</span> <span class="ident">synstructure_test_traits</span>::<span class="ident">Interest</span>::<span class="ident">interesting</span>(#<span class="ident">bi</span>)
});
<span class="ident">s</span>.<span class="ident">gen_impl</span>(<span class="macro">quote</span><span class="macro">!</span> {
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">synstructure_test_traits</span>;
<span class="ident">gen</span> <span class="kw">impl</span> <span class="ident">synstructure_test_traits</span>::<span class="ident">Interest</span> <span class="kw">for</span> @<span class="self">Self</span> {
<span class="kw">fn</span> <span class="ident">interesting</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="ident">bool</span> {
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
#<span class="ident">body</span>
}
}
}
})
}
<span class="macro">decl_derive</span><span class="macro">!</span>([<span class="ident">Interest</span>] <span class="op">=&gt;</span> <span class="ident">interest_derive</span>);
<span class="comment">/*
* Test Case
*/</span>
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="macro">test_derive</span><span class="macro">!</span>{
<span class="ident">interest_derive</span> {
<span class="kw">enum</span> <span class="ident">A</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">B</span>(<span class="ident">i32</span>, <span class="ident">T</span>),
<span class="ident">C</span>(<span class="ident">i32</span>),
}
}
<span class="ident">expands</span> <span class="ident">to</span> {
<span class="attribute">#[<span class="ident">allow</span>(<span class="ident">non_upper_case_globals</span>)]</span>
<span class="kw">const</span> <span class="ident">_DERIVE_synstructure_test_traits_Interest_FOR_A</span>: () <span class="op">=</span> {
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">synstructure_test_traits</span>;
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">synstructure_test_traits</span>::<span class="ident">Interest</span> <span class="kw">for</span> <span class="ident">A</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>
<span class="kw">where</span> <span class="ident">T</span>: <span class="ident">synstructure_test_traits</span>::<span class="ident">Interest</span>
{
<span class="kw">fn</span> <span class="ident">interesting</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="ident">bool</span> {
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="ident">A</span>::<span class="ident">B</span>(<span class="kw-2">ref</span> <span class="ident">__binding_0</span>, <span class="kw-2">ref</span> <span class="ident">__binding_1</span>,) <span class="op">=&gt;</span> {
<span class="bool-val">false</span> <span class="op">||</span>
<span class="ident">synstructure_test_traits</span>::<span class="ident">Interest</span>::<span class="ident">interesting</span>(<span class="ident">__binding_0</span>) <span class="op">||</span>
<span class="ident">synstructure_test_traits</span>::<span class="ident">Interest</span>::<span class="ident">interesting</span>(<span class="ident">__binding_1</span>)
}
<span class="ident">A</span>::<span class="ident">C</span>(<span class="kw-2">ref</span> <span class="ident">__binding_0</span>,) <span class="op">=&gt;</span> {
<span class="bool-val">false</span> <span class="op">||</span>
<span class="ident">synstructure_test_traits</span>::<span class="ident">Interest</span>::<span class="ident">interesting</span>(<span class="ident">__binding_0</span>)
}
}
}
}
};
}
}
}</pre>
<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><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
<table>
<tr class=' module-item'>
<td><a class="macro" href="macro.decl_derive.html"
title='macro synstructure::decl_derive'>decl_derive</a></td>
<td class='docblock-short'>
<p>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.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="macro" href="macro.quote.html"
title='macro synstructure::quote'>quote</a></td>
<td class='docblock-short'>
<p>The whole point.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="macro" href="macro.quote_spanned.html"
title='macro synstructure::quote_spanned'>quote_spanned</a></td>
<td class='docblock-short'>
<p>Same as <code>quote!</code>, but applies a given span to all tokens originating within
the macro invocation.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="macro" href="macro.test_derive.html"
title='macro synstructure::test_derive'>test_derive</a></td>
<td class='docblock-short'>
<p>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.</p>
</td>
</tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
<tr class=' module-item'>
<td><a class="struct" href="struct.BindingInfo.html"
title='struct synstructure::BindingInfo'>BindingInfo</a></td>
<td class='docblock-short'>
<p>Information about a specific binding. This contains both an <code>Ident</code>
reference to the given field, and the syn <code>&amp;'a Field</code> descriptor for that
field.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Structure.html"
title='struct synstructure::Structure'>Structure</a></td>
<td class='docblock-short'>
<p>A wrapper around a <code>syn::DeriveInput</code> which provides utilities for creating
custom derive trait implementations.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.VariantAst.html"
title='struct synstructure::VariantAst'>VariantAst</a></td>
<td class='docblock-short'>
<p>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.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.VariantInfo.html"
title='struct synstructure::VariantInfo'>VariantInfo</a></td>
<td class='docblock-short'>
<p>A wrapper around a <code>syn::DeriveInput</code>'s variant which provides utilities
for destructuring <code>Variant</code>s with <code>match</code> expressions.</p>
</td>
</tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
<table>
<tr class=' module-item'>
<td><a class="enum" href="enum.BindStyle.html"
title='enum synstructure::BindStyle'>BindStyle</a></td>
<td class='docblock-short'>
<p>The type of binding to use when generating a pattern.</p>
</td>
</tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
<table>
<tr class=' module-item'>
<td><a class="fn" href="fn.unpretty_print.html"
title='fn synstructure::unpretty_print'>unpretty_print</a></td>
<td class='docblock-short'>
<p>Dumps an unpretty version of a tokenstream. Takes any type which implements
<code>Display</code>.</p>
</td>
</tr></table></section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
<aside id="help" class="hidden">
<div>
<h1 class="hidden">Help</h1>
<div class="shortcuts">
<h2>Keyboard Shortcuts</h2>
<dl>
<dt><kbd>?</kbd></dt>
<dd>Show this help dialog</dd>
<dt><kbd>S</kbd></dt>
<dd>Focus the search field</dd>
<dt><kbd></kbd></dt>
<dd>Move up in search results</dd>
<dt><kbd></kbd></dt>
<dd>Move down in search results</dd>
<dt><kbd></kbd></dt>
<dd>Switch tab</dd>
<dt><kbd>&#9166;</kbd></dt>
<dd>Go to active search result</dd>
<dt><kbd>+</kbd></dt>
<dd>Expand all sections</dd>
<dt><kbd>-</kbd></dt>
<dd>Collapse all sections</dd>
</dl>
</div>
<div class="infos">
<h2>Search Tricks</h2>
<p>
Prefix searches with a type followed by a colon (e.g.
<code>fn:</code>) to restrict the search to a given type.
</p>
<p>
Accepted types are: <code>fn</code>, <code>mod</code>,
<code>struct</code>, <code>enum</code>,
<code>trait</code>, <code>type</code>, <code>macro</code>,
and <code>const</code>.
</p>
<p>
Search functions by type signature (e.g.
<code>vec -> usize</code> or <code>* -> vec</code>)
</p>
</div>
</div>
</aside>
<script>
window.rootPath = "../";
window.currentCrate = "synstructure";
</script>
<script src="../main.js"></script>
<script defer src="../search-index.js"></script>
</body>
</html>