Skip to content

Commit 1a29ba5

Browse files
committed
Added trait for IterVariants, IterVariantNames.
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.
1 parent eea837e commit 1a29ba5

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

enum_derive/src/iter_variants.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ macro_attr! {
2020
2121
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.
2222
23+
It also derives an implementation of the `IterVariants` trait.
24+
2325
# Example
2426
2527
```rust
@@ -32,8 +34,11 @@ macro_attr! {
3234
}
3335
3436
# fn main() {
37+
use enum_derive::iter_variants::IterVariants;
38+
3539
let names: Vec<Cheese> = Cheese::iter_variants().collect();
3640
assert_eq!(names, vec![Cheese::RedLeicester, Cheese::Tilsit, Cheese::Stilton]);
41+
assert_eq!(<Cheese as IterVariants>::variants().next(), Some(Cheese::RedLeicester));
3742
# }
3843
```
3944
*/
@@ -70,6 +75,15 @@ macro_rules! IterVariants {
7075
}
7176
}
7277
}
78+
79+
impl $crate::iter_variants::IterVariants for $name {
80+
type Iter = $itername;
81+
82+
#[inline]
83+
fn variants() -> Self::Iter {
84+
$name::iter_variants()
85+
}
86+
}
7387
};
7488

7589
(
@@ -89,6 +103,15 @@ macro_rules! IterVariants {
89103
}
90104
}
91105
}
106+
107+
impl $crate::iter_variants::IterVariants for $name {
108+
type Iter = $itername;
109+
110+
#[inline]
111+
fn variants() -> Self::Iter {
112+
$name::iter_variants()
113+
}
114+
}
92115
};
93116

94117
(
@@ -187,6 +210,8 @@ macro_attr! {
187210
188211
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.
189212
213+
It also derives an implementation of the `IterVariantNames` trait.
214+
190215
# Example
191216
192217
```rust
@@ -198,8 +223,11 @@ macro_attr! {
198223
}
199224
200225
# fn main() {
226+
use enum_derive::iter_variants::IterVariantNames;
227+
201228
let names: Vec<&str> = Currency::iter_variant_names().collect();
202229
assert_eq!(names, vec!["Pounds", "FrenchFranks", "Lira", "DeutscheMark"]);
230+
assert_eq!(<Currency as IterVariantNames>::variant_names().next(), Some("Pounds"));
203231
# }
204232
```
205233
*/
@@ -236,6 +264,15 @@ macro_rules! IterVariantNames {
236264
}
237265
}
238266
}
267+
268+
impl $crate::iter_variants::IterVariantNames for $name {
269+
type Iter = $itername;
270+
271+
#[inline]
272+
fn variant_names() -> Self::Iter {
273+
$name::iter_variant_names()
274+
}
275+
}
239276
};
240277

241278
(
@@ -255,6 +292,15 @@ macro_rules! IterVariantNames {
255292
}
256293
}
257294
}
295+
296+
impl $crate::iter_variants::IterVariantNames for $name {
297+
type Iter = $itername;
298+
299+
#[inline]
300+
fn variant_names() -> Self::Iter {
301+
$name::iter_variant_names()
302+
}
303+
}
258304
};
259305

260306
(
@@ -342,3 +388,13 @@ macro_rules! IterVariantNames {
342388
}
343389
};
344390
}
391+
392+
pub trait IterVariants: Sized {
393+
type Iter: Iterator<Item=Self>;
394+
fn variants() -> Self::Iter;
395+
}
396+
397+
pub trait IterVariantNames {
398+
type Iter: Iterator<Item=&'static str>;
399+
fn variant_names() -> Self::Iter;
400+
}

enum_derive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ IterVariants! { (Vars) enum ItAintRight { BabeNo, NoNo, BoyBoy } }
4848
#[macro_use] mod fmt;
4949
#[macro_use] mod from_str;
5050
#[macro_use] mod inner;
51-
#[macro_use] mod iter_variants;
51+
#[macro_use] pub mod iter_variants;
5252
#[macro_use] mod step_variants;
5353
#[macro_use] mod tag;
5454

0 commit comments

Comments
 (0)