Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[storage] Allow one variant enum to derive SpreadLayout #942

Merged
merged 3 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/storage/derive/src/spread_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ 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().len() >= 1, "can only operate on enums");
tash-2s marked this conversation as resolved.
Show resolved Hide resolved
let footprint_body = footprint(s);
let requires_deep_clean_up_body = requires_deep_clean_up(s);
let pull_body = s
Expand Down
48 changes: 48 additions & 0 deletions crates/storage/derive/src/tests/spread_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down