Packed Buffers

A ByteBuffer stores bytes. Its numeric accessors read one lane at a byte offset, which is what wire formats and C calls need. When you want a sequence of same-width numbers, wrap the buffer in a view from @std/bytes:

  • UInt8View stores one byte per element.
  • UInt16View stores two bytes per element, little-endian.
  • UInt32View stores four bytes per element, little-endian.
  • UInt64View stores eight bytes per element, little-endian.
  • Float32View stores four bytes per element, little-endian.
  • Float64View stores eight bytes per element, little-endian.

The view holds the buffer, so the buffer stays alive while the view is in use. wrap(buffer) makes a view; fromHandle(handle) is used by generated Rust wrappers and is not needed in ordinary code.

.rnx
import { ByteBuffer, UInt8View, Float64View } from "@std/bytes";

let bytes = UInt8View.wrap(ByteBuffer.fromArray([10, 20, 30]));
println!(bytes.length(), bytes.get(1));
bytes.set(2, 40);

let doubles = Float64View.wrap(ByteBuffer.allocate(16));
println!(doubles.length());
doubles.set(1, 1.5);
println!(doubles.get(1));

length() is the byte length divided by the element width. A byte length that is not a multiple of the width leaves trailing bytes unused. byteLength() reports the backing byte count. The view does not resize the buffer.

Bounds

Index i maps to byte offset i * width. A read or write must fit entirely inside the buffer. A negative index, or an index at or past length(), stops the program with byte buffer out of bounds. There is no clamping and no error value to check. The same rule applies to every view width.

.rnx
let bytes = UInt16View.wrap(ByteBuffer.allocate(4));
println!(bytes.length());
println!(bytes.get(2));

The last line stops the program: the view holds two elements at offsets 0 and 2, and index 2 would start at byte 4.

Integer views hold bit patterns, not signed values. UInt32View.get takes the four stored bytes and widens them into Int. A value above the signed maximum reads back as a negative Int. Use as UInt when you need unsigned printing. Float32View narrows a Float on write and widens it on read, so 0.1 comes back as 0.10000000149011612.

Locals, fields, and generic values still occupy one 8-byte slot. The packed tier changes how a buffer is interpreted, not how values are tagged or passed between functions. Object fields are not split into separate arrays by width.

Rust slices and vectors

Generated wrappers for rust: dependencies use these views at the rnx surface. A Rust &[T] or Vec<T> parameter becomes a view parameter, and a Vec<T> result becomes a view result. The wrapper reads the buffer handle and calls into Rust with a slice built over the same bytes.

.rnx
import { ByteBuffer, UInt8View } from "@std/bytes";
import { sumBytes } from "probe";

let data = UInt8View.wrap(ByteBuffer.fromArray([1, 2, 3, 4]));
println!(sumBytes(data));

The mapping is by width. i8 and u8 use UInt8View, i16 and u16 use UInt16View, and so on through UInt64View. f32 uses Float32View and f64 uses Float64View. On the supported 64-bit targets, isize and usize use UInt64View. The Rust signature decides whether an integer lane is signed; the view only decides how many bytes it occupies.

Copies follow ownership:

  • A &[T] argument borrows the buffer. No bytes are copied.
  • A Vec<T> argument is copied into an owned Rust vector. The buffer belongs to rnx, and Rust must not free rnx memory using T's layout.
  • A Vec<T> result is copied into a fresh rnx ByteBuffer. A Rust Vec<T> cannot be handed over as rnx's byte vector because the element layouts differ.

Wrappers with a view parameter or result use plain fn, not export fn, since views are not C-ABI types. Scalar, Bool, Option, monomorphized, and nested module imports keep the API they had before this tier.