Skip to content

consensus_encoding: Refactor BytesEncoder, ScriptEncoder, SliceEncoder & ArrayEncoder#5104

Closed
jrakibi wants to merge 3 commits intorust-bitcoin:masterfrom
jrakibi:remove-with-without-lenght-prefix
Closed

consensus_encoding: Refactor BytesEncoder, ScriptEncoder, SliceEncoder & ArrayEncoder#5104
jrakibi wants to merge 3 commits intorust-bitcoin:masterfrom
jrakibi:remove-with-without-lenght-prefix

Conversation

@jrakibi
Copy link
Copy Markdown
Contributor

@jrakibi jrakibi commented Oct 7, 2025

Refactor the encoder API by removing with_length_prefix/without_length_prefix and replacing it with a single new() constructor across all encoders.

Length prefixing is now handled through composition with CompactSizeEncoder

This is the first step in simplifying the encoder API to use composition
with CompactSizeEncoder.

We remove with_length_prefix() and without_length_prefix() methods and
replace with single new() constructor.

We also pdate ScriptEncoder to use BytesEncoder::new() with composition
ArrayEncoder only has `without_length_prefix()`, so we
can use the `new()` constructor directly to keep consistency
with other encoders
remove `with_length_prefix()` method from SliceEncoder. and add `new()`
constructor.
If length prefix is needed, we can use composition with `CompactSizeEncoder`

This completes the refactor to use composition-based length prefixing.
@github-actions github-actions bot added C-units PRs modifying the units crate C-consensus_encoding PRs modifying the consensus-encoding crate C-io PRs modifying the io crate test C-primitives labels Oct 7, 2025
@jrakibi jrakibi closed this Oct 7, 2025
@tcharding
Copy link
Copy Markdown
Member

Oh were we dancing on each others toes?

@jrakibi
Copy link
Copy Markdown
Contributor Author

jrakibi commented Oct 7, 2025

Oh were we dancing on each others toes?

Haha, I think so
But we can still use this PR to show the approach I was describing in the issue

@apoelstra
Copy link
Copy Markdown
Member

I kinda wish we'd kept this one, which is fairly complete. Other than the name new which I think is misleading this looks good to me.

@tcharding
Copy link
Copy Markdown
Member

I went back over this with fresh eyes after reading your comment and I now agree, reverting my previous belief. The usage of SliceEncoder, while a bit more verbose, is much more clear to read. The WitnessEncoder is a no brainer, the change is internal to encoders and clearly better.

@jrakibi want to push these two changes up again? Sorry for my original hesitation on this.

apoelstra added a commit that referenced this pull request Oct 10, 2025
63bbe12 Remove length prefix from SliceEncoder (Tobin C. Harding)

Pull request description:

  Currently we hide the length prefix inside the `SliceEncoder`. While technically correct the usage of the `SliceEncoder` can be made more clear by forcing users to combine a compact size encoder and a slice encoder using an `Encoder2` instead of the current abstraction.
  
  Original idea from jrakibi in #5104. I gave you a co-developed-by tag mate. Pushing this up because I'm itching to get `primitives 1.0.0-rc.0` out.


ACKs for top commit:
  apoelstra:
    ACK 63bbe12; successfully ran local tests


Tree-SHA512: befe9b25a35644195834fdf0fec137d9250278416277409828cca2cd888ec69d4d29309f4fc761fe95790046131e58d743a724075c6b1092e86a564627bc3f67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-consensus_encoding PRs modifying the consensus-encoding crate C-io PRs modifying the io crate C-primitives C-units PRs modifying the units crate test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants