Skip to content

Commit

Permalink
Added trait for IterVariants, IterVariantNames.
Browse files Browse the repository at this point in the history
This avoids the issues of the proposed PR by having both the inherent
method *and* the trait, and by using a different method name for the trait.
This is less than ideal, but solves the backwards compatibility issues well
enough.

Closes #32.
  • Loading branch information
DanielKeep committed Sep 22, 2017
1 parent eea837e commit 1a29ba5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
56 changes: 56 additions & 0 deletions enum_derive/src/iter_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ macro_attr! {
Derives a `$name::iter_variants() -> $itername` method. The generated `$itername` type implements `Iterator<Item=$name>`, and yields each of the enumeration's variants. This can only be used on an enum comprised on unitary variants.
It also derives an implementation of the `IterVariants` trait.
# Example
```rust
Expand All @@ -32,8 +34,11 @@ macro_attr! {
}
# fn main() {
use enum_derive::iter_variants::IterVariants;
let names: Vec<Cheese> = Cheese::iter_variants().collect();
assert_eq!(names, vec![Cheese::RedLeicester, Cheese::Tilsit, Cheese::Stilton]);
assert_eq!(<Cheese as IterVariants>::variants().next(), Some(Cheese::RedLeicester));
# }
```
*/
Expand Down Expand Up @@ -70,6 +75,15 @@ macro_rules! IterVariants {
}
}
}

impl $crate::iter_variants::IterVariants for $name {
type Iter = $itername;

#[inline]
fn variants() -> Self::Iter {
$name::iter_variants()
}
}
};

(
Expand All @@ -89,6 +103,15 @@ macro_rules! IterVariants {
}
}
}

impl $crate::iter_variants::IterVariants for $name {
type Iter = $itername;

#[inline]
fn variants() -> Self::Iter {
$name::iter_variants()
}
}
};

(
Expand Down Expand Up @@ -187,6 +210,8 @@ macro_attr! {
Derives a `$name::iter_variant_names() -> $itername` method. The generated `$itername` type implements `Iterator<Item=&'static str>`, and yields the name of each of the enumeration's variants. This can only be used on an enum comprised on unitary variants.
It also derives an implementation of the `IterVariantNames` trait.
# Example
```rust
Expand All @@ -198,8 +223,11 @@ macro_attr! {
}
# fn main() {
use enum_derive::iter_variants::IterVariantNames;
let names: Vec<&str> = Currency::iter_variant_names().collect();
assert_eq!(names, vec!["Pounds", "FrenchFranks", "Lira", "DeutscheMark"]);
assert_eq!(<Currency as IterVariantNames>::variant_names().next(), Some("Pounds"));
# }
```
*/
Expand Down Expand Up @@ -236,6 +264,15 @@ macro_rules! IterVariantNames {
}
}
}

impl $crate::iter_variants::IterVariantNames for $name {
type Iter = $itername;

#[inline]
fn variant_names() -> Self::Iter {
$name::iter_variant_names()
}
}
};

(
Expand All @@ -255,6 +292,15 @@ macro_rules! IterVariantNames {
}
}
}

impl $crate::iter_variants::IterVariantNames for $name {
type Iter = $itername;

#[inline]
fn variant_names() -> Self::Iter {
$name::iter_variant_names()
}
}
};

(
Expand Down Expand Up @@ -342,3 +388,13 @@ macro_rules! IterVariantNames {
}
};
}

pub trait IterVariants: Sized {
type Iter: Iterator<Item=Self>;
fn variants() -> Self::Iter;
}

pub trait IterVariantNames {
type Iter: Iterator<Item=&'static str>;
fn variant_names() -> Self::Iter;
}
2 changes: 1 addition & 1 deletion enum_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ IterVariants! { (Vars) enum ItAintRight { BabeNo, NoNo, BoyBoy } }
#[macro_use] mod fmt;
#[macro_use] mod from_str;
#[macro_use] mod inner;
#[macro_use] mod iter_variants;
#[macro_use] pub mod iter_variants;
#[macro_use] mod step_variants;
#[macro_use] mod tag;

Expand Down

0 comments on commit 1a29ba5

Please sign in to comment.