Skip to content

Introduce IsEmpty derive trait #669

Open
@azarovh

Description

@azarovh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerCompiler-related issues (primarily rust, but could be something else)enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions