mintlayer-core icon indicating copy to clipboard operation
mintlayer-core copied to clipboard

Introduce IsEmpty derive trait

Open azarovh opened this issue 2 years ago • 0 comments

Consider following example:

struct Foo {
    a: BTreeMap<u32, u32>,
    b: BTreeMap<u32, u32>,
}

impl Foo {
    fn is_empty(&self) -> bool {
        self.a.is_empty() && self.b.is_empty()
    }
}

The problem with this code is that if an additional field c is added it is easy to forget to update is_empty method.

One solution would be to write a procedural macro that would automatically iterate over fields and generate is_empty method.

Potential problem is that it should work for types that don't have 'is_empty' method like POD types. To circumvent that an attribute can be added that requires a type to implement the emptiness property.

azarovh avatar Feb 02 '23 14:02 azarovh