Skip to content

feat(serde): add array and optional byte adapters - #33

Open
patrickZWY wants to merge 1 commit into
fast:mainfrom
patrickZWY:feat/serde-array-helper
Open

patrickZWY wants to merge 1 commit into
fast:mainfrom
patrickZWY:feat/serde-array-helper

Conversation

@patrickZWY

@patrickZWY patrickZWY commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixed-size byte arrays currently need separate serialize_with and deserialize_with attributes, and optional byte fields need custom glue. Add three zero-sized adapters that support a single Serde with attribute:

  • Array encodes and decodes fixed-size byte arrays, enforcing the declared byte length.
  • OptionalArray handles optional fixed-size arrays, including without alloc.
  • OptionalVec handles optional byte vectors when alloc is enabled.

Addresses the Serde portion of #15. The stream reset discussion remains open in that issue.

Design Notes

  • Keep representation separate from encoding policy. Each adapter takes an existing Codec profile and reuses its encoding and decoding paths through private helpers. Existing profile Serde methods remain available; no dependencies are added.
  • Preserve Option semantics. Present values use encoded strings; absent values use Serde's None representation. In JSON, null remains distinct from an empty string: the latter represents a present empty vector or a zero-length array. Invalid encoding and incorrect array lengths are rejected.
  • Keep field-presence behavior explicit. Use Serde's default attribute to accept missing optional fields, and skip_serializing_if to omit absent values. Array adapters require only the serde feature; format-specific allocation remains the serializer or deserializer's responsibility.
  • Downstream import compatibility. The adapters use the bases::serde path proposed in Evaluate fixed-array serde helper ergonomics and stream reset #15. With the serde feature enabled, downstream code that combines use bases::* with use serde::Serialize can fail to compile because serde may refer to either crate's namespace. Import the needed items from bases explicitly or use ::serde::Serialize to select the external Serde crate.

Validation

  • cargo x test: passed the workspace tests, doctests, serde configurations with and without alloc, and release integration tests. Shared contracts compare wire output against an independent codec across all 24 profiles; focused tests cover empty values, malformed input, exact array lengths, null, missing fields, and omission.
  • cargo x check: passed all six feature configurations with warnings denied.
  • cargo x lint: passed Clippy, formatting, TOML formatting, spelling, license checks, and rustdoc with warnings denied.

Co-authored-by: Codex <noreply@openai.com>
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