Skip to content

Reduce serde and iterator forwarding boilerplate#65

Open
jskoiz wants to merge 1 commit into
mainfrom
claude/funny-knuth-58879a
Open

Reduce serde and iterator forwarding boilerplate#65
jskoiz wants to merge 1 commit into
mainfrom
claude/funny-knuth-58879a

Conversation

@jskoiz

@jskoiz jskoiz commented Jun 8, 2026

Copy link
Copy Markdown
Owner

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.

File Change LOC
src/with.rs forward_singleton_deserialize! (both Deserializer impls, 48 methods) + forward_visit_scalars! (recursive Visitor's 20 scalar visits) −602
src/ser.rs serialize_value_from_scalars! across 5 Serializer impls (10 uniform numeric methods) −252 / +114
src/ast.rs mapping_projection_iter! for 7 Mapping iterator views −222 / +94
src/event_de/source.rs remove unused scalar_key_at / scalar_key_at_in −18

What is intentionally left explicit

These are not boilerplate and stay hand-written:

  • with.rs: the recursive visit_some / visit_seq / visit_map / visit_newtype_struct (real recursion) and the *AsEnum visitors.
  • ser.rs: bool, i128, u128 keep per-serializer handling (lossless 128-bit narrowing in ValueSerializer, width preservation in PreserveNumberSerializer). &mut Serializer<W> is untouched.
  • ast.rs: IntoIter (identity, no projection).

Verification

  • cargo test: 909 passed, 0 failed; 8 doctests passed
  • cargo clippy --all-targets: clean
  • builds clean with and without --no-default-features
  • public API byte-identical to docs/PUBLIC_API.txt (scripts/check-public-api.sh exits 0 with no diff) — SemVer-safe, no doc update required

The macros are private (no #[macro_export]) and expand to the same items that were written by hand, so the change is purely internal.

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.
@jskoiz

jskoiz commented Jun 12, 2026

Copy link
Copy Markdown
Owner Author

Review (2026-06-12). The red CI is just formatting: cargo fmt --all --check fails with a diff at src/ast.rs:1843 — the mapping_projection_iter! invocation for Iter<'a>. Both the ubuntu and windows "Rust checks" jobs die on that step (everything else, including MSRV and supply-chain audit, is green). Running cargo fmt --all and re-pushing should make the PR fully green; it's otherwise MERGEABLE against main.

Went through the macro expansions against the original hand-written bodies:

  • forward_singleton_deserialize! / forward_visit_scalars! (src/with.rs) — method lists are complete and expansions match the originals exactly.
  • mapping_projection_iter! (src/ast.rs) — all 7 projection-iterator invocations (including the multi-line IterMut closure) preserve the original next/size_hint/len/double-ended behavior.
  • src/event_de/source.rsscalar_key_at / scalar_key_at_in are confirmed unused on this branch (scalar_key_at was only called by scalar_key_at_in); removal is safe.

One observation worth a line in the PR description: for NestedValueSerializer, the numeric methods previously delegated through ValueSerializer.serialize_*(value) and now inline Value::from(value) directly via the macro. The result is identical (the delegate was itself just Value::from), but it's the one place the refactor changes the shape of the code rather than purely folding duplicates, so calling it out keeps the "no behavior change" claim precise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant