{"id":13822404,"url":"https://github.com/taiki-e/auto_enums","last_synced_at":"2025-05-14T09:08:33.677Z","repository":{"id":44139393,"uuid":"161024964","full_name":"taiki-e/auto_enums","owner":"taiki-e","description":"A library for to allow multiple return types by automatically generated enum.","archived":false,"fork":false,"pushed_at":"2024-10-20T18:52:17.000Z","size":1183,"stargazers_count":349,"open_issues_count":6,"forks_count":12,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-10-20T23:27:17.789Z","etag":null,"topics":["no-std","proc-macro","rust"],"latest_commit_sha":null,"homepage":"https://docs.rs/auto_enums","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taiki-e.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"taiki-e"}},"created_at":"2018-12-09T09:43:31.000Z","updated_at":"2024-10-20T18:52:20.000Z","dependencies_parsed_at":"2023-10-12T23:59:43.793Z","dependency_job_id":"97bc0364-fcec-479e-a5ef-a722d619a62e","html_url":"https://github.com/taiki-e/auto_enums","commit_stats":{"total_commits":669,"total_committers":3,"mean_commits":223.0,"dds":0.002989536621823663,"last_synced_commit":"4cc4db63ed9e44c8ae01e653cd82c8617739174b"},"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taiki-e%2Fauto_enums","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taiki-e%2Fauto_enums/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taiki-e%2Fauto_enums/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taiki-e%2Fauto_enums/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taiki-e","download_url":"https://codeload.github.com/taiki-e/auto_enums/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248334916,"owners_count":21086473,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["no-std","proc-macro","rust"],"created_at":"2024-08-04T08:01:59.075Z","updated_at":"2025-04-11T03:26:46.762Z","avatar_url":"https://github.com/taiki-e.png","language":"Rust","readme":"# auto_enums\n\n[![crates.io](https://img.shields.io/crates/v/auto_enums?style=flat-square\u0026logo=rust)](https://crates.io/crates/auto_enums)\n[![docs.rs](https://img.shields.io/badge/docs.rs-auto__enums-blue?style=flat-square\u0026logo=docs.rs)](https://docs.rs/auto_enums)\n[![license](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue?style=flat-square)](#license)\n[![msrv](https://img.shields.io/badge/msrv-1.56-blue?style=flat-square\u0026logo=rust)](https://www.rust-lang.org)\n[![github actions](https://img.shields.io/github/actions/workflow/status/taiki-e/auto_enums/ci.yml?branch=main\u0026style=flat-square\u0026logo=github)](https://github.com/taiki-e/auto_enums/actions)\n\nA library for to allow multiple return types by automatically generated enum.\n\nThis crate is a procedural macro implementation of the features discussions\nin [rust-lang/rfcs#2414]. This idea is also known as\n[\"Anonymous sum types\"][rust-lang/rfcs#294].\n\nThis library provides the following attribute macros:\n\n- `#[auto_enum]`\n\n  Parses syntax, creates the enum, inserts variants, and passes specified\n  traits to `#[enum_derive]`.\n\n- `#[enum_derive]`\n\n  Implements specified traits to the enum.\n\n## Usage\n\nAdd this to your `Cargo.toml`:\n\n```toml\n[dependencies]\nauto_enums = \"0.8\"\n```\n\n## Examples\n\n`#[auto_enum]`'s basic feature is to wrap the value returned by the obvious\nbranches (`match`, `if`, `return`, etc..) by an enum that implemented the\nspecified traits.\n\n```rust\nuse auto_enums::auto_enum;\n\n#[auto_enum(Iterator)]\nfn foo(x: i32) -\u003e impl Iterator\u003cItem = i32\u003e {\n    match x {\n        0 =\u003e 1..10,\n        _ =\u003e vec![5, 10].into_iter(),\n    }\n}\n```\n\n`#[auto_enum]` generates code in two stages.\n\nFirst, `#[auto_enum]` will do the following.\n\n- parses syntax\n- creates the enum\n- inserts variants\n\nCode like this will be generated:\n\n```rust\nfn foo(x: i32) -\u003e impl Iterator\u003cItem = i32\u003e {\n    #[::auto_enums::enum_derive(Iterator)]\n    enum __Enum1\u003c__T1, __T2\u003e {\n        __T1(__T1),\n        __T2(__T2),\n    }\n\n    match x {\n        0 =\u003e __Enum1::__T1(1..10),\n        _ =\u003e __Enum1::__T2(vec![5, 10].into_iter()),\n    }\n}\n```\n\nNext, `#[enum_derive]` implements the specified traits.\n\n[Code like this will be generated](tests/expand/enum_derive/example-1.expanded.rs)\n\n`#[auto_enum]` can also parse nested arms/branches by using the `#[nested]`\nattribute.\n\n```rust\nuse auto_enums::auto_enum;\n\n#[auto_enum(Iterator)]\nfn foo(x: i32) -\u003e impl Iterator\u003cItem = i32\u003e {\n    match x {\n        0 =\u003e 1..10,\n        #[nested]\n        _ =\u003e match x {\n            1 =\u003e vec![5, 10].into_iter(),\n            _ =\u003e 0..=x,\n        },\n    }\n}\n```\n\nSee [documentation](https://docs.rs/auto_enums) for more details.\n\n## Supported traits\n\n`#[enum_derive]` implements the supported traits and passes unsupported\ntraits to `#[derive]`.\n\n`#[enum_derive]` supports many of the standard library traits and some popular\nthird-party libraries traits such as [rayon], [futures][futures03],\n[tokio][tokio1], [http_body][http_body1]. **See [documentation](https://docs.rs/auto_enums/latest/auto_enums/#supported-traits) for a complete list of supported traits.**\n\nIf you want to use traits that are not supported by `#[enum_derive]`, you\ncan use another crate that provides [derives macros][proc-macro-derive], or\nyou can define derives macros yourself ([derive_utils] probably can help it).\n\nBasic usage of `#[enum_derive]`\n\n```rust\nuse auto_enums::enum_derive;\n\n// `#[enum_derive]` implements `Iterator`, and `#[derive]` implements `Clone`.\n#[enum_derive(Iterator, Clone)]\nenum Foo\u003cA, B\u003e {\n    A(A),\n    B(B),\n}\n```\n\n## Optional features\n\n- **`std`** *(enabled by default)*\n  - Enable to use `std` library's traits.\n- **`ops`**\n  - Enable to use `[std|core]::ops`'s `Deref`, `DerefMut`, `Index`, `IndexMut`, and `RangeBounds` traits.\n- **`convert`**\n  - Enable to use `[std|core]::convert`'s `AsRef` and `AsMut` traits.\n- **`fmt`**\n  - Enable to use `[std|core]::fmt`'s traits other than `Debug`, `Display` and `Write`.\n- **`transpose_methods`**\n  - Enable to use `transpose*` methods.\n- **`futures03`**\n  - Enable to use [futures v0.3][futures03] traits.\n- **`futures01`**\n  - Enable to use [futures v0.1][futures01] traits.\n- **`rayon`**\n  - Enable to use [rayon] traits.\n- **`serde`**\n  - Enable to use [serde] traits.\n- **`tokio1`**\n  - Enable to use [tokio v1][tokio1] traits.\n- **`tokio03`**\n  - Enable to use [tokio v0.3][tokio03] traits.\n- **`tokio02`**\n  - Enable to use [tokio v0.2][tokio02] traits.\n- **`tokio01`**\n  - Enable to use [tokio v0.1][tokio01] traits.\n- **`http_body1`**\n  - Enable to use [http_body v1][http_body1] traits.\n- **`coroutine_trait`**\n  - Enable to use `[std|core]::ops::Coroutine` trait.\n  - Note that this feature is unstable and may cause incompatible changes between patch versions.\n- **`fn_traits`**\n  - Enable to use `[std|core]::ops`'s `Fn`, `FnMut`, and `FnOnce` traits.\n  - Note that this feature is unstable and may cause incompatible changes between patch versions.\n- **`trusted_len`**\n  - Enable to use `[std|core]::iter::TrustedLen` trait.\n  - Note that this feature is unstable and may cause incompatible changes between patch versions.\n\n### `type_analysis` feature\n\nAnalyze return type of function and `let` binding.\n\n*Note that this feature is still experimental.*\n\nExamples:\n\n```rust\nuse auto_enums::auto_enum;\n\n#[auto_enum] // there is no need to specify std library's traits\nfn func1(x: i32) -\u003e impl Iterator\u003cItem = i32\u003e {\n    match x {\n        0 =\u003e 1..10,\n        _ =\u003e vec![5, 10].into_iter(),\n    }\n}\n\n#[auto_enum]\nfn func2(x: i32) {\n    // Unlike `feature(impl_trait_in_bindings)`, this works on stable compilers.\n    #[auto_enum]\n    let iter: impl Iterator\u003cItem = i32\u003e = match x {\n        0 =\u003e Some(0).into_iter(),\n        _ =\u003e 0..x,\n    };\n}\n```\n\nPlease be careful if you return another traits with the same name.\n\n## Related Projects\n\n- [derive_utils]: A procedural macro helper for easily writing [derives macros][proc-macro-derive] for enums.\n- [io-enum]: \\#\\[derive(Read, Write, Seek, BufRead)\\] for enums.\n- [iter-enum]: \\#\\[derive(Iterator, DoubleEndedIterator, ExactSizeIterator, Extend)\\] for enums.\n\n[derive_utils]: https://github.com/taiki-e/derive_utils\n[futures01]: https://docs.rs/futures/0.1\n[futures03]: https://docs.rs/futures/0.3\n[io-enum]: https://github.com/taiki-e/io-enum\n[iter-enum]: https://github.com/taiki-e/iter-enum\n[proc-macro-derive]: https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros\n[rayon]: https://docs.rs/rayon/1\n[rust-lang/rfcs#294]: https://github.com/rust-lang/rfcs/issues/294\n[rust-lang/rfcs#2414]: https://github.com/rust-lang/rfcs/issues/2414\n[serde]: https://docs.rs/serde/1\n[tokio01]: https://docs.rs/tokio/0.1\n[tokio02]: https://docs.rs/tokio/0.2\n[tokio03]: https://docs.rs/tokio/0.3\n[tokio1]: https://docs.rs/tokio/1\n[http_body1]: https://docs.rs/http_body/1\n\n## License\n\nLicensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or\n[MIT license](LICENSE-MIT) at your option.\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall\nbe dual licensed as above, without any additional terms or conditions.\n","funding_links":["https://github.com/sponsors/taiki-e"],"categories":["Rust"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaiki-e%2Fauto_enums","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaiki-e%2Fauto_enums","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaiki-e%2Fauto_enums/lists"}