enum_arr 0.1.2

Crate for Enum-Indexed arrays inspired by the Odin Programming Language
Documentation
use std::sync::atomic::AtomicU64;

#[test]
fn array_test() {
    #[derive(Clone, Copy, Debug, enum_arr::Enum)]
    #[repr(u8)]
    enum MyEnum {
        A,
        B,
        C,
    }

    let mut arr = MyEnumArray::new(0);
    arr[MyEnum::C] = 42;
    arr[MyEnum::A] = 12;

    assert_eq!(format!("{:?}", arr), "[A: 12, B: 0, C: 42]");

    let arr2 = MyEnumArray::<String>::default();
    assert_eq!(format!("{:?}", arr2), "[A: \"\", B: \"\", C: \"\"]");
}

#[test]
fn try_init_test() {
    static DROP_COUNT: AtomicU64 = AtomicU64::new(0);

    struct Foo {}
    impl Drop for Foo {
        fn drop(&mut self) {
            DROP_COUNT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
    }

    #[derive(Clone, Copy, Debug, enum_arr::Enum)]
    #[repr(u8)]
    enum MyEnum {
        A,
        B,
        C,
        D,
        E,
    }

    let res: Result<MyEnumArray<Foo>, &'static str> = MyEnumArray::try_init_with(|e| {
        if matches!(e, MyEnum::C) {
            Err("Failed to initialize C")
        } else {
            Ok(Foo {})
        }
    });

    // we expect two values to be dropped: A and B, since C failed to initialize, and D and E were never initialized at all.
    assert!(res.is_err());
    assert_eq!(DROP_COUNT.load(std::sync::atomic::Ordering::SeqCst), 2);
}