Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/teddriggs/from_variants
A rust proc-macro to generate conversions for an enum of newtype variants
https://github.com/teddriggs/from_variants
Last synced: 2 months ago
JSON representation
A rust proc-macro to generate conversions for an enum of newtype variants
- Host: GitHub
- URL: https://github.com/teddriggs/from_variants
- Owner: TedDriggs
- Created: 2017-04-18T21:42:34.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-08T15:42:05.000Z (almost 2 years ago)
- Last Synced: 2024-10-16T12:05:35.906Z (3 months ago)
- Language: Rust
- Size: 64.5 KB
- Stars: 28
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
[![Build Status](https://github.com/TedDriggs/from_variants/workflows/CI/badge.svg)](https://github.com/TedDriggs/from_variants/actions)
[![Latest Version](https://img.shields.io/crates/v/from_variants.svg)](https://crates.io/crates/from_variants)# Newtype Variant Conversions
Rust macro crate to automatically generate conversions from variant types into the target enum.
This crate requires Rust 1.45 or above to compile on stable.
## Examples
```rust
use from_variants::FromVariants;#[derive(Debug, Clone, PartialEq, Eq, FromVariants)]
pub enum Lorem {
Str(String),
Num(u16),
}fn main() {
assert_eq!(Lorem::Num(10), Lorem::from(10));
}
```You can skip variants to avoid type collisions:
```rust
use from_variants::FromVariants;#[derive(Debug, Clone, PartialEq, Eq, FromVariants)]
pub enum Ipsum {
Hello(String),#[from_variants(skip)]
Goodbye(String),
}fn main() {
assert_eq!(Ipsum::Hello("John".to_string()), Ipsum::from("John".to_string()));
}
```## Features
- **Variant opt-out**: To skip a variant, add `#[from_variants(skip)]` to that variant.
- **Conversion opt-in**: Use `#[from_variants(into)]` on an enum or variant to generate conversions
that will automatically convert - for example, accepting a `&str` for a `String` variant.
This must be used sparingly to avoid generating conflicting impls.
- **no_std support**: Generated conversions do not depend on the standard library.