index.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="generator" content="rustdoc">
  7. <meta name="description" content="API documentation for the Rust `enum_primitive` crate.">
  8. <meta name="keywords" content="rust, rustlang, rust-lang, enum_primitive">
  9. <title>enum_primitive - Rust</title>
  10. <link rel="stylesheet" type="text/css" href="../normalize.css">
  11. <link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle">
  12. <link rel="stylesheet" type="text/css" href="../dark.css">
  13. <link rel="stylesheet" type="text/css" href="../main.css" id="themeStyle">
  14. <script src="../storage.js"></script>
  15. </head>
  16. <body class="rustdoc mod">
  17. <!--[if lte IE 8]>
  18. <div class="warning">
  19. This old browser is unsupported and will most likely display funky
  20. things.
  21. </div>
  22. <![endif]-->
  23. <nav class="sidebar">
  24. <div class="sidebar-menu">&#9776;</div>
  25. <p class='location'>Crate enum_primitive</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#macros">Macros</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'enum_primitive', ty: 'mod', relpath: '../'};</script></div>
  26. </nav>
  27. <div class="theme-picker">
  28. <button id="theme-picker" aria-label="Pick another theme!">
  29. <img src="../brush.svg" width="18" alt="Pick another theme!">
  30. </button>
  31. <div id="theme-choices"></div>
  32. </div>
  33. <script src="../theme.js"></script>
  34. <nav class="sub">
  35. <form class="search-form js-only">
  36. <div class="search-container">
  37. <input class="search-input" name="search"
  38. autocomplete="off"
  39. placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
  40. type="search">
  41. </div>
  42. </form>
  43. </nav>
  44. <section id='main' class="content">
  45. <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'>
  46. <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
  47. [<span class='inner'>&#x2212;</span>]
  48. </a>
  49. </span><a class='srclink' href='../src/enum_primitive/lib.rs.html#23-196' title='goto source code'>[src]</a></span></h1>
  50. <div class='docblock'><p>This crate exports a macro <code>enum_from_primitive!</code> that wraps an
  51. <code>enum</code> declaration and automatically adds an implementation of
  52. <code>num::FromPrimitive</code> (reexported here), to allow conversion from
  53. primitive integers to the enum. It therefore provides an
  54. alternative to the built-in <code>#[derive(FromPrimitive)]</code>, which
  55. requires the unstable <code>std::num::FromPrimitive</code> and is disabled in
  56. Rust 1.0.</p>
  57. <h1 id="example" class="section-header"><a href="#example">Example</a></h1>
  58. <pre class="rust rust-example-rendered">
  59. <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>;
  60. <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">num_traits</span>;
  61. <span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">FromPrimitive</span>;
  62. <span class="macro">enum_from_primitive</span><span class="macro">!</span> {
  63. <span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span>
  64. <span class="kw">enum</span> <span class="ident">FooBar</span> {
  65. <span class="ident">Foo</span> <span class="op">=</span> <span class="number">17</span>,
  66. <span class="ident">Bar</span> <span class="op">=</span> <span class="number">42</span>,
  67. <span class="ident">Baz</span>,
  68. }
  69. }
  70. <span class="kw">fn</span> <span class="ident">main</span>() {
  71. <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>));
  72. <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>));
  73. <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>));
  74. <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>);
  75. }</pre>
  76. </div><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
  77. <table>
  78. <tr class=' module-item'>
  79. <td><a class="macro" href="macro.enum_from_primitive.html"
  80. title='macro enum_primitive::enum_from_primitive'>enum_from_primitive</a></td>
  81. <td class='docblock-short'>
  82. <p>Wrap this macro around an <code>enum</code> declaration to get an
  83. automatically generated implementation of <code>num::FromPrimitive</code>.</p>
  84. </td>
  85. </tr>
  86. <tr class=' module-item'>
  87. <td><a class="macro" href="macro.enum_from_primitive_impl.html"
  88. title='macro enum_primitive::enum_from_primitive_impl'>enum_from_primitive_impl</a></td>
  89. <td class='docblock-short'>
  90. <p>Helper macro for internal use by <code>enum_from_primitive!</code>.</p>
  91. </td>
  92. </tr>
  93. <tr class=' module-item'>
  94. <td><a class="macro" href="macro.enum_from_primitive_impl_ty.html"
  95. title='macro enum_primitive::enum_from_primitive_impl_ty'>enum_from_primitive_impl_ty</a></td>
  96. <td class='docblock-short'>
  97. <p>Helper macro for internal use by <code>enum_from_primitive!</code>.</p>
  98. </td>
  99. </tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
  100. <table>
  101. <tr class=' module-item'>
  102. <td><a class="enum" href="enum.Option.html"
  103. title='enum enum_primitive::Option'>Option</a></td>
  104. <td class='docblock-short'>
  105. <p>The <code>Option</code> type. See <a href="index.html">the module level documentation</a> for more.</p>
  106. </td>
  107. </tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
  108. <table>
  109. <tr class=' module-item'>
  110. <td><a class="trait" href="trait.FromPrimitive.html"
  111. title='trait enum_primitive::FromPrimitive'>FromPrimitive</a></td>
  112. <td class='docblock-short'>
  113. <p>A generic trait for converting a number to a value.</p>
  114. </td>
  115. </tr></table></section>
  116. <section id='search' class="content hidden"></section>
  117. <section class="footer"></section>
  118. <aside id="help" class="hidden">
  119. <div>
  120. <h1 class="hidden">Help</h1>
  121. <div class="shortcuts">
  122. <h2>Keyboard Shortcuts</h2>
  123. <dl>
  124. <dt><kbd>?</kbd></dt>
  125. <dd>Show this help dialog</dd>
  126. <dt><kbd>S</kbd></dt>
  127. <dd>Focus the search field</dd>
  128. <dt><kbd>↑</kbd></dt>
  129. <dd>Move up in search results</dd>
  130. <dt><kbd>↓</kbd></dt>
  131. <dd>Move down in search results</dd>
  132. <dt><kbd>↹</kbd></dt>
  133. <dd>Switch tab</dd>
  134. <dt><kbd>&#9166;</kbd></dt>
  135. <dd>Go to active search result</dd>
  136. <dt><kbd>+</kbd></dt>
  137. <dd>Expand all sections</dd>
  138. <dt><kbd>-</kbd></dt>
  139. <dd>Collapse all sections</dd>
  140. </dl>
  141. </div>
  142. <div class="infos">
  143. <h2>Search Tricks</h2>
  144. <p>
  145. Prefix searches with a type followed by a colon (e.g.
  146. <code>fn:</code>) to restrict the search to a given type.
  147. </p>
  148. <p>
  149. Accepted types are: <code>fn</code>, <code>mod</code>,
  150. <code>struct</code>, <code>enum</code>,
  151. <code>trait</code>, <code>type</code>, <code>macro</code>,
  152. and <code>const</code>.
  153. </p>
  154. <p>
  155. Search functions by type signature (e.g.
  156. <code>vec -> usize</code> or <code>* -> vec</code>)
  157. </p>
  158. </div>
  159. </div>
  160. </aside>
  161. <script>
  162. window.rootPath = "../";
  163. window.currentCrate = "enum_primitive";
  164. </script>
  165. <script src="../main.js"></script>
  166. <script defer src="../search-index.js"></script>
  167. </body>
  168. </html>