trait_enum

Crates.iotrait_enum
lib.rstrait_enum
version0.5.0
sourcesrc
created_at2018-04-06 14:54:47.217617
updated_at2020-08-31 17:43:54.983481
descriptionEasy enum wrapper that implements all traits that the wrapped objects implement
homepagehttps://github.com/trangar/trait_enum
repositoryhttps://github.com/trangar/trait_enum
max_upload_size
id59275
size10,987
Trangar (VictorKoenders)

documentation

README

Trait Enum

Build Status

An enum wrapper for types that implement the same trait

The trait_enum macro generates an enum that manages several objects.

These objects are expected to have the same trait impl

After which the enum will have a std::ops::Deref impl which returns a reference to that trait.

#[macro_use]
extern crate trait_enum;

pub trait CommonTrait {
    fn test(&self) -> u32;
}

pub struct InnerOne;
impl CommonTrait for InnerOne {
    fn test(&self) -> u32 {
        1
    }
}

pub struct InnerTwo;
impl CommonTrait for InnerTwo {
    fn test(&self) -> u32 {
        2
    }
}

trait_enum!{
    pub enum Combined: CommonTrait {
        InnerOne,
        InnerTwo,
    }
}

fn main() {
    use std::ops::Deref;

    let combined = Combined::InnerOne(InnerOne);
    let deref: &dyn CommonTrait = combined.deref();
    assert_eq!(deref.test(), 1);

    let combined = Combined::InnerTwo(InnerTwo);
    let deref: &dyn CommonTrait = combined.deref();
    assert_eq!(deref.test(), 2);
}
Commit count: 16

cargo fmt