Sugar for wrapping structs in enums


It would be nice to have sugar for wrapping structs in enums. Perhaps rust could derive the following two traits.

// Returns a reference to the wrapped struct "as A"
trait InnerAs<A: Send+Sync> {
    fn inner_as(&self) -> Option<&A>;

// Wrap a struct to create an enum
trait FromInner<A> {
    fn from_inner(inner: A) -> Self;

Here is a contrived example

trait Error {}

// Wrapped errors from submodule 1 and 2
pub struct Error1; 
pub struct Error2; 
impl Error for Error1 {}
impl Error for Error2 {}

// Error from present module
pub struct MyError {
    pub description: Box<String>,    
    pub kind: MyErrorKind

// #[deriving(FromInner)]
// #[deriving(InnerAs(Error))]
pub enum MyErrorKind {

I would like rust to derive the following boiler plate code.

impl InnerAs<Error> for MyErrorKind {

    fn inner_as<'a>(&'a self) -> Option<&'a Error> {
        match *self {
            CausedBy1(ref error1) => Some(error1 as &'a Error),
            CausedBy2(ref error2) => Some(error2 as &'a Error),
            SomethingElse => None,
            Excuses => None,

Rust should complain if ErrorKind has a variant that contains more than 1 element

My code contains a lifetime error which is somehow caused by generics, but I hope that the general idea is clear

I would also like rust to derive the following boiler plate-code.

impl FromInner<Error1> for MyErrorKind {
    fn from_inner(error1: Error1) -> MyErrorKind {

impl FromInner<Error2> for MyErrorKind {
    fn from_inner(error2: Error2) -> MyErrorKind {

Rust should complain if any variant contains more than 1 element, or if it contains two variants with the same inner type

I believe that the code requires multicast

The syntax can probably be improved in many ways, but I like the general idea because it allows me to use match when I work with containers of structs that share a trait. Anyway I just wanted to throw the ideas in the air, hoping that someone would feel inspired to develop them in to an RFC.

EDIT: Typos layout language