https://github.com/taiki-e/derive_utils
A procedural macro helper for easily writing custom derives for enums.
https://github.com/taiki-e/derive_utils
proc-macro rust
Last synced: 10 months ago
JSON representation
A procedural macro helper for easily writing custom derives for enums.
- Host: GitHub
- URL: https://github.com/taiki-e/derive_utils
- Owner: taiki-e
- License: apache-2.0
- Created: 2018-12-15T06:25:51.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2024-10-20T18:33:55.000Z (over 1 year ago)
- Last Synced: 2024-10-20T22:59:33.636Z (over 1 year ago)
- Topics: proc-macro, rust
- Language: Shell
- Homepage: https://docs.rs/derive_utils
- Size: 369 KB
- Stars: 31
- Watchers: 3
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# derive_utils
[](https://crates.io/crates/derive_utils)
[](https://docs.rs/derive_utils)
[](#license)
[](https://www.rust-lang.org)
[](https://github.com/taiki-e/derive_utils/actions)
A procedural macro helper for easily writing [derives macros][proc-macro-derive] for enums.
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
derive_utils = "0.15"
```
## Examples
[`quick_derive!`] macro make easy to write [`proc_macro_derive`][proc-macro-derive]
like deriving trait to enum so long as all variants are implemented that trait.
```rust
use derive_utils::quick_derive;
use proc_macro::TokenStream;
#[proc_macro_derive(Iterator)]
pub fn derive_iterator(input: TokenStream) -> TokenStream {
quick_derive! {
input,
// trait path
std::iter::Iterator,
// trait definition
trait Iterator {
type Item;
fn next(&mut self) -> Option;
fn size_hint(&self) -> (usize, Option);
}
}
}
#[proc_macro_derive(ExactSizeIterator)]
pub fn derive_exact_size_iterator(input: TokenStream) -> TokenStream {
quick_derive! {
input,
// trait path
std::iter::ExactSizeIterator,
// super trait's associated types
,
// trait definition
trait ExactSizeIterator: Iterator {
fn len(&self) -> usize;
}
}
}
```
### Generated code
When deriving for enum like the following:
```rust
#[derive(Iterator, ExactSizeIterator, Future)]
enum Enum {
A(A),
B(B),
}
```
Code like this will be generated:
```rust
enum Enum {
A(A),
B(B),
}
#[automatically_derived]
impl std::iter::Iterator for Enum
where
A: std::iter::Iterator,
B: std::iter::Iterator::Item>,
{
type Item = ::Item;
fn next(&mut self) -> Option {
match self {
Enum::A(x) => x.next(),
Enum::B(x) => x.next(),
}
}
fn size_hint(&self) -> (usize, Option) {
match self {
Enum::A(x) => x.size_hint(),
Enum::B(x) => x.size_hint(),
}
}
}
#[automatically_derived]
impl std::iter::ExactSizeIterator for Enum
where
A: std::iter::ExactSizeIterator,
B: std::iter::ExactSizeIterator::Item>,
{
fn len(&self) -> usize {
match self {
Enum::A(x) => x.len(),
Enum::B(x) => x.len(),
}
}
}
```
## Related Projects
- [auto_enums]: A library for to allow multiple return types by automatically generated enum.
- [io-enum]: \#\[derive(Read, Write, Seek, BufRead)\] for enums.
- [iter-enum]: \#\[derive(Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, Extend)\] for enums.
[auto_enums]: https://github.com/taiki-e/auto_enums
[io-enum]: https://github.com/taiki-e/io-enum
[iter-enum]: https://github.com/taiki-e/iter-enum
[proc-macro-derive]: https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros
[`quick_derive!`]: https://docs.rs/derive_utils/latest/derive_utils/macro.quick_derive.html
## License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
[MIT license](LICENSE-MIT) at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.