Skip to content

Add FromByteSlice: fallible variable-length integer parse (0.1.2) - #13

Merged
kaidokert merged 4 commits into
mainfrom
proposal/byte-trait-ecosystem
Jul 6, 2026
Merged

kaidokert merged 4 commits into
mainfrom
proposal/byte-trait-ecosystem

Conversation

@kaidokert

@kaidokert kaidokert commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Opens the 0.1.2-alpha line and lands capability (b) from the byte-trait ecosystem plan: a fallible variable-length integer parse.

Why

FromBytes mirrors core's from_le_bytes — fixed width, infallible — so it can't parse a &[u8] whose length the caller doesn't match to the target width, and has no channel to reject over-length input. FromByteSlice fills that gap:

pub trait FromByteSlice: Sized {
    fn from_be_slice(bytes: &[u8]) -> Result<Self, ByteSliceError>;
    fn from_le_slice(bytes: &[u8]) -> Result<Self, ByteSliceError>;
}

Shorter-than-width input zero-extends; equal is exact; wider → ByteSliceError { Overflow } (never truncates); empty → ByteSliceError { Empty }.

Properties

  • Additive — no existing item changes signature. Patch-slot alpha.
  • Unsigned-only (u8–u128, usize): zero-extension has no sign-extension analogue.
  • Crate-owned error (ByteSliceError + ByteSliceErrorKind), mirroring AsciiParseError — constructible on stable, no std type.
  • Const-callable on nightly (canary in const_nightly.rs).
  • Root + prelude re-exports.

Design feedback (from the earlier proposal doc)

  • Empty → error, not Ok(0) — adopted. A truncated/missing buffer can't masquerade as a valid zero; matches FromAscii.
  • from_ne_slice — declined. Native endianness is for fixed-width reinterpretation (from_ne_bytes on [u8; N]); a variable-length parse in "native" order is semantically dubious and invites misuse. Additive if ever wanted.
  • "Fits" semantics — length-based (matches the live rsa/new_reduce check): len > width → Overflow regardless of whether surplus high bytes are zero.

Not included

The backend-side changes (fixed-bigint FromBytes::Bytes = [u8] flip, &Backend ToBytes impl, const-eval inherent) and the ed25519/rsa rebindings land in their own repos. This PR is only the additive const-num-traits primitive.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The crate version was bumped, and a new byte_slice parsing API was added, re-exported from the crate root, and exercised in nightly const tests.

Changes

Byte slice parsing API

Layer / File(s) Summary
Version bump
Cargo.toml
Package version updated from 0.1.1 to 0.1.2-alpha.0.
Module exports
src/ops/mod.rs, src/lib.rs
The new byte_slice module is included in the module tree, and its error types and trait are re-exported through the crate root and prelude.
Error and trait contract
src/ops/byte_slice.rs
Defines ByteSliceErrorKind, ByteSliceError, and the FromByteSlice const trait for big-endian and little-endian parsing from &[u8].
Implementations and tests
src/ops/byte_slice.rs, tests/const_nightly.rs
Implements FromByteSlice for unsigned integer types with length-based padding and errors, and adds a const test using from_be_slice.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding the fallible variable-length FromByteSlice parser.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch proposal/byte-trait-ecosystem

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request bumps the crate version to 0.1.2-alpha.0 and adds a comprehensive proposal document detailing a byte-trait ecosystem for backend-agnostic numeric consumers. The review feedback suggests correcting inconsistent method names in the capability table, adding a native-endian parsing method (from_ne_slice) for completeness, and treating empty slice inputs as errors rather than defaulting to zero to prevent potential bugs.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread notes/BYTE_TRAIT_ECOSYSTEM.md Outdated
Comment thread notes/BYTE_TRAIT_ECOSYSTEM.md Outdated
Comment thread notes/BYTE_TRAIT_ECOSYSTEM.md Outdated
Additive alpha cycle for the upcoming byte-trait work (a fallible
variable-length FromByteSlice parse). Patch slot, not minor: this crate's
change is purely additive; the only breaking change in the broader plan lives
in the backend (fixed-bigint), not here.
@kaidokert
kaidokert force-pushed the proposal/byte-trait-ecosystem branch from 854ecb9 to c7c6138 Compare July 6, 2026 01:08
@kaidokert kaidokert changed the title Open 0.1.2-alpha: byte-trait ecosystem proposal Open 0.1.2-alpha pre-release line Jul 6, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The design note uses c0nst::c0nst! and similar pseudo-identifiers; consider adding a short note up front that this is illustrative syntax and mapping it explicitly to the real macro/paths to avoid confusion for readers trying to correlate with the actual code.
  • The Open decisions section is very helpful; you might want to turn these into explicit, numbered action items (with tentative resolutions or acceptance criteria) so it’s easier to track what must be settled before landing the FromByteSlice/ByteSliceError implementation.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The design note uses `c0nst::c0nst!` and similar pseudo-identifiers; consider adding a short note up front that this is illustrative syntax and mapping it explicitly to the real macro/paths to avoid confusion for readers trying to correlate with the actual code.
- The `Open decisions` section is very helpful; you might want to turn these into explicit, numbered action items (with tentative resolutions or acceptance criteria) so it’s easier to track what must be settled before landing the `FromByteSlice`/`ByteSliceError` implementation.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Capability (b) from the byte-trait ecosystem plan. FromBytes mirrors core's
from_le_bytes — fixed width, infallible — and so can't parse a byte slice
whose length the caller doesn't match to the target, nor reject over-length
input. FromByteSlice fills that: from_be_slice / from_le_slice take a &[u8] of
arbitrary length, zero-extend shorter input, and return a crate-owned
ByteSliceError on over-width (never truncating) or empty input.

Unsigned-only (zero-extension has no sign-extend analogue); crate-owned error
mirrors AsciiParseError so it stays constructible on stable; const-callable on
nightly (canary in const_nightly.rs). Additive — no existing item changes.

Empty rejected rather than read as 0, so a truncated buffer can't pass as a
valid zero (matches FromAscii). No from_ne_slice: native endianness is for
fixed-width reinterpretation, not a variable-length parse.
@kaidokert kaidokert changed the title Open 0.1.2-alpha pre-release line Add FromByteSlice (0.1.2-alpha) Jul 6, 2026
kaidokert added 2 commits July 5, 2026 18:41
The module header and ByteSliceError doc justified themselves by analogy to
FromBytes / AsciiParseError. A reader doesn't care which trait it was modeled
on, and the reference rots if the sibling changes. State the contract and the
reason (fallible because an over-long slice has no lossless read; crate-owned
because std's parse error is opaque) on their own terms.
A downstream `= "0.1.2"` requirement doesn't match a `0.1.2-alpha.0`
pre-release without exact opt-in, which makes prototyping the byte-trait
integration awkward. Carry the plain version and mark the alpha/prototype
status with the git tag (v0.1.2-alpha.0) instead.
@kaidokert kaidokert changed the title Add FromByteSlice (0.1.2-alpha) Add FromByteSlice: fallible variable-length integer parse (0.1.2) Jul 6, 2026
@kaidokert
kaidokert merged commit ca16b36 into main Jul 6, 2026
9 checks passed
@kaidokert
kaidokert deleted the proposal/byte-trait-ecosystem branch July 11, 2026 05:22
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