Skip to content

Use unsafe Rust for catalyst#161

Merged
yanganto merged 5 commits into
mainfrom
unsafe
Jul 12, 2026
Merged

Use unsafe Rust for catalyst#161
yanganto merged 5 commits into
mainfrom
unsafe

Conversation

@yanganto

@yanganto yanganto commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Add unsafe feature for zero-memory-move catalyst operations

This PR introduces an optional unsafe feature that uses ManuallyDrop + ptr::read / MaybeUninit + ptr::write to avoid memory moves in the generated catalyst and substrate code.

Changes

derive/src/catalyst.rs

  • Complex::decouple — gated into two implementations:
    • unsafe: wraps self in ManuallyDrop, reads fields via ptr::read to construct the catalyst and substrate without moving
    • safe: original destructuring approach
  • Catalyst::bind — gated into two implementations:
    • unsafe: wraps self in ManuallyDrop, reads catalyst fields via ptr::read (substrate still uses __substrate_unpack() since those fields are private cross-crate)
    • safe: original destructuring approach
  • to_ptr_read_stream / to_ptr_read_arg_stream parameterized with a source Ident

derive/src/substrate.rs

  • __substrate_new — gated into two implementations:
    • unsafe: uses MaybeUninit + ptr::write + addr_of_mut to construct the struct field-by-field
    • safe: original struct literal construction
  • __substrate_unpack — gated into two implementations:
    • unsafe: uses ManuallyDrop + ptr::read to extract fields without running Drop
    • safe: original tuple destructuring

Feature forwarding

  • derive/Cargo.toml: added unsafe = [] feature
  • lib/Cargo.toml: unsafe forwards to struct-patch-derive/unsafe
  • complex-example/catalyst/Cargo.toml: unsafe forwards to struct-patch/unsafe, enabling cargo test -p catalyst --features unsafe for easy testing

Other

  • flake.nix: check-catalyst now also runs cargo test -p catalyst --features unsafe
  • README.md: documented the unsafe feature; replaced the "future" note with the actual implementation
  • Bumped version to 0.12.7

Testing

All 9 tests pass both with and without the unsafe feature:

  • cargo test -p substrate (3 tests)
  • cargo test -p catalyst (3 tests, safe path)
  • cargo test -p catalyst --features unsafe (3 tests, unsafe path)

yanganto added 3 commits July 12, 2026 10:35
This follows the existing conventions. (e.g.
`catalyst: add exclude_field_attributes`, `catalyst: handle private fields`).
The change adds an `unsafe` feature gate that switches `Complex::decouple`
between safe destructuring and `ManuallyDrop` + `ptr::read`, with the feature
forwarded from `struct-patch` and the complex-example crate.
@yanganto yanganto marked this pull request as ready for review July 12, 2026 02:43
@yanganto yanganto merged commit 772543c into main Jul 12, 2026
3 checks passed
@yanganto yanganto deleted the unsafe branch July 12, 2026 11:34
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