Skip to content

feat: support parser rounding modes - #24

Open
tisonkun wants to merge 8 commits into
mainfrom
codex/define-parse-rounding
Open

feat: support parser rounding modes#24
tisonkun wants to merge 8 commits into
mainfrom
codex/define-parse-rounding

Conversation

@tisonkun

@tisonkun tisonkun commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a public RoundMode with five non-redundant modes: Ceil, Floor, HalfCeil, HalfFloor, and HalfEven.
  • Add a non-exhaustive ParseOptions with a public round_mode field and HalfCeil as its default.
  • Add ByteSize::parse_with(input, options); standard and Serde string parsing preserve the existing ties-to-larger behavior.
  • Apply the unit multiplier first, then round exactly once to a whole byte without floating point.
  • Keep the default parser free from runtime rounding state, track later discarded digits only for modes that need exact-tie detection, and scale long decimal fractions without per-digit division.
  • Cover every mode with boundary, unit, overflow, and exact-arithmetic property tests; add isolated benchmarks for FromStr and parse_with.
  • Document the contract in the API overview, README, and changelog.
  • Verify with cargo x test, cargo x lint, and cargo x bench.

Design Notes

The rounding semantics are adapted from Jiff’s RoundMode, but the API does not copy all nine variants. ByteSize only accepts non-negative values, making Expand equivalent to Ceil, Trunc equivalent to Floor, HalfExpand equivalent to HalfCeil, and HalfTrunc equivalent to HalfFloor. The public enum keeps one direction-based name for each distinct behavior relevant to this domain.

The parsing API follows the cronexpr ParseOptions pattern: configurable behavior lives in a defaultable, non-exhaustive options type. FromStr uses the same HalfCeil contract through a zero-sized internal strategy, while parse_with uses the selected runtime mode. This preserves one parsing algorithm without carrying runtime mode state through the default integer scan.

Controlled same-machine A/B runs used matching benchmark binaries and at least one second per case. Against main, every existing FromStr case was at parity or faster: high-precision decimal parsing improved by about 17%, high-precision binary by about 1%, and the remaining cases by about 1–11%. Against the pre-optimization PR head, all five parse_with modes improved by about 8–14%.

@tisonkun tisonkun changed the title docs: define parser rounding contract feat: support parser rounding modes Jul 31, 2026
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