from_variants
is a crate which automatically implements conversion traits for arms of a newtype enum.
//! Example
#![warn(missing_docs)]
#[macro_use]
extern crate from_variants;
/// A sample struct.
#[derive(Debug, Clone, FromVariants)]
pub enum Lorem {
/// Hello world
#[from_variants(skip)]
Str(String),
/// Hello world
Num(u16),
}
fn main() {
println!("{:?}", Lorem::from(10));
}
This generates the following code:
#[macro_use]
extern crate from_variants;
/// A sample struct.
pub enum Lorem {
/// Hello world
#[from_variants(skip)]
Str(String),
/// Hello world
Num(u16),
}
#[doc = "Convert into a `Num` variant."]
impl ::std::convert::From<u16> for Lorem {
fn from(v: u16) -> Self {
Lorem::Num(v)
}
}
fn main() {
// ... elided ...
}
Usage
- Add
#[derive(FromVariants)]
to any enum whose arms are all single-field tuple variants of different types (or skip non-compliant variants, as explained below).
Features
- Ability to skip variants using
#[from_variants(skip)]
-
no_std
support (thanks to @colin_kiegel for example on how to implement this)