filterable-enum 0.2.0

A library for generating filterable enums (Combining bitflags and discriminated unions)
Documentation
// Run these safety regressions in both `cargo test` and `cargo +nightly miri test`.
use filterable_enum::{filterable_enum, FilterableEnum};

#[repr(align(64))]
struct Aligned(u8);

macro_rules! check_payloads {
    ($test:ident, $repr:literal) => {
        #[test]
        fn $test() {
            #[filterable_enum(repr = $repr)]
            enum Value<'a> {
                Unit,
                Borrowed(&'a mut [u8]),
                Aligned { payload: Aligned },
            }

            // The inactive payload and padding can be uninitialized. Tag reads
            // must access only the initialized discriminant.
            let unit = Value::Unit;
            assert_eq!(unit.filterable_id(), ValueKind::Unit);
            assert!(unit.filter_ref(ValueKind::Borrowed).is_none());

            let mut bytes = [1, 2, 3];
            let borrowed = Value::Borrowed(&mut bytes);
            for _ in 0..3 {
                assert_eq!(borrowed.filterable_id(), ValueKind::Borrowed);
                let Some(Value::Borrowed(payload)) = borrowed.filter_ref(ValueKind::Borrowed)
                else {
                    panic!("wrong variant");
                };
                assert_eq!(&**payload, &[1, 2, 3]);
            }
            let Some(Value::Borrowed(payload)) = borrowed.filter_and_take(ValueKind::Borrowed)
            else {
                panic!("wrong variant");
            };
            payload[1] = 9;
            assert_eq!(bytes, [1, 9, 3]);

            // Exercise inline payload alignment and padding on both the heap
            // and the stack, including a move and a subsequent variant change.
            let aligned = Box::new(Value::Aligned {
                payload: Aligned(42),
            });
            assert_eq!(aligned.filterable_id(), ValueKind::Aligned);
            let Some(Value::Aligned { payload }) = aligned.filter_ref(ValueKind::Aligned) else {
                panic!("wrong variant");
            };
            assert_eq!(payload.0, 42);
            let mut moved = (*aligned).filter_and_take(ValueKind::Aligned).unwrap();
            assert_eq!(moved.filterable_id(), ValueKind::Aligned);
            if let Value::Aligned { payload } = &moved {
                assert_eq!(payload.0, 42);
            }
            moved = Value::Unit;
            assert_eq!(moved.filterable_id(), ValueKind::Unit);
        }
    };
}

check_payloads!(payloads_u8, "u8");
check_payloads!(payloads_u16, "u16");
check_payloads!(payloads_u32, "u32");
check_payloads!(payloads_u64, "u64");
check_payloads!(payloads_u128, "u128");

macro_rules! check_full_u128 {
    ($($unit:ident),* $(,)?) => {
        #[test]
        fn full_u128_with_borrowed_payload() {
            #[filterable_enum(repr = "u128")]
            enum Wide<'a> {
                $($unit,)*
                Highest(&'a u8),
            }

            for (index, value) in [$(Wide::$unit,)*].into_iter().enumerate() {
                assert_eq!(value.filterable_id() as u128, 1u128 << index);
            }

            let payload = 42;
            let value = Wide::Highest(&payload);
            assert_eq!(value.filterable_id() as u128, 1u128 << 127);
            assert!(value.filter_ref(WideKind::V0).is_none());
            let Some(Wide::Highest(borrowed)) = value.filter_ref(WideKind::Highest) else {
                panic!("wrong variant");
            };
            assert!(std::ptr::eq(*borrowed, &payload));
            let Some(Wide::Highest(borrowed)) = value.filter_and_take(WideKind::Highest) else {
                panic!("wrong variant");
            };
            assert_eq!(*borrowed, 42);
        }
    };
}

// 127 unit variants put the borrowed variant in bit 127, exercising the full
// tag width (including its most significant bit) on both endian targets.
check_full_u128!(
    V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20,
    V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39,
    V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55, V56, V57, V58,
    V59, V60, V61, V62, V63, V64, V65, V66, V67, V68, V69, V70, V71, V72, V73, V74, V75, V76, V77,
    V78, V79, V80, V81, V82, V83, V84, V85, V86, V87, V88, V89, V90, V91, V92, V93, V94, V95, V96,
    V97, V98, V99, V100, V101, V102, V103, V104, V105, V106, V107, V108, V109, V110, V111, V112,
    V113, V114, V115, V116, V117, V118, V119, V120, V121, V122, V123, V124, V125, V126,
);