<!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_primitive` crate.">
<meta name="keywords" content="rust, rustlang, rust-lang, enum_primitive">
<title>enum_primitive - Rust</title>
<link rel="stylesheet" type="text/css" href="../normalize.css">
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
<link rel="stylesheet" type="text/css" href="../main.css">
</head>
<body class="rustdoc mod">
<nav class="sidebar">
<p class='location'></p><script>window.sidebarCurrent = {name: 'enum_primitive', ty: 'mod', relpath: '../'};</script>
</nav>
<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=''>enum_primitive</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'>−</span>]
</a>
</span><a class='srclink' href='../src/enum_primitive/lib.rs.html#23-196' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>This crate exports a macro <code>enum_from_primitive!</code> that wraps an
<code>enum</code> declaration and automatically adds an implementation of
<code>num::FromPrimitive</code> (reexported here), to allow conversion from
primitive integers to the enum. It therefore provides an
alternative to the built-in <code>#[derive(FromPrimitive)]</code>, which
requires the unstable <code>std::num::FromPrimitive</code> and is disabled in
Rust 1.0.</p>
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
<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_primitive</span>;
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">num_traits</span>;
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">FromPrimitive</span>;
<span class="macro">enum_from_primitive</span><span class="macro">!</span> {
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span>
<span class="kw">enum</span> <span class="ident">FooBar</span> {
<span class="ident">Foo</span> <span class="op">=</span> <span class="number">17</span>,
<span class="ident">Bar</span> <span class="op">=</span> <span class="number">42</span>,
<span class="ident">Baz</span>,
}
}
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">FooBar</span>::<span class="ident">from_i32</span>(<span class="number">17</span>), <span class="prelude-val">Some</span>(<span class="ident">FooBar</span>::<span class="ident">Foo</span>));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">FooBar</span>::<span class="ident">from_i32</span>(<span class="number">42</span>), <span class="prelude-val">Some</span>(<span class="ident">FooBar</span>::<span class="ident">Bar</span>));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">FooBar</span>::<span class="ident">from_i32</span>(<span class="number">43</span>), <span class="prelude-val">Some</span>(<span class="ident">FooBar</span>::<span class="ident">Baz</span>));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">FooBar</span>::<span class="ident">from_i32</span>(<span class="number">91</span>), <span class="prelude-val">None</span>);
}</pre>
</div><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
<table>
<tr class=' module-item'>
<td><a class="macro" href="macro.enum_from_primitive.html"
title='macro enum_primitive::enum_from_primitive'>enum_from_primitive</a></td>
<td class='docblock-short'>
<p>Wrap this macro around an <code>enum</code> declaration to get an
automatically generated implementation of <code>num::FromPrimitive</code>.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="macro" href="macro.enum_from_primitive_impl.html"
title='macro enum_primitive::enum_from_primitive_impl'>enum_from_primitive_impl</a></td>
<td class='docblock-short'>
<p>Helper macro for internal use by <code>enum_from_primitive!</code>.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="macro" href="macro.enum_from_primitive_impl_ty.html"
title='macro enum_primitive::enum_from_primitive_impl_ty'>enum_from_primitive_impl_ty</a></td>
<td class='docblock-short'>
<p>Helper macro for internal use by <code>enum_from_primitive!</code>.</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.Option.html"
title='enum enum_primitive::Option'>Option</a></td>
<td class='docblock-short'>
<p>The <code>Option</code> type. See <a href="index.html">the module level documentation</a> for more.</p>
</td>
</tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
<table>
<tr class=' module-item'>
<td><a class="trait" href="trait.FromPrimitive.html"
title='trait enum_primitive::FromPrimitive'>FromPrimitive</a></td>
<td class='docblock-short'>
<p>A generic trait for converting a number to a value.</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>?</dt>
<dd>Show this help dialog</dd>
<dt>S</dt>
<dd>Focus the search field</dd>
<dt>⇤</dt>
<dd>Move up in search results</dd>
<dt>⇥</dt>
<dd>Move down in search results</dd>
<dt>⏎</dt>
<dd>Go to active search result</dd>
<dt>+</dt>
<dd>Collapse/expand 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 = "enum_primitive";
</script>
<script src="../jquery.js"></script>
<script src="../main.js"></script>
<script defer src="../search-index.js"></script>
</body>
</html>