use typesize::{derive::TypeSize, TypeSize}; #[test] fn enum_derive() { #[derive(TypeSize)] #[allow(clippy::large_enum_variant)] enum DerivedEnum { BigVariant([u8; 1024]), ValNamed { field: u8 }, Vec(Vec), None, } let vec_variant = DerivedEnum::Vec(vec![0, 1, 2, 3]); let big_variant = DerivedEnum::BigVariant([0; 1024]); let val_variant = DerivedEnum::ValNamed { field: 0 }; let none_variant = DerivedEnum::None; assert_eq!( vec_variant.get_size(), core::mem::size_of::() + (core::mem::size_of::() * 4) ); assert_eq!(val_variant.get_size(), core::mem::size_of::()); assert_eq!(big_variant.get_size(), core::mem::size_of::()); assert_eq!(none_variant.get_size(), core::mem::size_of::()); } #[test] fn enum_no_data() { #[derive(TypeSize)] #[allow(dead_code)] enum NoData { JustVariants, OnlyStore, TheTag, Please, } assert_eq!( NoData::JustVariants.get_size(), core::mem::size_of::() ); } #[test] fn enum_padding() { #[derive(TypeSize)] enum PaddingTest { Variant(u8, u64), } assert_eq!( PaddingTest::Variant(0, 0).get_size(), core::mem::size_of::() ); } #[test] fn enum_generic() { #[derive(TypeSize)] #[allow(dead_code)] enum Result { Ok(T), Err(E), } assert_eq!( Result::::Ok(0).get_size(), core::mem::size_of::>() ); } #[test] fn enum_with_attr() { fn returns_1(_: &usize) -> usize { 1 } fn returns_2(_: &usize) -> usize { 2 } #[derive(TypeSize)] enum Foo { A(usize), B(#[typesize(with = returns_1)] usize), C { #[typesize(with = returns_2)] field: usize, }, } assert_eq!(Foo::A(0).extra_size(), 0); assert_eq!(Foo::B(0).extra_size(), 1); assert_eq!(Foo::C { field: 0 }.extra_size(), 2); }