diff --git a/crates/storage/derive/src/spread_layout.rs b/crates/storage/derive/src/spread_layout.rs index b20f3e5b28e..c517027b5d8 100644 --- a/crates/storage/derive/src/spread_layout.rs +++ b/crates/storage/derive/src/spread_layout.rs @@ -121,7 +121,10 @@ fn spread_layout_struct_derive(s: &synstructure::Structure) -> TokenStream2 { /// `SpreadLayout` derive implementation for `enum` types. fn spread_layout_enum_derive(s: &synstructure::Structure) -> TokenStream2 { - assert!(s.variants().len() >= 2, "can only operate on enums"); + assert!( + !s.variants().is_empty(), + "encountered invalid empty enum type deriving SpreadLayout trait" + ); let footprint_body = footprint(s); let requires_deep_clean_up_body = requires_deep_clean_up(s); let pull_body = s diff --git a/crates/storage/derive/src/tests/spread_layout.rs b/crates/storage/derive/src/tests/spread_layout.rs index 286a509e64b..296ef20b92b 100644 --- a/crates/storage/derive/src/tests/spread_layout.rs +++ b/crates/storage/derive/src/tests/spread_layout.rs @@ -167,6 +167,54 @@ fn struct_works() { } } +#[test] +fn one_variant_enum_works() { + synstructure::test_derive! { + spread_layout_derive { + enum OneVariantEnum { + A, + } + } + expands to { + const _: () = { + impl ::ink_storage::traits::SpreadLayout for OneVariantEnum { + #[allow(unused_comparisons)] + const FOOTPRINT : ::core::primitive::u64 = 1 + [0u64, 0u64][(0u64 < 0u64) as ::core::primitive::usize]; + + const REQUIRES_DEEP_CLEAN_UP : ::core::primitive::bool = (false || false); + + fn pull_spread(__key_ptr: &mut ::ink_storage::traits::KeyPtr) -> Self { + match <::core::primitive::u8 as ::ink_storage::traits::SpreadLayout>::pull_spread(__key_ptr) + { + 0u8 => OneVariantEnum::A, + _ => unreachable!("encountered invalid enum discriminant"), + } + } + + fn push_spread(&self, __key_ptr: &mut ::ink_storage::traits::KeyPtr) { + match self { + OneVariantEnum::A => { + { + <::core::primitive::u8 as ::ink_storage::traits::SpreadLayout>::push_spread( + &0u8, + __key_ptr + ); + } + } + } + } + + fn clear_spread(&self, __key_ptr: &mut ::ink_storage::traits::KeyPtr) { + match self { + OneVariantEnum::A => {} + } + } + } + }; + } + } +} + #[test] fn enum_works() { synstructure::test_derive! {