Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(experimental): add padLeftCodec and padRightCodec helpers to @solana/codecs-core #2314

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

lorisleiva
Copy link
Collaborator

@lorisleiva lorisleiva commented Mar 13, 2024

This PR adds new padLeftCodec and padRightCodec helper functions. See copy/pasted documentation below:


Padding codecs

The padLeftCodec and padRightCodec helpers can be used to add padding to the left or right of a given codec. They accept an offset number that tells us how big the padding should be.

const getLeftPaddedCodec = () => padLeftCodec(getU16Codec(), 4);
getLeftPaddedCodec().encode(0xffff);
// 0x00000000ffff
//   |       └-- Our encoded u16 number.
//   └-- Our 4-byte padding.

const getRightPaddedCodec = () => padRightCodec(getU16Codec(), 4);
getRightPaddedCodec().encode(0xffff);
// 0xffff00000000
//   |   └-- Our 4-byte padding.
//   └-- Our encoded u16 number.

Note that both the padLeftCodec and padRightCodec functions are simple wrappers around the offsetCodec and resizeCodec functions. For more complex padding strategies, you may want to use the offsetCodec and resizeCodec functions directly instead.

As usual, encoder-only and decoder-only helpers are available for these padding functions. Namely, padLeftEncoder, padRightEncoder, padLeftDecoder and padRightDecoder.

const getMyPaddedEncoder = () => padLeftEncoder(getU16Encoder());
const getMyPaddedDecoder = () => padLeftDecoder(getU16Decoder());
const getMyPaddedCodec = () => combineCodec(getMyPaddedEncoder(), getMyPaddedDecoder());

Copy link

changeset-bot bot commented Mar 13, 2024

⚠️ No Changeset found

Latest commit: f783650

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Collaborator

@buffalojoec buffalojoec left a comment

Choose a reason for hiding this comment

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

Nice! Good API sugar here.

sugar

Copy link
Collaborator Author

lorisleiva commented Mar 14, 2024

Merge activity

  • Mar 14, 6:08 AM EDT: @lorisleiva started a stack merge that includes this pull request via Graphite.
  • Mar 14, 6:11 AM EDT: Graphite rebased this pull request as part of a merge.
  • Mar 14, 6:12 AM EDT: @lorisleiva merged this pull request with Graphite.

Base automatically changed from loris/offset-codec to master March 14, 2024 10:10
@lorisleiva lorisleiva merged commit f9509c7 into master Mar 14, 2024
7 checks passed
@lorisleiva lorisleiva deleted the loris/pad-codec branch March 14, 2024 10:12
Copy link
Contributor

🎉 This PR is included in version 1.91.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants