mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-25 10:22:19 +00:00
97 lines
11 KiB
HTML
97 lines
11 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="API documentation for the Rust `enum_methods` crate."><meta name="keywords" content="rust, rustlang, rust-lang, enum_methods"><title>enum_methods - 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="../light.css" id="themeStyle"><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="../favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></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">☰</div><a href='../enum_methods/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><p class='location'>Crate enum_methods</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all enum_methods's items</p></a><p class='location'></p><script>window.sidebarCurrent = {name: 'enum_methods', 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"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><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'>−</span>]</a></span><a class='srclink' href='../src/enum_methods/lib.rs.html#1-175' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>enum_methods</a></span></h1><div class='docblock'><h1 id="enum-methods" class="section-header"><a href="#enum-methods">enum-methods</a></h1>
|
||
<p><a href="https://travis-ci.org/alekratz/enum-methods"><img src="https://travis-ci.org/alekratz/enum-methods.svg?branch=master" alt="Build Status" /></a>
|
||
<a href="https://crates.io/crates/enum-methods"><img src="https://img.shields.io/crates/v/enum-methods.svg" alt="crates.io" /></a></p>
|
||
<p>Enum getter/<code>is_*</code> method generation.</p>
|
||
<h4 id="please-note-that-this-crate-is-unstable-and-is-subject-to-change-frequently" class="section-header"><a href="#please-note-that-this-crate-is-unstable-and-is-subject-to-change-frequently">Please note that this crate is unstable and is subject to change frequently.</a></h4>
|
||
<p>I will attempt to prevent <em>seriously</em> breaking changes after we hit 0.1.0.</p>
|
||
<h1 id="links" class="section-header"><a href="#links">Links</a></h1>
|
||
<ul>
|
||
<li><a href="https://github.com/alekratz/enum-methods">Github</a></li>
|
||
<li><a href="https://crates.io/crates/enum-methods">crates.io</a></li>
|
||
<li><a href="https://docs.rs/enum-methods/0.0.8/enum_methods/">docs.rs</a></li>
|
||
</ul>
|
||
<h1 id="usage" class="section-header"><a href="#usage">Usage</a></h1>
|
||
<p>In your <code>Cargo.toml</code>, add this line under your <code>[dependencies]</code> section:</p>
|
||
<pre><code class="language-toml,no_run">enum-methods = "0.0.8"
|
||
</code></pre>
|
||
<p>To use, simply derive and call methods (see the example below).</p>
|
||
<h1 id="why" class="section-header"><a href="#why">Why?</a></h1>
|
||
<p>Usually when you write an enum with one or zero values, you might want to
|
||
add a set of getters for them. As such:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>)]</span>
|
||
<span class="kw">enum</span> <span class="ident">MyEnum</span> {
|
||
<span class="ident">FooBarBaz</span>(<span class="ident">i64</span>),
|
||
<span class="ident">BazBarFoo</span>(<span class="ident">String</span>),
|
||
<span class="comment">// ... and others</span>
|
||
}
|
||
|
||
<span class="kw">impl</span> <span class="ident">MyEnum</span> {
|
||
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">foo_bar_baz</span>(<span class="kw-2">&</span><span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">i64</span> {
|
||
<span class="kw">if</span> <span class="kw">let</span> <span class="kw-2">&</span><span class="ident">MyEnum</span>::<span class="ident">FooBarBaz</span>(<span class="ident">i</span>) <span class="op">=</span> <span class="self">self</span> {
|
||
<span class="ident">i</span>
|
||
}
|
||
<span class="kw">else</span> {
|
||
<span class="macro">panic</span><span class="macro">!</span>(<span class="string">"called MyEnum::FooBarBaz() on {:?}"</span>, <span class="self">self</span>)
|
||
}
|
||
}
|
||
<span class="comment">// et cetera</span>
|
||
}
|
||
</pre></div>
|
||
<p>But this gets tedious, and adds a lot code for this simple functionality.
|
||
Enter <code>enum-methods</code>.</p>
|
||
<p>Instead of doing the above with the <code>if let ... else { panic!(...) }</code>, you
|
||
simply derive from the <code>EnumIntoGetters</code></p>
|
||
|
||
<div class="example-wrap"><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">enum_methods</span>;
|
||
|
||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">EnumIntoGetters</span>, <span class="ident">EnumAsGetters</span>, <span class="ident">EnumIsA</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">enum</span> <span class="ident">MyEnum</span> {
|
||
<span class="ident">FooBarBaz</span>(<span class="ident">i64</span>),
|
||
<span class="ident">BazBarFoo</span>(<span class="ident">String</span>),
|
||
<span class="comment">// ... and others</span>
|
||
}
|
||
|
||
<span class="kw">fn</span> <span class="ident">main</span>() {
|
||
<span class="kw">let</span> <span class="ident">my_foo</span> <span class="op">=</span> <span class="ident">MyEnum</span>::<span class="ident">FooBarBaz</span>(<span class="number">42</span>);
|
||
<span class="comment">// EnumIsA - creates is_* methods for every member</span>
|
||
<span class="kw">if</span> <span class="ident">my_foo</span>.<span class="ident">is_foo_bar_baz</span>() {
|
||
<span class="comment">// EnumAsGetters - gets a reference to the enum, panicking if it is</span>
|
||
<span class="comment">// not the specified variant</span>
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">my_foo</span>.<span class="ident">as_foo_bar_baz</span>(), <span class="number">42</span>);
|
||
<span class="comment">// EnumIntoGetters - consumes the enum, yielding its owned value,</span>
|
||
<span class="comment">// and panicking if it is not the specified variant</span>
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">my_foo</span>.<span class="ident">into_foo_bar_baz</span>(), <span class="number">42</span>);
|
||
}
|
||
}</pre></div>
|
||
<h1 id="requirements-and-gotchas" class="section-header"><a href="#requirements-and-gotchas">Requirements and gotchas</a></h1>
|
||
<p>Right now, <code>enum-methods</code> has four derivable options:</p>
|
||
<ul>
|
||
<li><code>EnumAsGetters</code> for generating <code>as_*</code> methods, which return a reference.</li>
|
||
<li><code>EnumIntoGetters</code> for generating <code>into_*</code> methods, which consume the enum
|
||
and returns the data held by the variant.</li>
|
||
<li><code>EnumToGetters</code> for generating <code>to_*</code> methods, which returns a clone of
|
||
the data held by the variant.</li>
|
||
<li><code>EnumIsA</code> for generating <code>is_*</code> methods, which return a boolean indicating
|
||
whether the enum matches that variant.</li>
|
||
</ul>
|
||
<p><code>EnumAsGetters</code>, <code>EnumIntoGetters</code>, and <code>EnumToGetters</code> have some limitations.</p>
|
||
<ul>
|
||
<li>Any enum variant which has exactly 1 member will have a getter generated for
|
||
it. All other variants are ignored.</li>
|
||
<li>Enums which derive from <code>EnumIntoGetters</code> must also derive from <code>Debug</code> - this
|
||
is for when a method is called for the wrong variant and needs to <code>panic!</code>.</li>
|
||
</ul>
|
||
<p>Furthermore, <code>EnumToGetters</code> is <em>only</em> for enums whose variants implement
|
||
<code>Clone</code>. There is not yet support for th</p>
|
||
<p><code>EnumIsA</code> is much simpler than the previous; it simply adds <code>is_*</code>
|
||
methods returning a boolean for whether the variant matches or not.</p>
|
||
<p><strong>For all generated methods, all names are automatically converted to
|
||
snake_case</strong>.</p>
|
||
<h1 id="license" class="section-header"><a href="#license">License</a></h1>
|
||
<p>This software is released under the Apache license 2.0. See the LICENSE file
|
||
for more details.</p>
|
||
</div></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>⏎</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><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "enum_methods";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html> |