Crate ace_it

source ·
Expand description

Auto Convert Enums

This crate provides a proc macro that generates From impls for unnamed enum fields that have a type.

Ever get tired of writing the same From impls to enable using ? on some Result types? This crate is for you!

This is useful for enums that are used as a wrapper for a collecting multiple errors into one big enum.

Example

#[macro_use] extern crate ace_it;

#[derive(Debug)]
#[ace_it]
enum Error {
  Io(std::io::Error),
  ParseInt(std::num::ParseIntError),
  ParseFloat(std::num::ParseFloatError),
}

After this, Error has three From impls:

Now you can use ? on any of these types and get an Error back.

 
use std::io::Read;

fn read_int<R: Read>(reader: &mut R) -> Result<i32, Error> {
    let mut buf = String::new();
    reader.read_to_string(&mut buf)?;
    Ok(buf.parse()?)
}

Attribute Macros

Generates From impls for the given enum.