feat: add std::io::Write impl for BufferMut#62
Merged
iduartgomez merged 4 commits intomainfrom Mar 23, 2026
Merged
Conversation
Enables bincode::serialize_into to write directly into WASM linear memory buffers, eliminating the intermediate Vec<u8> allocation that was previously needed when serializing data across the FFI boundary.
iduartgomez
added a commit
to freenet/freenet-core
that referenced
this pull request
Mar 23, 2026
- Remove 3 unnecessary WrappedState .clone() calls in contract.rs; write() only needs AsRef<[u8]>, no ownership transfer required - Add init_buf_with_capacity() to allocate WASM buffers by size without needing the data upfront - Use bincode::serialize_into to write RelatedContracts and UpdateData directly into WASM memory, eliminating intermediate Vec allocations - Fix pre-existing clippy warnings in test code (wildcard_enum_match_arm, assertions_on_constants) Depends on freenet/freenet-stdlib#62 for the std::io::Write impl on BufferMut.
4 tasks
Tests cover basic write, exact fill, partial write when near full, error on full buffer, empty slice, write_all, write_all overflow, and bincode::serialize_into integration. Uses host-memory buffers (no WASM runtime needed).
The from_raw_parts call in from_raw_builder's trace block panics when start_ptr is null (even with size=0) on newer Rust versions. Skip the trace logging when the linear memory pointer is null, which happens in host-memory buffer tests.
Includes std::io::Write impl for BufferMut, enabling direct serialization into WASM linear memory buffers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
impl std::io::Write for BufferMutinmemory/buf.rsbincode::serialize_intoto write directly into WASM linear memory buffers, eliminating the intermediateVec<u8>allocation previously needed when serializing data across the FFI boundaryContext
Part of the state copying improvement initiative. The host currently serializes
RelatedContractsandUpdateDatato a tempVec<u8>, then copies that Vec into WASM memory — a double copy. With thisWriteimpl, freenet-core can usebincode::serialize_into(&mut buf)to write directly into the WASM buffer.Test plan
cargo testpassescargo clippy --all-targetsclean