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

Add serialization helpers to improve api ergonomics #34

Merged
merged 3 commits into from
May 10, 2021

Conversation

jstarry
Copy link
Contributor

@jstarry jstarry commented May 7, 2021

Fixes: #31

Problem

BorshSerialize::serialize method signature is not very ergonomic when using mutable slices. This is because a &mut [T] already implements Write but the method expects a &mut W where W itself implements Write. This means that in order to pass a mutable slice, you need to pass a mutable reference to the mutable slice: &mut &mut [T].

Changes

  • Added top-level serialize_to_writer and serialize_to_vec helper functions

@jstarry jstarry requested review from ailisp and frol as code owners May 7, 2021 01:52
Copy link
Contributor

@matklad matklad left a comment

Choose a reason for hiding this comment

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

needs a test & different argument order, but otherwise LGTM.

I'd probably use to_vec, to_writer names (short & follows serde_json), but I am ok with these names as well.

borsh/src/ser/helpers.rs Outdated Show resolved Hide resolved
borsh/src/ser/helpers.rs Outdated Show resolved Hide resolved
borsh/src/ser/helpers.rs Outdated Show resolved Hide resolved
Copy link
Member

@ailisp ailisp left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thank you!

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.

BorshSerialize::serialize method is unergonomic for mutable slices
3 participants