Reduce serde and iterator forwarding boilerplate#65
Conversation
Collapse hand-copied forwarding methods into three small declarative macros, replacing roughly 115 near-identical method bodies: - with.rs: forward_singleton_deserialize! for the SingletonMap and SingletonMapRecursive Deserializer impls, and forward_visit_scalars! for the recursive Visitor's scalar visits. The recursive visit_some/seq/map/newtype methods stay explicit. - ser.rs: serialize_value_from_scalars! for the uniform fixed-width integer and float serialize methods across five Serializer impls. bool, i128, and u128 keep their per-serializer handling. - ast.rs: mapping_projection_iter! for the Mapping key/value iterator views. IntoIter stays explicit. Also remove two unused private helpers in event_de. No behavior change; the public API is unchanged.
|
Review (2026-06-12). The red CI is just formatting: Went through the macro expansions against the original hand-written bodies:
One observation worth a line in the PR description: for |
Summary
Collapses hand-copied forwarding methods into three small private
macro_rules!, replacing ~115 near-identical method bodies. Net −638 src LOC (31,070 → 30,432) with no behavior change.src/with.rsforward_singleton_deserialize!(both Deserializer impls, 48 methods) +forward_visit_scalars!(recursive Visitor's 20 scalar visits)src/ser.rsserialize_value_from_scalars!across 5 Serializer impls (10 uniform numeric methods)src/ast.rsmapping_projection_iter!for 7 Mapping iterator viewssrc/event_de/source.rsscalar_key_at/scalar_key_at_inWhat is intentionally left explicit
These are not boilerplate and stay hand-written:
with.rs: the recursivevisit_some/visit_seq/visit_map/visit_newtype_struct(real recursion) and the*AsEnumvisitors.ser.rs:bool,i128,u128keep per-serializer handling (lossless 128-bit narrowing inValueSerializer, width preservation inPreserveNumberSerializer).&mut Serializer<W>is untouched.ast.rs:IntoIter(identity, no projection).Verification
cargo test: 909 passed, 0 failed; 8 doctests passedcargo clippy --all-targets: clean--no-default-featuresdocs/PUBLIC_API.txt(scripts/check-public-api.shexits 0 with no diff) — SemVer-safe, no doc update requiredThe macros are private (no
#[macro_export]) and expand to the same items that were written by hand, so the change is purely internal.