Option.is_empty?


#1

Hi,

i’ve been exploring serdepackage. it has recently added support for not serializing empty fields. The problem is, developers had to add two attributes for this purpose, one of which handles all collections with method is_empty, and the other handles Option, because latter has is_none method. And due to code generation happening on syntax tree, with no type info, there’s no way to deduce which one to use.

So I’m curious a bit, if it would be Ok to add Option.is_empty as an alias method.

Thanks.


#2

have you tried creating a dummy trait that adds the is_empty method and then implementing that trait for Option? It might just do the trick.


#3

Sounds very reasonable. Though it would require importing that trait during codegen from serde… Worth a try.


#4

I meant as a first test you could do this completely in your code without touching serde.


#5

I checked this in my code, and then added this tiny extension to serde and serde_codegen. Works like a charm. The only inconvenience left is the warning about unused import, for which I didn’t figure out yet how to add attribute. Simply adding `#[allow(unused_imports)] to quasi-quotation does nothing, sadly.


#6

I’m trying to construct use marked with attribute this way:

    let use_empty = builder.item()
        .attr().allow(&["unused_imports"])
        .use_().global().ids(&["serde", "ser", "empty", "Empty"])
        .build().build();

And it lacks that attribute anyway. The result type is Past::Item, as expected.

UPD:
A simpler

    let allow_unused_imports = builder.attr().allow(&["unused_imports"]);

doesn’t succeed either.