Skip to main content

Module convert

Module convert 

Source
Expand description

Marshalling helpers that bridge Rust values and the C ABI’s (ptr, len) slots.

These functions are the audited home of the pointer operations a WeaveFFI producer performs on strings, bytes, and value buffers. The #[weaveffi::module] expansion wires the generated thunks to them:

  • lift functions read a borrowed (ptr, len) parameter. They never take ownership of caller memory; the borrowing variants (lift_str, lift_byte_slice) don’t even copy.
  • lower functions hand an owned value to the consumer as a producer-allocated (ptr, len) run, which the consumer releases with {prefix}_free_bytes (see free_bytes).
  • alloc is the body of {prefix}_alloc: a run the consumer fills and either frees with {prefix}_free_bytes or hands to the producer (a callback’s string, bytes, or buffer return), which adopts it with adopt_bytes. Every run is a Box<[u8]> allocation, so one release function covers both directions.

Strings, bytes, and serialized value buffers all share this one representation: a string is its UTF-8 bytes, never NUL-terminated, so an interior NUL round-trips intact. A null pointer is valid only with a length of 0 and denotes the empty run.

Functions§

adopt_bytes⚠
Adopt a run from alloc (or one this runtime returned) as an owned vector. Null with a length of 0 is the empty run; null with any other length yields None.
alloc
The body of {prefix}_alloc: allocate a zero-filled run of len bytes the consumer fills, released with free_bytes (the same len) or handed to the producer, which adopts it. A len of 0 returns null, which is the empty run.
bytes_into_raw
Turn an owned byte vector into the producer-allocated (ptr, len) run a return slot or async result carries, to be released with free_bytes. An empty vector yields (null, 0) and allocates nothing.
free_bytes⚠
The body of {prefix}_free_bytes: release a run returned by this runtime (a string, bytes, or a value buffer) or allocated with alloc. A null ptr or a len of 0 is a no-op.
lift_byte_slice⚠
Borrow a (ptr, len) parameter as a byte slice for a caller-chosen lifetime, without copying.
lift_bytes⚠
Copy a (ptr, len) parameter into an owned Vec<u8>.
lift_str⚠
Borrow a (ptr, len) string parameter as a &str, validating UTF-8, without copying.
lift_string⚠
Copy a (ptr, len) string parameter into an owned String, validating UTF-8.
lower_bytes⚠
Lower an owned byte vector into a return value plus the trailing size_t* out_len slot. See bytes_into_raw.
lower_string⚠
Lower an owned string as its UTF-8 bytes, exactly like lower_bytes.