Heya, would you be open to supporting ignoring variants in a sequence?
Something like this (the #[enum_iterator(ignore)] is new):
#[derive(Debug, PartialEq, Sequence)]
enum EnumWithCustom {
One,
Two,
#[enum_iterator(ignore)]
Custom(String),
}
#[test]
fn variant_can_be_ignored() {
assert_eq!(
all::<EnumWithCustom>().collect::<Vec<_>>(),
vec![EnumWithCustom::One, EnumWithCustom::Two]
);
}
Sometimes I have an enum with a last variant being "extensible" (and custom deserialization).
I tried implementing it (#37), and did this:
-
Here's a function to check for the #[enum_iterator(ignore)] attribute.
/// Returns whether the `#[enum_iterator(ignore)]` attribute exists.
fn is_ignored(field_or_variant_attrs: &[Attribute]) -> bool {
field_or_variant_attrs.iter().any(|attr| {
matches!(&attr.meta, Meta::List(meta_list)
if meta_list.path.is_ident("enum_iterator") &&
matches!(meta_list.parse_args::<Ident>(), Ok(ident) if ident == "ignore"))
})
}
-
fn next(self) { .. } and related functions need to skip ignored variants
// impl Sequence
fn next(&self) -> ::core::option::Option<Self> {
match *self {
EnumWithCustom::One {} => next_variant(1usize),
EnumWithCustom::Two {} => next_variant(2usize),
// EnumWithCustom::Custom(..) => // ??,
}
}
// or maybe `next_variant` is the one that needs to change
-
Adding this to advance_enum partially gets us forward:
let i_next = {
let mut i_next = i;
while variants
.get(i_next)
.is_some_and(|variant| is_ignored(&variant.attrs))
{
i_next += 1;
}
i_next
};
advance_enum_arm(crate_path, ty, direction, i_next, variant)
-
Similar for Direction::Backward, but using i_prev = i_prev.saturating_sub(1)
-
Ignore field types for trait bounds.
-
Ignore field types in cardinality.
-
It's also a bit more complex if there are ignored variants in the middle of the enum.
-
I haven't looked at how Sequence works for structs.
Heya, would you be open to supporting ignoring variants in a sequence?
Something like this (the
#[enum_iterator(ignore)]is new):Sometimes I have an enum with a last variant being "extensible" (and custom deserialization).
I tried implementing it (#37), and did this:
Here's a function to check for the
#[enum_iterator(ignore)]attribute.fn next(self) { .. }and related functions need to skip ignored variantsAdding this to
advance_enumpartially gets us forward:Similar for
Direction::Backward, but usingi_prev = i_prev.saturating_sub(1)Ignore field types for trait bounds.
Ignore field types in cardinality.
It's also a bit more complex if there are ignored variants in the middle of the enum.
I haven't looked at how
Sequenceworks forstructs.