serde icon indicating copy to clipboard operation
serde copied to clipboard

Turn skip_field into a skip_field_if_possible

Open Manishearth opened this issue 4 years ago • 3 comments

Currently, skip_field on SerializeStruct is expected to unconditionally skip the field.

Unfortunately, this is not possible for all serialization formats, e.g. bincode, and this leads to issues like https://github.com/serde-rs/serde/issues/1732, which is currently WONTFIX since serde does not wish to add further toggles.

This means that the status quo is that skip_serializing_if should only ever be used by end user crates (not libraries wishing to provide more efficient serialization, because then they can't be used with a class of deserializer). This is suboptimal and is a bit of a footgun.

Ideally, all libraries can use this feature without having to worry about it. Perhaps a path forward is to turn skip_field into skip_field_if_possible and give it the same signature as serialize_field. serde-json, etc, can continue to implement SerializeStruct without any modifications since they use the default. bincode can write skip_field_if_possible to forward to serialize_field, i.e. it can choose to never skip a field.

A downside is that the footgun still remains in a smaller form where if skip_serializing_if was used by a crate author because calling .serialize() on the field would panic (or do something bad); and with this change we're tweaking the guarantee of skip_serializing_if

I have a patch with this feature in https://github.com/serde-rs/serde/compare/master...Manishearth:skip-if-possible?expand=1

This would be a breaking change since serde-derive and serde would have to be updated in tandem, and any manual implementors (I doubt there are many) of skip_field would have to update it.

Manishearth avatar Apr 05 '21 18:04 Manishearth

Bump, this would be a great feature. It also seems like there would be little impact to existing crates.

dtzxporter avatar Aug 30 '21 18:08 dtzxporter

Stumbled over this. Yes, the "skip-if" attribute is a massive footgun in its current incarnation.

dominikwerder avatar Jul 20 '23 07:07 dominikwerder

This would also help with the issues discussed in this thread for the avro (de)serializer: https://github.com/apache/avro-rs/pull/227

jdarais avatar Jul 26 '25 04:07 jdarais