Unable to get possible values at compile time
Before 1.0 it was possible to use POSSIBLE_VALUES
to do more interesting things. For instance:
/// Convenience method to iterate all of the possible values of an `Enum`
pub trait EnumExt: Sized {
fn all() -> EnumIter<Self>;
}
impl<T: Enum<()>> EnumExt for T {
fn all() -> EnumIter<Self> {
EnumIter {
i: 0,
_phantom: std::marker::PhantomData,
}
}
}
pub struct EnumIter<T> {
i: usize,
_phantom: std::marker::PhantomData<T>,
}
impl<T: Enum<()>> Iterator for EnumIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.i < T::POSSIBLE_VALUES {
return None;
}
let result = T::from_usize(self.i);
self.i += 1;
Some(result)
}
}
I also have code that manually implements the Enum
trait for for a nested enum, like:
enum MyEnum {
A(SomeEnum)
B(AnotherEnum)
}
impl<T> Enum<T> for MyEnum {
type Array = [T; SomeEnum::POSSIBLE_VALUES + AnotherEnum::POSSIBLE_VALUES];
...
}
The only workaround I seem to be able to find involves doing std::mem::size_of::<<SomeEnum as Enum<u8>>::Array>()
which seems extremely hacky and bug-prone.
Am I missing something? If not, is it possible to add some sort of known-compile-time possible value functionality?