num_enum-0.7.5/.cargo_vcs_info.json0000644000000001460000000000100126710ustar { "git": { "sha1": "984e150eb0753cfe17ac514e714d443db4aeea4a" }, "path_in_vcs": "num_enum" }num_enum-0.7.5/Cargo.lock0000644000000225570000000000100106560ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "anyhow" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "glob" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "hashbrown" version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "indexmap" version = "2.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", "hashbrown", ] [[package]] name = "itoa" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "num_enum" version = "0.7.5" dependencies = [ "anyhow", "num_enum_derive", "paste", "rustversion", "trybuild", "walkdir", ] [[package]] name = "num_enum_derive" version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff32365de1b6743cb203b710788263c44a03de03802daf96092f2da4fe6ba4d7" dependencies = [ "proc-macro-crate", "proc-macro2", "quote", "syn", ] [[package]] name = "paste" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "proc-macro-crate" version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" dependencies = [ "toml_edit 0.21.1", ] [[package]] name = "proc-macro2" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "rustversion" version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" [[package]] name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] [[package]] name = "serde" version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.121" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ab380d7d9f22ef3f21ad3e6c1ebe8e4fc7a2000ccba2e4d71fc96f15b2cb609" dependencies = [ "itoa", "memchr", "ryu", "serde", ] [[package]] name = "serde_spanned" version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" dependencies = [ "serde", ] [[package]] name = "syn" version = "2.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "termcolor" version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" dependencies = [ "winapi-util", ] [[package]] name = "toml" version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81967dd0dd2c1ab0bc3468bd7caecc32b8a4aa47d0c8c695d8c2b2108168d62c" dependencies = [ "serde", "serde_spanned", "toml_datetime", "toml_edit 0.22.17", ] [[package]] name = "toml_datetime" version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fb9f64314842840f1d940ac544da178732128f1c78c21772e876579e0da1db" dependencies = [ "serde", ] [[package]] name = "toml_edit" version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" dependencies = [ "indexmap", "toml_datetime", "winnow 0.5.40", ] [[package]] name = "toml_edit" version = "0.22.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d9f8729f5aea9562aac1cc0441f5d6de3cff1ee0c5d67293eeca5eb36ee7c16" dependencies = [ "indexmap", "serde", "serde_spanned", "toml_datetime", "winnow 0.6.16", ] [[package]] name = "trybuild" version = "1.0.98" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b55265878356bdd85c9baa15859c87de93b2bf1f33acf752040a561e4a228f62" dependencies = [ "glob", "serde", "serde_derive", "serde_json", "termcolor", "toml", ] [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "walkdir" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", ] [[package]] name = "winapi-util" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ "windows-sys", ] [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" version = "0.5.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" dependencies = [ "memchr", ] [[package]] name = "winnow" version = "0.6.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b480ae9340fc261e6be3e95a1ba86d54ae3f9171132a73ce8d4bbaf68339507c" dependencies = [ "memchr", ] num_enum-0.7.5/Cargo.toml0000644000000042570000000000100106760ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.70.0" name = "num_enum" version = "0.7.5" authors = [ "Daniel Wagner-Hall ", "Daniel Henry-Mantilla ", "Vincent Esche ", ] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Procedural macros to make inter-operation between primitives and enums easier." readme = "README.md" keywords = [ "enum", "conversion", "safe", "ffi", "derive", ] categories = ["rust-patterns"] license = "BSD-3-Clause OR MIT OR Apache-2.0" repository = "https://github.com/illicitonion/num_enum" [package.metadata.docs.rs] features = ["external_doc"] [badges.maintenance] status = "passively-maintained" [features] complex-expressions = ["num_enum_derive/complex-expressions"] default = ["std"] external_doc = [] std = ["num_enum_derive/std"] [lib] name = "num_enum" path = "src/lib.rs" [[test]] name = "default" path = "tests/default.rs" [[test]] name = "from_primitive" path = "tests/from_primitive.rs" [[test]] name = "into_primitive" path = "tests/into_primitive.rs" [[test]] name = "renamed_num_enum" path = "tests/renamed_num_enum.rs" [[test]] name = "try_build" path = "tests/try_build.rs" [[test]] name = "try_from_primitive" path = "tests/try_from_primitive.rs" [[test]] name = "unsafe_from_primitive" path = "tests/unsafe_from_primitive.rs" [dependencies.num_enum_derive] version = "=0.7.5" default-features = false [dependencies.rustversion] version = "1.0.4" [dev-dependencies.anyhow] version = "1.0.14" [dev-dependencies.paste] version = "1" [dev-dependencies.trybuild] version = "1.0.98" [dev-dependencies.walkdir] version = "2" num_enum-0.7.5/Cargo.toml.orig000064400000000000000000000021721046102023000143510ustar 00000000000000[package] name = "num_enum" version = "0.7.5" # Keep in sync with num_enum_derive, the the dependency on it below. rust-version = "1.70.0" authors = [ "Daniel Wagner-Hall ", "Daniel Henry-Mantilla ", "Vincent Esche ", ] description = "Procedural macros to make inter-operation between primitives and enums easier." edition = "2021" repository = "https://github.com/illicitonion/num_enum" readme = "README.md" keywords = ["enum", "conversion", "safe", "ffi", "derive"] categories = ["rust-patterns"] license = "BSD-3-Clause OR MIT OR Apache-2.0" [features] std = ["num_enum_derive/std"] complex-expressions = ["num_enum_derive/complex-expressions"] external_doc = [] default = ["std"] # disable to use in a `no_std` environment [package.metadata.docs.rs] features = ["external_doc"] [badges] maintenance = { status = "passively-maintained" } [dependencies] rustversion = "1.0.4" num_enum_derive = { version = "=0.7.5", path = "../num_enum_derive", default-features = false } [dev-dependencies] anyhow = "1.0.14" paste = "1" trybuild = "1.0.98" walkdir = "2" num_enum-0.7.5/LICENSE-APACHE000064400000000000000000000227731046102023000134170ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS num_enum-0.7.5/LICENSE-BSD000064400000000000000000000027141046102023000130770ustar 00000000000000Copyright (c) 2018, Daniel Wagner-Hall All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of num_enum nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. num_enum-0.7.5/LICENSE-MIT000064400000000000000000000017771046102023000131300ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. num_enum-0.7.5/README.md000064400000000000000000000200601046102023000127350ustar 00000000000000# num_enum Procedural macros to make inter-operation between primitives and enums easier. This crate is no_std compatible. [![crates.io](https://img.shields.io/crates/v/num_enum.svg)](https://crates.io/crates/num_enum) [![Documentation](https://docs.rs/num_enum/badge.svg)](https://docs.rs/num_enum) [![Build Status](https://travis-ci.org/illicitonion/num_enum.svg?branch=master)](https://travis-ci.org/illicitonion/num_enum) ## Turning an enum into a primitive ```rust use num_enum::IntoPrimitive; #[derive(IntoPrimitive)] #[repr(u8)] enum Number { Zero, One, } fn main() { let zero: u8 = Number::Zero.into(); assert_eq!(zero, 0u8); } ``` `num_enum`'s `IntoPrimitive` is more type-safe than using `as`, because `as` will silently truncate - `num_enum` only derives `From` for exactly the discriminant type of the enum. ## Attempting to turn a primitive into an enum with try_from ```rust use num_enum::TryFromPrimitive; use std::convert::TryFrom; #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Number { Zero, One, } fn main() { let zero = Number::try_from(0u8); assert_eq!(zero, Ok(Number::Zero)); let three = Number::try_from(3u8); assert_eq!( three.unwrap_err().to_string(), "No discriminant in enum `Number` matches the value `3`", ); } ``` ### Variant alternatives Sometimes a single enum variant might be representable by multiple numeric values. The `#[num_enum(alternatives = [..])]` attribute allows you to define additional value alternatives for individual variants. (The behavior of `IntoPrimitive` is unaffected by this attribute, it will always return the canonical value.) ```rust use num_enum::TryFromPrimitive; use std::convert::TryFrom; #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Number { Zero = 0, #[num_enum(alternatives = [2])] OneOrTwo = 1, } fn main() { let zero = Number::try_from(0u8); assert_eq!(zero, Ok(Number::Zero)); let one = Number::try_from(1u8); assert_eq!(one, Ok(Number::OneOrTwo)); let two = Number::try_from(2u8); assert_eq!(two, Ok(Number::OneOrTwo)); let three = Number::try_from(3u8); assert_eq!( three.unwrap_err().to_string(), "No discriminant in enum `Number` matches the value `3`", ); } ``` Range expressions are also supported for alternatives, but this requires enabling the `complex-expressions` feature: ```rust use num_enum::TryFromPrimitive; use std::convert::TryFrom; #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Number { Zero = 0, #[num_enum(alternatives = [2..16])] Some = 1, #[num_enum(alternatives = [17, 18..=255])] Many = 16, } fn main() { let zero = Number::try_from(0u8); assert_eq!(zero, Ok(Number::Zero)); let some = Number::try_from(15u8); assert_eq!(some, Ok(Number::Some)); let many = Number::try_from(255u8); assert_eq!(many, Ok(Number::Many)); } ``` ### Custom error types `TryFromPrimitive` by default will use `num_enum::TryFromPrimitiveError` as its `Error` type. If you want to use a different type, you can use an annotation for this: ```rust use num_enum::TryFromPrimitive; #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[num_enum(error_type(name = CustomError, constructor = CustomError::new))] #[repr(u8)] enum FirstNumber { Zero, One, Two, } struct CustomError {} impl CustomError { fn new(value: u8) -> CustomError { CustomError {} } } ``` ## Safely turning a primitive into an exhaustive enum with from_primitive If your enum has all possible primitive values covered, you can derive `FromPrimitive` for it (which auto-implement stdlib's `From`): You can cover all possible values by: * Having variants for every possible value * Having a variant marked `#[num_enum(default)]` * Having a variant marked `#[num_enum(catch_all)]` * Having `#[num_enum(alternatives = [...])`s covering values not covered by a variant. ```rust use num_enum::FromPrimitive; #[derive(Debug, Eq, PartialEq, FromPrimitive)] #[repr(u8)] enum Number { Zero, #[num_enum(default)] NonZero, } fn main() { assert_eq!( Number::Zero, Number::from(0_u8), ); assert_eq!( Number::NonZero, Number::from(1_u8), ); } ``` ### Default variant Sometimes it is desirable to have an `Other` variant in an enum that acts as a kind of a wildcard matching all the value not yet covered by other variants. The `#[num_enum(default)]` attribute (or the stdlib `#[default]` attribute) allows you to mark variant as the default. (The behavior of `IntoPrimitive` is unaffected by this attribute, it will always return the canonical value.) ```rust use num_enum::FromPrimitive; use std::convert::TryFrom; #[derive(Debug, Eq, PartialEq, FromPrimitive)] #[repr(u8)] enum Number { Zero = 0, #[num_enum(default)] NonZero = 1, } fn main() { let zero = Number::from(0u8); assert_eq!(zero, Number::Zero); let one = Number::from(1u8); assert_eq!(one, Number::NonZero); let two = Number::from(2u8); assert_eq!(two, Number::NonZero); } ``` Only `FromPrimitive` pays attention to `default` attributes, `TryFromPrimitive` ignores them. ### Catch-all variant Sometimes it is desirable to have an `Other` variant which holds the otherwise un-matched value as a field. The `#[num_enum(catch_all)]` attribute allows you to mark at most one variant for this purpose. The variant it's applied to must be a tuple variant with exactly one field matching the `repr` type. ```rust use num_enum::FromPrimitive; use std::convert::TryFrom; #[derive(Debug, Eq, PartialEq, FromPrimitive)] #[repr(u8)] enum Number { Zero = 0, #[num_enum(catch_all)] NonZero(u8), } fn main() { let zero = Number::from(0u8); assert_eq!(zero, Number::Zero); let one = Number::from(1u8); assert_eq!(one, Number::NonZero(1_u8)); let two = Number::from(2u8); assert_eq!(two, Number::NonZero(2_u8)); } ``` As this is naturally exhaustive, this is only supported for `FromPrimitive`, not also `TryFromPrimitive`. ## Unsafely turning a primitive into an enum with unchecked_transmute_from If you're really certain a conversion will succeed (and have not made use of `#[num_enum(default)]` or `#[num_enum(alternatives = [..])]` for any of its variants), and want to avoid a small amount of overhead, you can use unsafe code to do this conversion. Unless you have data showing that the match statement generated in the `try_from` above is a bottleneck for you, you should avoid doing this, as the unsafe code has potential to cause serious memory issues in your program. ```rust use num_enum::UnsafeFromPrimitive; #[derive(Debug, Eq, PartialEq, UnsafeFromPrimitive)] #[repr(u8)] enum Number { Zero, One, } fn main() { assert_eq!( unsafe { Number::unchecked_transmute_from(0_u8) }, Number::Zero, ); assert_eq!( unsafe { Number::unchecked_transmute_from(1_u8) }, Number::One, ); } unsafe fn undefined_behavior() { let _ = Number::unchecked_transmute_from(2); // 2 is not a valid discriminant! } ``` Note that this derive ignores any `default`, `catch_all`, and `alternatives` attributes on the enum. If you need support for conversions from these values, you should use `TryFromPrimitive` or `FromPrimitive`. This means, for instance, that the following is undefined behaviour: ```rust,no_run use num_enum::UnsafeFromPrimitive; #[derive(UnsafeFromPrimitive)] #[repr(u8)] enum Number { Zero = 0, // Same for `#[num_enum(catch_all)]`, and `#[num_enum(alternatives = [2, ...])]` #[num_enum(default)] One = 1, } let _undefined_behavior = unsafe { Number::unchecked_transmute_from(2) }; ``` ## Optional features Some enum values may be composed of complex expressions, for example: ```rust enum Number { Zero = (0, 1).0, One = (0, 1).1, } ``` To cut down on compile time, these are not supported by default, but if you enable the `complex-expressions` feature of your dependency on `num_enum`, these should start working. ## License num_enum may be used under your choice of the BSD 3-clause, Apache 2, or MIT license. num_enum-0.7.5/src/lib.rs000064400000000000000000000065671046102023000134010ustar 00000000000000// Wrap this in two cfg_attrs so that it continues to parse pre-1.54.0. // See https://github.com/rust-lang/rust/issues/82768 #![cfg_attr(feature = "external_doc", cfg_attr(all(), doc = include_str!("../README.md")))] #![cfg_attr( not(feature = "external_doc"), doc = "See for more info about this crate." )] #![cfg_attr(not(feature = "std"), no_std)] pub use ::num_enum_derive::{ Default, FromPrimitive, IntoPrimitive, TryFromPrimitive, UnsafeFromPrimitive, }; use ::core::fmt; pub trait FromPrimitive: Sized { type Primitive: Copy + Eq; fn from_primitive(number: Self::Primitive) -> Self; } pub trait TryFromPrimitive: Sized { type Primitive: Copy + Eq + fmt::Debug; type Error; const NAME: &'static str; fn try_from_primitive(number: Self::Primitive) -> Result; } pub trait UnsafeFromPrimitive: Sized { type Primitive: Copy + Eq; /// Transmutes into an enum from its primitive. /// /// # Safety /// /// - `number` must represent a valid discriminant of `Self`. #[deprecated( since = "0.6.0", note = "Prefer to use `unchecked_transmute_from`, `from_unchecked` will be removed in a future release." )] unsafe fn from_unchecked(number: Self::Primitive) -> Self { Self::unchecked_transmute_from(number) } /// Transmutes into an enum from its primitive. /// /// # Safety /// /// - `number` must represent a valid discriminant of `Self`. unsafe fn unchecked_transmute_from(number: Self::Primitive) -> Self; } #[derive(Copy, Clone, PartialEq, Eq)] pub struct TryFromPrimitiveError { pub number: Enum::Primitive, } impl TryFromPrimitiveError { pub fn new(number: Enum::Primitive) -> Self { Self { number } } } impl fmt::Debug for TryFromPrimitiveError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_struct("TryFromPrimitiveError") .field("number", &self.number) .finish() } } impl fmt::Display for TryFromPrimitiveError { fn fmt(&self, stream: &'_ mut fmt::Formatter<'_>) -> fmt::Result { write!( stream, "No discriminant in enum `{name}` matches the value `{input:?}`", name = Enum::NAME, input = self.number, ) } } #[rustversion::since(1.81)] impl ::core::error::Error for TryFromPrimitiveError {} #[cfg(feature = "std")] #[rustversion::before(1.81)] impl ::std::error::Error for TryFromPrimitiveError {} // This trait exists to try to give a more clear error message when someone attempts to derive both FromPrimitive and TryFromPrimitive. // This isn't allowed because both end up creating a `TryFrom` implementation. // TryFromPrimitive explicitly implements TryFrom with Error=TryFromPrimitiveError, which conflicts with: // FromPrimitive explicitly implements From which has a blanket implementation of TryFrom with Error=Infallible. // // This is a private implementation detail of the num_enum crate which should not be depended on externally. // It is subject to change in any release regardless of semver. #[doc(hidden)] pub trait CannotDeriveBothFromPrimitiveAndTryFromPrimitive {} num_enum-0.7.5/tests/default.rs000064400000000000000000000013231046102023000146130ustar 00000000000000// Guard against https://github.com/illicitonion/num_enum/issues/27 mod alloc {} mod core {} mod num_enum {} mod std {} #[test] fn default() { #[derive(Debug, Eq, PartialEq, ::num_enum::Default)] #[repr(u8)] enum Enum { #[allow(unused)] Zero = 0, #[num_enum(default)] NonZero = 1, } assert_eq!(Enum::NonZero, ::default()); } #[test] fn default_standard_default_attribute() { #[derive(Debug, Eq, PartialEq, ::num_enum::Default)] #[repr(u8)] enum Enum { #[allow(unused)] Zero = 0, #[default] NonZero = 1, } assert_eq!(Enum::NonZero, ::default()); } num_enum-0.7.5/tests/from_primitive.rs000064400000000000000000000102171046102023000162240ustar 00000000000000use ::num_enum::FromPrimitive; // Guard against https://github.com/illicitonion/num_enum/issues/27 mod alloc {} mod core {} mod num_enum {} mod std {} macro_rules! has_from_primitive_number { ( $type:ty ) => { paste::paste! { #[test] fn []() { #[derive(Debug, Eq, PartialEq, FromPrimitive)] #[repr($type)] enum Enum { Zero = 0, #[num_enum(default)] NonZero = 1, } let zero = Enum::from_primitive(0 as $type); assert_eq!(zero, Enum::Zero); let one = Enum::from_primitive(1 as $type); assert_eq!(one, Enum::NonZero); let two = Enum::from_primitive(2 as $type); assert_eq!(two, Enum::NonZero); } } }; } has_from_primitive_number!(u8); has_from_primitive_number!(u16); has_from_primitive_number!(u32); has_from_primitive_number!(u64); has_from_primitive_number!(usize); has_from_primitive_number!(i8); has_from_primitive_number!(i16); has_from_primitive_number!(i32); has_from_primitive_number!(i64); has_from_primitive_number!(isize); // repr with 128-bit type is unstable // has_from_primitive_number!(u128); // has_from_primitive_number!(i128); #[test] fn has_from_primitive_number_standard_default_attribute() { #[derive(Debug, Eq, PartialEq, FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[default] NonZero = 1, } let zero = Enum::from_primitive(0_u8); assert_eq!(zero, Enum::Zero); let one = Enum::from_primitive(1_u8); assert_eq!(one, Enum::NonZero); let two = Enum::from_primitive(2_u8); assert_eq!(two, Enum::NonZero); } #[test] fn from_primitive_number() { #[derive(Debug, Eq, PartialEq, FromPrimitive)] #[repr(u8)] enum Enum { #[num_enum(default)] Whatever = 0, } // #[derive(FromPrimitive)] generates implementations for the following traits: // // - `FromPrimitive` // - `From` // - `TryFromPrimitive` // - `TryFrom` let from_primitive = Enum::from_primitive(0_u8); assert_eq!(from_primitive, Enum::Whatever); let from = Enum::from(0_u8); assert_eq!(from, Enum::Whatever); let from_primitive = Enum::from_primitive(1_u8); assert_eq!(from_primitive, Enum::Whatever); let from = Enum::from(1_u8); assert_eq!(from, Enum::Whatever); } #[test] fn from_primitive_number_catch_all() { #[derive(Debug, Eq, PartialEq, FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(catch_all)] NonZero(u8), } let zero = Enum::from_primitive(0_u8); assert_eq!(zero, Enum::Zero); let one = Enum::from_primitive(1_u8); assert_eq!(one, Enum::NonZero(1_u8)); let two = Enum::from_primitive(2_u8); assert_eq!(two, Enum::NonZero(2_u8)); } #[test] fn from_primitive_number_catch_all_in_middle() { #[derive(Debug, PartialEq, Eq, FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(catch_all)] Else(u8) = 2, One = 1, } let zero = Enum::from_primitive(0_u8); assert_eq!(zero, Enum::Zero); let one = Enum::from_primitive(1_u8); assert_eq!(one, Enum::One); let two = Enum::from_primitive(2_u8); assert_eq!(two, Enum::Else(2_u8)); let three = Enum::from_primitive(3_u8); assert_eq!(three, Enum::Else(3_u8)); } #[cfg(feature = "complex-expressions")] #[test] fn from_primitive_number_with_inclusive_range() { #[derive(Debug, Eq, PartialEq, FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(alternatives = [2..=255])] NonZero, } let zero = Enum::from_primitive(0_u8); assert_eq!(zero, Enum::Zero); let one = Enum::from_primitive(1_u8); assert_eq!(one, Enum::NonZero); let two = Enum::from_primitive(2_u8); assert_eq!(two, Enum::NonZero); let three = Enum::from_primitive(3_u8); assert_eq!(three, Enum::NonZero); let twofivefive = Enum::from_primitive(255_u8); assert_eq!(twofivefive, Enum::NonZero); } num_enum-0.7.5/tests/into_primitive.rs000064400000000000000000000015151046102023000162330ustar 00000000000000use ::num_enum::IntoPrimitive; // Guard against https://github.com/illicitonion/num_enum/issues/27 mod alloc {} mod core {} mod num_enum {} mod std {} #[derive(IntoPrimitive)] #[repr(u8)] enum Enum { Zero, One, Two, } #[test] fn simple() { let zero: u8 = Enum::Zero.into(); assert_eq!(zero, 0u8); let one: u8 = Enum::One.into(); assert_eq!(one, 1u8); let two: u8 = Enum::Two.into(); assert_eq!(two, 2u8); } #[test] fn catch_all() { #[derive(Debug, Eq, PartialEq, IntoPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(catch_all)] NonZero(u8), } let zero: u8 = Enum::Zero.into(); assert_eq!(zero, 0u8); let one: u8 = Enum::NonZero(1u8).into(); assert_eq!(one, 1u8); let two: u8 = Enum::NonZero(2u8).into(); assert_eq!(two, 2u8); } num_enum-0.7.5/tests/renamed_num_enum.rs000064400000000000000000000015611046102023000165110ustar 00000000000000use std::process::Stdio; #[test] fn no_std() { assert!(::std::process::Command::new("cargo") .args([ "run", "--manifest-path", concat!( env!("CARGO_MANIFEST_DIR"), "/../renamed_num_enum/Cargo.toml", ), ]) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .status() .unwrap() .success()) } #[test] fn std() { assert!(::std::process::Command::new("cargo") .args([ "run", "--manifest-path", concat!( env!("CARGO_MANIFEST_DIR"), "/../renamed_num_enum/Cargo.toml", ), "--features", "std", ]) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .status() .unwrap() .success()) } num_enum-0.7.5/tests/try_build/compile_fail/alternative_clashes_with_its_discriminant.rs000064400000000000000000000002411046102023000303030ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [3,1,4])] One = 1, Two = 2, } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/alternative_clashes_with_its_discriminant.stderr000064400000000000000000000004121046102023000311620ustar 00000000000000error: '1' in the alternative values is already attributed as the discriminant of this variant --> tests/try_build/compile_fail/alternative_clashes_with_its_discriminant.rs:5:34 | 5 | #[num_enum(alternatives = [3,1,4])] | ^ num_enum-0.7.5/tests/try_build/compile_fail/alternative_clashes_with_variant.rs000064400000000000000000000002371046102023000264110ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [2])] One = 1, Two = 2, } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/alternative_clashes_with_variant.stderr000064400000000000000000000002761046102023000272730ustar 00000000000000error: The discriminant '2' collides with a value attributed to a previous variant --> tests/try_build/compile_fail/alternative_clashes_with_variant.rs:7:5 | 7 | Two = 2, | ^^^ num_enum-0.7.5/tests/try_build/compile_fail/alternative_clashes_with_variant_out_of_order.rs000064400000000000000000000002431046102023000311540ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [5,7,0,3])] One = 1, Two = 2, } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/alternative_clashes_with_variant_out_of_order.stderr000064400000000000000000000004041046102023000320320ustar 00000000000000error: '0' in the alternative values is already attributed to a previous variant --> tests/try_build/compile_fail/alternative_clashes_with_variant_out_of_order.rs:5:36 | 5 | #[num_enum(alternatives = [5,7,0,3])] | ^ num_enum-0.7.5/tests/try_build/compile_fail/alternative_exprs.rs000064400000000000000000000002631046102023000233500ustar 00000000000000const THREE: u8 = 3; #[derive(num_enum::TryFromPrimitive)] #[repr(i8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [-1, 2, THREE])] One = 1, } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/alternative_exprs.stderr000064400000000000000000000003411046102023000242240ustar 00000000000000error: Only literals are allowed as num_enum alternate values --> tests/try_build/compile_fail/alternative_exprs.rs:7:39 | 7 | #[num_enum(alternatives = [-1, 2, THREE])] | ^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/catch_all_multiple_fields.rs000064400000000000000000000002371046102023000247650ustar 00000000000000#[derive(Debug, Eq, PartialEq, num_enum::FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(catch_all)] NonZero(u8, u8), } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/catch_all_multiple_fields.stderr000064400000000000000000000003401046102023000256370ustar 00000000000000error: Variant with `catch_all` must be a tuple with exactly 1 field matching the repr type --> tests/try_build/compile_fail/catch_all_multiple_fields.rs:5:16 | 5 | #[num_enum(catch_all)] | ^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/catch_all_non_tuple.rs000064400000000000000000000002331046102023000236030ustar 00000000000000#[derive(Debug, Eq, PartialEq, num_enum::FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(catch_all)] NonZero = 1, } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/catch_all_non_tuple.stderr000064400000000000000000000003321046102023000244620ustar 00000000000000error: Variant with `catch_all` must be a tuple with exactly 1 field matching the repr type --> tests/try_build/compile_fail/catch_all_non_tuple.rs:5:16 | 5 | #[num_enum(catch_all)] | ^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/catch_all_type_mismatch.rs000064400000000000000000000002341046102023000244470ustar 00000000000000#[derive(Debug, Eq, PartialEq, num_enum::FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(catch_all)] NonZero(i32), } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/catch_all_type_mismatch.stderr000064400000000000000000000003361046102023000253310ustar 00000000000000error: Variant with `catch_all` must be a tuple with exactly 1 field matching the repr type --> tests/try_build/compile_fail/catch_all_type_mismatch.rs:5:16 | 5 | #[num_enum(catch_all)] | ^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/conflicting_default.rs000064400000000000000000000001521046102023000236110ustar 00000000000000#[derive(Default, num_enum::Default)] #[repr(u8)] enum Number { #[default] Zero, } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/conflicting_default.stderr000064400000000000000000000007141046102023000244740ustar 00000000000000error[E0119]: conflicting implementations of trait `Default` for type `Number` --> tests/try_build/compile_fail/conflicting_default.rs:1:19 | 1 | #[derive(Default, num_enum::Default)] | ------- ^^^^^^^^^^^^^^^^^ conflicting implementation for `Number` | | | first implementation here | = note: this error originates in the derive macro `num_enum::Default` (in Nightly builds, run with -Z macro-backtrace for more info) num_enum-0.7.5/tests/try_build/compile_fail/conflicting_derive.rs000064400000000000000000000002301046102023000234400ustar 00000000000000#[derive(num_enum::FromPrimitive, num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero, #[num_enum(default)] One, } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/conflicting_derive.stderr000064400000000000000000000021331046102023000243230ustar 00000000000000error[E0119]: conflicting implementations of trait `CannotDeriveBothFromPrimitiveAndTryFromPrimitive` for type `Numbers` --> tests/try_build/compile_fail/conflicting_derive.rs:1:35 | 1 | #[derive(num_enum::FromPrimitive, num_enum::TryFromPrimitive)] | ----------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Numbers` | | | first implementation here | = note: this error originates in the derive macro `num_enum::TryFromPrimitive` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0119]: conflicting implementations of trait `TryFrom` for type `Numbers` --> tests/try_build/compile_fail/conflicting_derive.rs:1:35 | 1 | #[derive(num_enum::FromPrimitive, num_enum::TryFromPrimitive)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: conflicting implementation in crate `core`: - impl TryFrom for T where U: Into; = note: this error originates in the derive macro `num_enum::TryFromPrimitive` (in Nightly builds, run with -Z macro-backtrace for more info) num_enum-0.7.5/tests/try_build/compile_fail/custom_error_type_parsing.rs000064400000000000000000000021311046102023000251140ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[num_enum(error_type(name = CustomError))] #[repr(u8)] enum MissingConstructor { Zero, One, Two, } #[derive(num_enum::TryFromPrimitive)] #[num_enum(error_type(constructor = CustomError::new))] #[repr(u8)] enum MissingName { Zero, One, Two, } #[derive(num_enum::TryFromPrimitive)] #[num_enum(error_type(name = CustomError, constructor = CustomError::new, extra = something))] #[repr(u8)] enum ExtraAttr { Zero, One, Two, } #[derive(num_enum::TryFromPrimitive)] #[num_enum(error_type(name = CustomError, constructor = CustomError::new), error_type(name = CustomError, constructor = CustomError::new))] #[repr(u8)] enum TwoErrorTypes { Zero, One, Two, } #[derive(num_enum::TryFromPrimitive)] #[num_enum(error_type(name = CustomError, constructor = CustomError::new))] #[num_enum(error_type(name = CustomError, constructor = CustomError::new))] #[repr(u8)] enum TwoAttrs { Zero, One, Two, } struct CustomError {} impl CustomError { fn new(_: u8) -> CustomError { CustomError{} } } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/custom_error_type_parsing.stderr000064400000000000000000000025061046102023000260010ustar 00000000000000error: num_enum error_type attribute requires `constructor` value --> tests/try_build/compile_fail/custom_error_type_parsing.rs:2:12 | 2 | #[num_enum(error_type(name = CustomError))] | ^^^^^^^^^^ error: num_enum error_type attribute requires `name` value --> tests/try_build/compile_fail/custom_error_type_parsing.rs:11:12 | 11 | #[num_enum(error_type(constructor = CustomError::new))] | ^^^^^^^^^^ error: expected `name` or `constructor` --> tests/try_build/compile_fail/custom_error_type_parsing.rs:20:75 | 20 | #[num_enum(error_type(name = CustomError, constructor = CustomError::new, extra = something))] | ^^^^^ error: num_enum attribute must have at most one error_type --> tests/try_build/compile_fail/custom_error_type_parsing.rs:29:76 | 29 | #[num_enum(error_type(name = CustomError, constructor = CustomError::new), error_type(name = CustomError, constructor = CustomError::new))] | ^^^^^^^^^^ error: num_enum attribute must have at most one error_type --> tests/try_build/compile_fail/custom_error_type_parsing.rs:39:12 | 39 | #[num_enum(error_type(name = CustomError, constructor = CustomError::new))] | ^^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/default_and_catch_all.rs000064400000000000000000000002521046102023000240470ustar 00000000000000#[derive(Debug, Eq, PartialEq, num_enum::FromPrimitive)] #[repr(u8)] enum Enum { #[default] Zero = 0, #[num_enum(catch_all)] NonZero(u8), } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/default_and_catch_all.stderr000064400000000000000000000003021046102023000247220ustar 00000000000000error: Attribute `catch_all` is mutually exclusive with `default` --> tests/try_build/compile_fail/default_and_catch_all.rs:6:16 | 6 | #[num_enum(catch_all)] | ^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/default_and_catch_all_alt.rs000064400000000000000000000002641046102023000247120ustar 00000000000000#[derive(Debug, Eq, PartialEq, num_enum::FromPrimitive)] #[repr(u8)] enum Enum { #[num_enum(default)] Zero = 0, #[num_enum(catch_all)] NonZero(u8), } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/default_and_catch_all_alt.stderr000064400000000000000000000003061046102023000255660ustar 00000000000000error: Attribute `catch_all` is mutually exclusive with `default` --> tests/try_build/compile_fail/default_and_catch_all_alt.rs:6:16 | 6 | #[num_enum(catch_all)] | ^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/default_and_catch_all_same_variant.rs000064400000000000000000000002521046102023000266000ustar 00000000000000#[derive(Debug, Eq, PartialEq, num_enum::FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(catch_all)] #[default] NonZero(u8), } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/default_and_catch_all_same_variant.stderr000064400000000000000000000002701046102023000274570ustar 00000000000000error: Attribute `default` is mutually exclusive with `catch_all` --> tests/try_build/compile_fail/default_and_catch_all_same_variant.rs:6:5 | 6 | #[default] | ^^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/default_and_catch_all_same_variant_alt.rs000064400000000000000000000002641046102023000274430ustar 00000000000000#[derive(Debug, Eq, PartialEq, num_enum::FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(catch_all)] #[num_enum(default)] NonZero(u8), } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/default_and_catch_all_same_variant_alt.stderr000064400000000000000000000003171046102023000303210ustar 00000000000000error: Attribute `default` is mutually exclusive with `catch_all` --> tests/try_build/compile_fail/default_and_catch_all_same_variant_alt.rs:6:16 | 6 | #[num_enum(default)] | ^^^^^^^ ././@LongLink00006440000000000000000000000150000000000000007767Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/!complex-expressions/alternate_exprs_with_range.rsnum_enum-0.7.5/tests/try_build/compile_fail/features/!complex-expressions/alternate_exprs_with_range000064400000000000000000000002311046102023000324760ustar 00000000000000#[derive(num_enum::FromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [2..=255])] NonZero = 1, } fn main() { } ././@LongLink00006440000000000000000000000154000000000000007773Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/!complex-expressions/alternate_exprs_with_range.stderrnum_enum-0.7.5/tests/try_build/compile_fail/features/!complex-expressions/alternate_exprs_with_range000064400000000000000000000005101046102023000324760ustar 00000000000000error: Ranges are only supported as num_enum alternate values if the `complex-expressions` feature of the crate `num_enum` is enabled --> tests/try_build/compile_fail/features/!complex-expressions/alternate_exprs_with_range.rs:5:5 | 5 | #[num_enum(alternatives = [2..=255])] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ././@LongLink00006440000000000000000000000166000000000000007776Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missing_lower_bound.rsnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missi000064400000000000000000000002321046102023000326070ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [..255])] NonZero = 1, } fn main() { } ././@LongLink00006440000000000000000000000172000000000000007773Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missing_lower_bound.stderrnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missi000064400000000000000000000004661046102023000326200ustar 00000000000000error: When ranges are used for alternate values, both bounds most be explicitly specified numeric literals --> tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missing_lower_bound.rs:5:32 | 5 | #[num_enum(alternatives = [..255])] | ^^^^^ ././@LongLink00006440000000000000000000000166000000000000007776Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missing_upper_bound.rsnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missi000064400000000000000000000002301046102023000326050ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [2..])] NonZero = 1, } fn main() { } ././@LongLink00006440000000000000000000000172000000000000007773Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missing_upper_bound.stderrnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missi000064400000000000000000000004621046102023000326140ustar 00000000000000error: When ranges are used for alternate values, both bounds most be explicitly specified numeric literals --> tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_missing_upper_bound.rs:5:32 | 5 | #[num_enum(alternatives = [2..])] | ^^^ ././@LongLink00006440000000000000000000000165000000000000007775Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonlit_lower_bound.rsnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonli000064400000000000000000000002621046102023000326050ustar 00000000000000const TWO: u8 = 2; #[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [TWO..=255])] NonZero = 1, } fn main() { } ././@LongLink00006440000000000000000000000171000000000000007772Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonlit_lower_bound.stderrnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonli000064400000000000000000000004751046102023000326130ustar 00000000000000error: When ranges are used for alternate values, both bounds most be explicitly specified numeric literals --> tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonlit_lower_bound.rs:7:32 | 7 | #[num_enum(alternatives = [TWO..=255])] | ^^^^^^^^^ ././@LongLink00006440000000000000000000000165000000000000007775Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonlit_upper_bound.rsnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonli000064400000000000000000000003021046102023000326000ustar 00000000000000const TWOFIVEFIVE: u8 = 255; #[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [2..=TWOFIVEFIVE])] NonZero = 1, } fn main() { } ././@LongLink00006440000000000000000000000171000000000000007772Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonlit_upper_bound.stderrnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonli000064400000000000000000000005111046102023000326020ustar 00000000000000error: When ranges are used for alternate values, both bounds most be explicitly specified numeric literals --> tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_nonlit_upper_bound.rs:7:32 | 7 | #[num_enum(alternatives = [2..=TWOFIVEFIVE])] | ^^^^^^^^^^^^^^^ ././@LongLink00006440000000000000000000000161000000000000007771Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_swapped_bounds.rsnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_swapp000064400000000000000000000002341046102023000326170ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [255..=2])] NonZero = 1, } fn main() { } ././@LongLink00006440000000000000000000000165000000000000007775Lustar num_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_swapped_bounds.stderrnum_enum-0.7.5/tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_swapp000064400000000000000000000004461046102023000326240ustar 00000000000000error: When using ranges for alternate values, upper bound must not be less than lower bound --> tests/try_build/compile_fail/features/complex-expressions/alternate_exprs_range_swapped_bounds.rs:5:32 | 5 | #[num_enum(alternatives = [255..=2])] | ^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/from_unchecked_deprecated_warning.rs000064400000000000000000000004651046102023000264760ustar 00000000000000#![deny(deprecated)] use num_enum::UnsafeFromPrimitive; #[derive(Debug, Eq, PartialEq, UnsafeFromPrimitive)] #[repr(u8)] enum Enum { Zero, One, } fn main() { unsafe { assert_eq!(Enum::from_unchecked(0_u8), Enum::Zero); assert_eq!(Enum::from_unchecked(1_u8), Enum::One); } } num_enum-0.7.5/tests/try_build/compile_fail/from_unchecked_deprecated_warning.stderr000064400000000000000000000016531046102023000273550ustar 00000000000000error: use of deprecated associated function `num_enum::UnsafeFromPrimitive::from_unchecked`: Prefer to use `unchecked_transmute_from`, `from_unchecked` will be removed in a future release. --> tests/try_build/compile_fail/from_unchecked_deprecated_warning.rs:14:26 | 14 | assert_eq!(Enum::from_unchecked(0_u8), Enum::Zero); | ^^^^^^^^^^^^^^ | note: the lint level is defined here --> tests/try_build/compile_fail/from_unchecked_deprecated_warning.rs:1:9 | 1 | #![deny(deprecated)] | ^^^^^^^^^^ error: use of deprecated associated function `num_enum::UnsafeFromPrimitive::from_unchecked`: Prefer to use `unchecked_transmute_from`, `from_unchecked` will be removed in a future release. --> tests/try_build/compile_fail/from_unchecked_deprecated_warning.rs:15:26 | 15 | assert_eq!(Enum::from_unchecked(1_u8), Enum::One); | ^^^^^^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/garbage_attribute.rs000064400000000000000000000002241046102023000232610ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(garbage)] One = 1, Two = 2, } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/garbage_attribute.stderr000064400000000000000000000002631046102023000241430ustar 00000000000000error: Invalid attribute: expected one of: `default`, `catch_all`, `alternatives` --> $DIR/garbage_attribute.rs:5:5 | 5 | #[num_enum(garbage)] | ^^^^^^^^^^^^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/missing_default.rs000064400000000000000000000001541046102023000227650ustar 00000000000000#[derive(num_enum::FromPrimitive)] #[repr(u8)] enum Numbers { Zero, One, Two, } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/missing_default.stderr000064400000000000000000000006471046102023000236530ustar 00000000000000error: #[derive(num_enum::FromPrimitive)] requires enum to be exhaustive, or a variant marked with `#[default]`, `#[num_enum(default)]`, or `#[num_enum(catch_all)` --> $DIR/missing_default.rs:1:10 | 1 | #[derive(num_enum::FromPrimitive)] | ^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in the derive macro `num_enum::FromPrimitive` (in Nightly builds, run with -Z macro-backtrace for more info) num_enum-0.7.5/tests/try_build/compile_fail/missing_repr.rs000064400000000000000000000001251046102023000223070ustar 00000000000000#[derive(num_enum::IntoPrimitive)] enum Numbers { Zero, One, } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/missing_repr.stderr000064400000000000000000000004561046102023000231750ustar 00000000000000error: Missing `#[repr({Integer})]` attribute --> $DIR/missing_repr.rs:1:10 | 1 | #[derive(num_enum::IntoPrimitive)] | ^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in the derive macro `num_enum::IntoPrimitive` (in Nightly builds, run with -Z macro-backtrace for more info) num_enum-0.7.5/tests/try_build/compile_fail/multiple_catch_all.rs000064400000000000000000000002661046102023000234410ustar 00000000000000#[derive(Debug, Eq, PartialEq, num_enum::FromPrimitive)] #[repr(u8)] enum Enum { #[num_enum(catch_all)] Zero(u8), #[num_enum(catch_all)] NonZero(u8), } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/multiple_catch_all.stderr000064400000000000000000000002731046102023000243160ustar 00000000000000error: Multiple variants marked with `#[num_enum(catch_all)]` --> tests/try_build/compile_fail/multiple_catch_all.rs:6:16 | 6 | #[num_enum(catch_all)] | ^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/multiple_catch_all_same_variant.rs000064400000000000000000000002661046102023000261720ustar 00000000000000#[derive(Debug, Eq, PartialEq, num_enum::FromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, #[num_enum(catch_all)] #[num_enum(catch_all)] NonZero(u8), } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/multiple_catch_all_same_variant.stderr000064400000000000000000000003101046102023000270370ustar 00000000000000error: Multiple variants marked with `#[num_enum(catch_all)]` --> tests/try_build/compile_fail/multiple_catch_all_same_variant.rs:6:16 | 6 | #[num_enum(catch_all)] | ^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/multiple_defaults.rs000064400000000000000000000002461046102023000233340ustar 00000000000000#[derive(num_enum::FromPrimitive, num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero, #[default] One, #[default] Two, } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/multiple_defaults.stderr000064400000000000000000000002321046102023000242060ustar 00000000000000error: Multiple variants marked `#[default]` or `#[num_enum(default)]` found --> $DIR/multiple_defaults.rs:7:5 | 7 | #[default] | ^^^^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/multiple_defaults_different_kinds.rs000064400000000000000000000002601046102023000265460ustar 00000000000000#[derive(num_enum::FromPrimitive, num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero, #[default] One, #[num_enum(default)] Two, } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/multiple_defaults_different_kinds.stderr000064400000000000000000000002751046102023000274330ustar 00000000000000error: Multiple variants marked `#[default]` or `#[num_enum(default)]` found --> $DIR/multiple_defaults_different_kinds.rs:7:16 | 7 | #[num_enum(default)] | ^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/multiple_num_enum_defaults.rs000064400000000000000000000002721046102023000252360ustar 00000000000000#[derive(num_enum::FromPrimitive, num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero, #[num_enum(default)] One, #[num_enum(default)] Two, } fn main() { } num_enum-0.7.5/tests/try_build/compile_fail/multiple_num_enum_defaults.stderr000064400000000000000000000002661046102023000261200ustar 00000000000000error: Multiple variants marked `#[default]` or `#[num_enum(default)]` found --> $DIR/multiple_num_enum_defaults.rs:7:16 | 7 | #[num_enum(default)] | ^^^^^^^ num_enum-0.7.5/tests/try_build/compile_fail/repr_c.rs000064400000000000000000000001401046102023000210550ustar 00000000000000#[derive(num_enum::IntoPrimitive)] #[repr(C)] enum Numbers { Zero, One, } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/repr_c.stderr000064400000000000000000000001501046102023000217350ustar 00000000000000error: repr(C) doesn't have a well defined size --> $DIR/repr_c.rs:2:8 | 2 | #[repr(C)] | ^ num_enum-0.7.5/tests/try_build/compile_fail/variants_with_fields.rs000064400000000000000000000005741046102023000240260ustar 00000000000000use num_enum::{FromPrimitive, IntoPrimitive, TryFromPrimitive}; #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Number { Zero, NonZero(u8), } #[derive(Debug, Eq, PartialEq, FromPrimitive)] #[repr(u8)] enum Colour { Red { intensity: u8 }, } #[derive(Debug, Eq, PartialEq, IntoPrimitive)] #[repr(u8)] enum Meaningless { Beep(), } fn main() {} num_enum-0.7.5/tests/try_build/compile_fail/variants_with_fields.stderr000064400000000000000000000011701046102023000246760ustar 00000000000000error: `::num_enum` only supports unit variants (with no associated data), but `Number::NonZero` was not a unit variant. --> $DIR/variants_with_fields.rs:7:5 | 7 | NonZero(u8), | ^^^^^^^^^^^ error: `::num_enum` only supports unit variants (with no associated data), but `Colour::Red` was not a unit variant. --> $DIR/variants_with_fields.rs:13:5 | 13 | Red { intensity: u8 }, | ^^^^^^^^^^^^^^^^^^^^^ error: `::num_enum` only supports unit variants (with no associated data), but `Meaningless::Beep` was not a unit variant. --> $DIR/variants_with_fields.rs:19:5 | 19 | Beep(), | ^^^^^^ num_enum-0.7.5/tests/try_build/pass/default_and_alternatives.rs000064400000000000000000000026161046102023000231670ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Default { #[num_enum(default)] Foo = 0, Bar = 1, } #[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Alternatives { #[num_enum(alternatives = [])] Foo = 0, #[num_enum(alternatives = [3])] Bar = 1, #[num_enum(alternatives = [4, 5])] Baz = 2, #[num_enum(alternatives = [7])] #[num_enum(alternatives = [8])] Blee = 6, } #[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Both { #[num_enum(default)] Foo = 0, #[num_enum(alternatives = [3])] Bar = 1, } mod mixed { #[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum AlternativesFollowedByDefaultInSingleAttribute { #[num_enum(alternatives = [1, 2], default)] Foo = 0, } #[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum DefaultFollowedByAlternativesInSingleAttribute { #[num_enum(default, alternatives = [1, 2])] Foo = 0, } #[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum AlternativesFollowedByDefaultInMultipleAttributes { #[num_enum(alternatives = [1, 2])] #[num_enum(default)] Foo = 0, } #[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum DefaultFollowedByAlternativesInMultipleAttributes { #[num_enum(default)] #[num_enum(alternatives = [1, 2])] Foo = 0, } } fn main() { } num_enum-0.7.5/tests/try_build/pass/exhaustive_enum_try_from.rs000064400000000000000000000035771046102023000233010ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum ExhaustiveTryFrom { #[num_enum(alternatives = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])] A = 0, #[num_enum(alternatives = [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])] B = 16, #[num_enum(alternatives = [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47])] C = 32, #[num_enum(alternatives = [49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63])] D = 48, #[num_enum(alternatives = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79])] E = 64, #[num_enum(alternatives = [81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95])] F = 80, #[num_enum(alternatives = [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111])] G = 96, #[num_enum(alternatives = [113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127])] H = 112, #[num_enum(alternatives = [129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143])] I = 128, #[num_enum(alternatives = [145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159])] J = 144, #[num_enum(alternatives = [161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175])] K = 160, #[num_enum(alternatives = [177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191])] L = 176, #[num_enum(alternatives = [193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207])] M = 192, #[num_enum(alternatives = [209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223])] N = 208, #[num_enum(alternatives = [225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239])] O = 224, #[num_enum(alternatives = [241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255])] P = 240, } fn main() { } num_enum-0.7.5/tests/try_build/pass/exhaustive_enum_via_alternatives.rs000064400000000000000000000035711046102023000247720ustar 00000000000000#[derive(num_enum::FromPrimitive)] #[repr(u8)] enum ExhaustiveFrom { #[num_enum(alternatives = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])] A = 0, #[num_enum(alternatives = [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])] B = 16, #[num_enum(alternatives = [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47])] C = 32, #[num_enum(alternatives = [49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63])] D = 48, #[num_enum(alternatives = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79])] E = 64, #[num_enum(alternatives = [81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95])] F = 80, #[num_enum(alternatives = [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111])] G = 96, #[num_enum(alternatives = [113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127])] H = 112, #[num_enum(alternatives = [129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143])] I = 128, #[num_enum(alternatives = [145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159])] J = 144, #[num_enum(alternatives = [161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175])] K = 160, #[num_enum(alternatives = [177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191])] L = 176, #[num_enum(alternatives = [193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207])] M = 192, #[num_enum(alternatives = [209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223])] N = 208, #[num_enum(alternatives = [225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239])] O = 224, #[num_enum(alternatives = [241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255])] P = 240, } fn main() { } num_enum-0.7.5/tests/try_build/pass/exhaustive_enum_via_default.rs000064400000000000000000000034311046102023000237100ustar 00000000000000#[derive(num_enum::FromPrimitive)] #[repr(u8)] enum ExhaustiveFrom { #[num_enum(default)] #[num_enum(alternatives = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])] A = 0, #[num_enum(alternatives = [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])] B = 16, #[num_enum(alternatives = [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47])] C = 32, #[num_enum(alternatives = [49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63])] D = 48, #[num_enum(alternatives = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79])] E = 64, #[num_enum(alternatives = [81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95])] F = 80, #[num_enum(alternatives = [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111])] G = 96, #[num_enum(alternatives = [113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127])] H = 112, #[num_enum(alternatives = [129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143])] I = 128, #[num_enum(alternatives = [145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159])] J = 144, #[num_enum(alternatives = [161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175])] K = 160, #[num_enum(alternatives = [177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191])] L = 176, #[num_enum(alternatives = [193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207])] M = 192, #[num_enum(alternatives = [209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223])] N = 208, #[num_enum(alternatives = [225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239])] O = 224, } fn main() { } ././@LongLink00006440000000000000000000000152000000000000007771Lustar num_enum-0.7.5/tests/try_build/pass/features/complex-expressions/alternate_exprs_exhaustive_with_range.rsnum_enum-0.7.5/tests/try_build/pass/features/complex-expressions/alternate_exprs_exhaustive_with_ran000064400000000000000000000002311046102023000327110ustar 00000000000000#[derive(num_enum::FromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [2..=255])] NonZero = 1, } fn main() { } ././@LongLink00006440000000000000000000000156000000000000007775Lustar num_enum-0.7.5/tests/try_build/pass/features/complex-expressions/alternate_exprs_non_exhaustive_with_range.rsnum_enum-0.7.5/tests/try_build/pass/features/complex-expressions/alternate_exprs_non_exhaustive_with000064400000000000000000000002331046102023000327250ustar 00000000000000#[derive(num_enum::TryFromPrimitive)] #[repr(u8)] enum Numbers { Zero = 0, #[num_enum(alternatives = [2..255])] NonZero = 1, } fn main() { } num_enum-0.7.5/tests/try_build.rs000064400000000000000000000077101046102023000151720ustar 00000000000000use std::error::Error; use std::path::{Path, PathBuf}; use walkdir::WalkDir; #[test] fn trybuild() { let directory = PathBuf::from("tests/try_build"); let mut _renamer = None; let compile_fail_dir = directory.join("compile_fail"); // Sometimes error messages change on beta/nightly - allow alternate errors on those. _renamer = Some(Renamer::rename(compile_fail_dir.clone()).unwrap()); let fail = trybuild::TestCases::new(); fail.compile_fail(compile_fail_dir.join("*.rs")); add_feature_dirs(&compile_fail_dir, &fail, ExpectedResult::Fail); let pass = trybuild::TestCases::new(); let pass_dir = directory.join("pass"); pass.pass(pass_dir.join("*.rs")); add_feature_dirs(&pass_dir, &pass, ExpectedResult::Pass); } enum ExpectedResult { Pass, Fail, } fn add_feature_dirs( parent_dir: &Path, test_cases: &trybuild::TestCases, expected_result: ExpectedResult, ) { let features_dir = parent_dir.join("features"); let feature_specific_dir = if cfg!(feature = "complex-expressions") { features_dir.join("complex-expressions") } else { features_dir.join("!complex-expressions") }; let tests = feature_specific_dir.join("*.rs"); match expected_result { ExpectedResult::Pass => test_cases.pass(tests), ExpectedResult::Fail => test_cases.compile_fail(tests), } } struct Renamer(Vec); impl Renamer { const STDERR_EXTENSION: &'static str = "stderr"; #[rustversion::all(beta)] const VERSION_SPECIFIC_EXTENSION: &'static str = "stderr_beta"; #[rustversion::all(nightly)] const VERSION_SPECIFIC_EXTENSION: &'static str = "stderr_nightly"; #[rustversion::all(not(beta), not(nightly))] const VERSION_SPECIFIC_EXTENSION: &'static str = "stderr_doesnotexist"; const NON_VERSION_SPECIFIC_BACKUP_EXTENSION: &'static str = "stderr_non_version_specific_backup"; fn rename(dir: PathBuf) -> anyhow::Result { let nightly_paths = WalkDir::new(dir) .max_depth(1) .into_iter() .filter_map(|dir_entry| { let dir_entry = match dir_entry { Ok(dir_entry) => dir_entry, Err(err) => return Some(Err(err)), }; let path = dir_entry.path(); if let Some(file_name) = path.file_name() { if Path::new(file_name).extension() == Some(Renamer::VERSION_SPECIFIC_EXTENSION.as_ref()) { return Some(Ok(path.to_path_buf())); } } None }) .collect::, _>>()?; // Create early so that if we end up returning an error this gets dropped and undoes any // already-done renames. let renamer = Renamer(nightly_paths); for nightly_path in &renamer.0 { std::fs::rename( nightly_path.with_extension(Renamer::STDERR_EXTENSION), nightly_path.with_extension(Renamer::NON_VERSION_SPECIFIC_BACKUP_EXTENSION), )?; std::fs::rename( nightly_path.with_extension(Renamer::VERSION_SPECIFIC_EXTENSION), nightly_path.with_extension(Renamer::STDERR_EXTENSION), )?; } Ok(renamer) } } impl Drop for Renamer { fn drop(&mut self) { for path in &self.0 { ignore_error(std::fs::rename( path.with_extension(Renamer::STDERR_EXTENSION), path.with_extension(Renamer::VERSION_SPECIFIC_EXTENSION), )); ignore_error(std::fs::rename( path.with_extension(Renamer::NON_VERSION_SPECIFIC_BACKUP_EXTENSION), path.with_extension(Renamer::STDERR_EXTENSION), )); } } } fn ignore_error(result: Result) { if let Err(err) = result { eprintln!("Ignoring error: {}", err); } } num_enum-0.7.5/tests/try_from_primitive.rs000064400000000000000000000335231046102023000171270ustar 00000000000000use ::std::convert::{TryFrom, TryInto}; use ::num_enum::TryFromPrimitive; // Guard against https://github.com/illicitonion/num_enum/issues/27 mod alloc {} mod core {} mod num_enum {} mod std {} #[test] fn simple() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { Zero, One, Two, } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let three: Result = 3u8.try_into(); assert_eq!( three.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `3`" ); } #[test] fn even() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, Two = 2, Four = 4, } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!( one.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `1`" ); let two: Result = 2u8.try_into(); assert_eq!(two, Ok(Enum::Two)); let three: Result = 3u8.try_into(); assert_eq!( three.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `3`" ); let four: Result = 4u8.try_into(); assert_eq!(four, Ok(Enum::Four)); } #[test] fn skipped_value() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { Zero, One, Three = 3, Four, } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2u8.try_into(); assert_eq!( two.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `2`" ); let three: Result = 3u8.try_into(); assert_eq!(three, Ok(Enum::Three)); let four: Result = 4u8.try_into(); assert_eq!(four, Ok(Enum::Four)); } #[test] fn wrong_order() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { Four = 4, Three = 3, Zero = 0, One, // Zero + 1 } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2u8.try_into(); assert_eq!( two.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `2`" ); let three: Result = 3u8.try_into(); assert_eq!(three, Ok(Enum::Three)); let four: Result = 4u8.try_into(); assert_eq!(four, Ok(Enum::Four)); } #[test] fn negative_values() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(i8)] enum Enum { MinusTwo = -2, MinusOne = -1, Zero = 0, One = 1, Two = 2, } let minus_two: Result = (-2i8).try_into(); assert_eq!(minus_two, Ok(Enum::MinusTwo)); let minus_one: Result = (-1i8).try_into(); assert_eq!(minus_one, Ok(Enum::MinusOne)); let zero: Result = 0i8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1i8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2i8.try_into(); assert_eq!(two, Ok(Enum::Two)); } #[test] fn discriminant_expressions() { const ONE: u8 = 1; #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { Zero, One = ONE, Two, Four = 4u8, Five, Six = ONE + ONE + 2u8 + 2, } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2u8.try_into(); assert_eq!(two, Ok(Enum::Two)); let three: Result = 3u8.try_into(); assert_eq!( three.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `3`", ); let four: Result = 4u8.try_into(); assert_eq!(four, Ok(Enum::Four)); let five: Result = 5u8.try_into(); assert_eq!(five, Ok(Enum::Five)); let six: Result = 6u8.try_into(); assert_eq!(six, Ok(Enum::Six)); } #[cfg(feature = "complex-expressions")] mod complex { use num_enum::TryFromPrimitive; use std::convert::TryInto; const ONE: u8 = 1; #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { Zero, One = ONE, Two, Four = 4u8, Five, Six = ONE + ONE + 2u8 + 2, Seven = (7, 2).0, } #[test] fn different_values() { let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2u8.try_into(); assert_eq!(two, Ok(Enum::Two)); let three: Result = 3u8.try_into(); assert_eq!( three.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `3`", ); let four: Result = 4u8.try_into(); assert_eq!(four, Ok(Enum::Four)); let five: Result = 5u8.try_into(); assert_eq!(five, Ok(Enum::Five)); let six: Result = 6u8.try_into(); assert_eq!(six, Ok(Enum::Six)); let seven: Result = 7u8.try_into(); assert_eq!(seven, Ok(Enum::Seven)); } #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum EnumWithExclusiveRange { Zero = 0, #[num_enum(alternatives = [2..4])] OneOrTwoOrThree, } #[test] fn different_values_with_exclusive_range() { let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(EnumWithExclusiveRange::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(EnumWithExclusiveRange::OneOrTwoOrThree)); let two: Result = 2u8.try_into(); assert_eq!(two, Ok(EnumWithExclusiveRange::OneOrTwoOrThree)); let three: Result = 3u8.try_into(); assert_eq!(three, Ok(EnumWithExclusiveRange::OneOrTwoOrThree)); let four: Result = 4u8.try_into(); assert_eq!( four.unwrap_err().to_string(), "No discriminant in enum `EnumWithExclusiveRange` matches the value `4`", ); } #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum EnumWithInclusiveRange { Zero = 0, #[num_enum(alternatives = [2..=3])] OneOrTwoOrThree, } #[test] fn different_values_with_inclusive_range() { let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(EnumWithInclusiveRange::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(EnumWithInclusiveRange::OneOrTwoOrThree)); let two: Result = 2u8.try_into(); assert_eq!(two, Ok(EnumWithInclusiveRange::OneOrTwoOrThree)); let three: Result = 3u8.try_into(); assert_eq!(three, Ok(EnumWithInclusiveRange::OneOrTwoOrThree)); let four: Result = 4u8.try_into(); assert_eq!( four.unwrap_err().to_string(), "No discriminant in enum `EnumWithInclusiveRange` matches the value `4`", ); } } #[test] fn missing_trailing_comma() { #[rustfmt::skip] #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { Zero, One } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2u8.try_into(); assert_eq!( two.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `2`" ); } #[test] fn ignores_extra_attributes() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[allow(unused)] #[repr(u8)] enum Enum { Zero, #[allow(unused)] One, } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2u8.try_into(); assert_eq!( two.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `2`" ); } #[test] fn visibility_is_fine() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] pub(crate) enum Enum { Zero, One, } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2u8.try_into(); assert_eq!( two.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `2`" ); } #[test] fn error_variant_is_allowed() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] pub enum Enum { Ok, Error, } let ok: Result = 0u8.try_into(); assert_eq!(ok, Ok(Enum::Ok)); let err: Result = 1u8.try_into(); assert_eq!(err, Ok(Enum::Error)); let unknown: Result = 2u8.try_into(); assert_eq!( unknown.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `2`" ); } #[test] fn alternative_values() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(i8)] enum Enum { Zero = 0, #[num_enum(alternatives = [-1, 2, 3])] OneTwoThreeOrMinusOne = 1, } let minus_one: Result = (-1i8).try_into(); assert_eq!(minus_one, Ok(Enum::OneTwoThreeOrMinusOne)); let zero: Result = 0i8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1i8.try_into(); assert_eq!(one, Ok(Enum::OneTwoThreeOrMinusOne)); let two: Result = 2i8.try_into(); assert_eq!(two, Ok(Enum::OneTwoThreeOrMinusOne)); let three: Result = 3i8.try_into(); assert_eq!(three, Ok(Enum::OneTwoThreeOrMinusOne)); let four: Result = 4i8.try_into(); assert_eq!( four.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `4`" ); } #[test] fn default_value() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { Zero = 0, One = 1, #[num_enum(default)] Other = 2, } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2u8.try_into(); assert_eq!(two, Ok(Enum::Other)); let max_value: Result = u8::max_value().try_into(); assert_eq!( max_value.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `255`" ); } #[test] fn alternative_values_and_default_value() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { #[num_enum(default)] Zero = 0, One = 1, #[num_enum(alternatives = [3])] TwoOrThree = 2, Four = 4, } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(Enum::Zero)); let one: Result = 1u8.try_into(); assert_eq!(one, Ok(Enum::One)); let two: Result = 2u8.try_into(); assert_eq!(two, Ok(Enum::TwoOrThree)); let three: Result = 3u8.try_into(); assert_eq!(three, Ok(Enum::TwoOrThree)); let four: Result = 4u8.try_into(); assert_eq!(four, Ok(Enum::Four)); let five: Result = 5u8.try_into(); assert_eq!( five.unwrap_err().to_string(), "No discriminant in enum `Enum` matches the value `5`" ); } #[test] fn try_from_primitive_number() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[repr(u8)] enum Enum { #[num_enum(default)] Whatever = 0, } // #[derive(FromPrimitive)] generates implementations for the following traits: // // - `TryFromPrimitive` // - `TryFrom` let try_from_primitive = Enum::try_from_primitive(0_u8); assert_eq!(try_from_primitive, Ok(Enum::Whatever)); let try_from = Enum::try_from(0_u8); assert_eq!(try_from, Ok(Enum::Whatever)); } #[test] fn custom_error() { #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[num_enum(error_type(name = CustomError, constructor = CustomError::new))] #[repr(u8)] enum FirstNumber { Zero, One, Two, } #[derive(Debug, Eq, PartialEq, TryFromPrimitive)] #[num_enum(error_type(constructor = CustomError::new, name = CustomError))] #[repr(u8)] enum SecondNumber { Zero, One, Two, } #[derive(Debug, PartialEq, Eq)] struct CustomError { bad_value: u8, } impl CustomError { fn new(value: u8) -> CustomError { CustomError { bad_value: value } } } let zero: Result = 0u8.try_into(); assert_eq!(zero, Ok(FirstNumber::Zero)); let three: Result = 3u8.try_into(); assert_eq!(three.unwrap_err(), CustomError { bad_value: 3u8 }); let three: Result = 3u8.try_into(); assert_eq!(three.unwrap_err(), CustomError { bad_value: 3u8 }); } // #[derive(FromPrimitive)] generates implementations for the following traits: // // - `FromPrimitive` // - `From` // - `TryFromPrimitive` // - `TryFrom` num_enum-0.7.5/tests/unsafe_from_primitive.rs000064400000000000000000000046011046102023000175650ustar 00000000000000use ::num_enum::UnsafeFromPrimitive; // Guard against https://github.com/illicitonion/num_enum/issues/27 mod alloc {} mod core {} mod num_enum {} mod std {} #[test] fn has_unsafe_from_primitive_number() { #[derive(Debug, Eq, PartialEq, UnsafeFromPrimitive)] #[repr(u8)] enum Enum { Zero, One, } unsafe { assert_eq!(Enum::unchecked_transmute_from(0_u8), Enum::Zero); assert_eq!(Enum::unchecked_transmute_from(1_u8), Enum::One); } } #[test] fn has_unsafe_from_primitive_number_with_alternatives_and_default_which_are_ignored() { #[derive(Debug, Eq, PartialEq, UnsafeFromPrimitive)] #[repr(u8)] enum Enum { Zero, One, #[num_enum(alternatives = [3, 4])] Some, #[num_enum(default)] Many = 5, } unsafe { assert_eq!(Enum::unchecked_transmute_from(0_u8), Enum::Zero); assert_eq!(Enum::unchecked_transmute_from(1_u8), Enum::One); assert_eq!(Enum::unchecked_transmute_from(2_u8), Enum::Some); assert_eq!(Enum::unchecked_transmute_from(5_u8), Enum::Many); // Any other conversions would be undefined behavior. } #[allow(deprecated)] unsafe { assert_eq!(Enum::from_unchecked(0_u8), Enum::Zero); assert_eq!(Enum::from_unchecked(1_u8), Enum::One); assert_eq!(Enum::from_unchecked(2_u8), Enum::Some); assert_eq!(Enum::from_unchecked(5_u8), Enum::Many); } } #[test] fn has_unsafe_from_primitive_number_with_alternatives_and_std_default_which_are_ignored() { #[derive(Debug, Default, Eq, PartialEq, UnsafeFromPrimitive)] #[repr(u8)] enum Enum { Zero, One, #[num_enum(alternatives = [3, 4])] Some, #[default] Many = 5, } unsafe { assert_eq!(Enum::unchecked_transmute_from(0_u8), Enum::Zero); assert_eq!(Enum::unchecked_transmute_from(1_u8), Enum::One); assert_eq!(Enum::unchecked_transmute_from(2_u8), Enum::Some); assert_eq!(Enum::unchecked_transmute_from(5_u8), Enum::Many); // Any other conversions would be undefined behavior. } #[allow(deprecated)] unsafe { assert_eq!(Enum::from_unchecked(0_u8), Enum::Zero); assert_eq!(Enum::from_unchecked(1_u8), Enum::One); assert_eq!(Enum::from_unchecked(2_u8), Enum::Some); assert_eq!(Enum::from_unchecked(5_u8), Enum::Many); } }