Skip to content

feat: add std::io::Write impl for BufferMut#62

Merged
iduartgomez merged 4 commits intomainfrom
feat/improve-state-copying
Mar 23, 2026
Merged

feat: add std::io::Write impl for BufferMut#62
iduartgomez merged 4 commits intomainfrom
feat/improve-state-copying

Conversation

@iduartgomez
Copy link
Copy Markdown
Contributor

Summary

  • Adds impl std::io::Write for BufferMut in memory/buf.rs
  • Enables bincode::serialize_into to write directly into WASM linear memory buffers, eliminating the intermediate Vec<u8> allocation previously needed when serializing data across the FFI boundary

Context

Part of the state copying improvement initiative. The host currently serializes RelatedContracts and UpdateData to a temp Vec<u8>, then copies that Vec into WASM memory — a double copy. With this Write impl, freenet-core can use bincode::serialize_into(&mut buf) to write directly into the WASM buffer.

Test plan

  • cargo test passes
  • cargo clippy --all-targets clean
  • freenet-core integration verified with path dependency

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.
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.
@iduartgomez iduartgomez enabled auto-merge (squash) March 23, 2026 16:44
@iduartgomez iduartgomez disabled auto-merge March 23, 2026 16:44
@iduartgomez iduartgomez merged commit a70d85c into main Mar 23, 2026
8 checks passed
@iduartgomez iduartgomez deleted the feat/improve-state-copying branch March 23, 2026 16:44
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