Skip to content

Add an IsEmpty derive macro and use it in pos-accounting#2096

Open
oliv3rdrt wants to merge 1 commit into
mintlayer:masterfrom
oliv3rdrt:is_empty_derive
Open

Add an IsEmpty derive macro and use it in pos-accounting#2096
oliv3rdrt wants to merge 1 commit into
mintlayer:masterfrom
oliv3rdrt:is_empty_derive

Conversation

@oliv3rdrt

Copy link
Copy Markdown

Problem

PoSAccountingData::is_empty is written by hand as the conjunction of its five fields' is_empty calls, and carries a // TODO: avoid manual implementation (mintlayer/mintlayer-core#669). As the issue notes, adding a field and forgetting to update such a method is an easy mistake.

Change

Add an IsEmpty trait and a derive macro for it, as a new is-empty / is-empty-derive crate pair that mirrors the existing typename / typename-derive setup. The derive generates is_empty as the AND of each field's own is_empty, so a struct is empty exactly when all of its fields are empty (a field-less struct is always empty). Every field type therefore needs an is_empty method: the standard collections, String, or another IsEmpty type.

Apply the derive to PoSAccountingData, removing the manual implementation and the TODO. pos-accounting re-exports the trait so the existing callers only need it in scope.

Tests

The is-empty crate has unit tests covering named-field, tuple and unit structs, and the empty and non-empty cases. The existing pos-accounting and chainstate tests still pass.

Closes #669

PoSAccountingData had a hand-written is_empty that ANDs all of its fields, with a
TODO asking for it to be generated. It is easy to add a field and forget to
update such a method.

Add an is-empty crate with an IsEmpty trait and a matching derive, mirroring the
existing typename/typename-derive pair. The derive implements is_empty as the
conjunction of each field's own is_empty, so a value is empty when all of its
fields are empty. Apply it to PoSAccountingData and drop the manual method;
pos-accounting re-exports the trait so existing callers keep working.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce IsEmpty derive trait

1 participant