Atom

Type descriptors and packed data

Choose a fixed-width buffer

Packed arrays hold their elements next to each other in memory. Use one for a binary buffer or when native code expects a particular element size.

Atom
bytes = create(u8, 3)
bytes[0] = 42
print(type(bytes[0]) == u8)
print(bytes[0])
Output
true
42

Each element has an unsigned eight-bit representation. The array still has three elements.

Give each record two fields

A packed layout is a recipe for how the fields of a record are stored. Reuse it for an array of points.

Atom
use binary
point = binary.layout({x = f32, y = f32})
points = create(point, 2)
points[0].x = 4
print(points[0].x)
Output
4

The first record's x field is updated in the array's storage.

Read and request a type

A type descriptor names a runtime representation. Use type to inspect a value and convert to request another representation.

Atom
wide = 42
narrow = convert(u8, wide)
print(type(wide) == i64)
print(type(narrow) == u8)
Output
true
true

The original integer is i64. The conversion creates a value represented as u8.

Type descriptors

Use 'type(value)' to inspect a runtime value and 'convert' to request a checked conversion. Descriptors also tell 'create' which representation to use.

Packed arrays and binary buffers

Use 'create(u8, length)' for a contiguous byte buffer. Change the element descriptor for another fixed-width representation.

Layouts and packed records

A layout describes the fields of a record. Use 'binary.layout' to reuse a layout or declare a nested record.

Alignment and memory views

A packed record uses the target C layout unless you specify a packing cap. Packed element and slice views share storage. Use 'copy' to get independent storage.

The numeric descriptors are i8, i16, i32, i64, u8, u16, u32, u64, f32 and f64. Other descriptors include bool, char, string, array, dict, table, function, range, packed, module, ptr, cstring and void. Type returns a descriptor. Convert applies the selected representation, with checked bounds for narrowing and no fractional loss on an integer conversion. Arithmetic returns results at the default width of the promoted kind.

Create accepts a packed scalar descriptor and an element count, or a dictionary layout and an optional count. A dictionary defines fields in insertion order. Layout dictionaries can contain nested layouts and scalar descriptors. Layouts use the host C ABI's scalar alignment and trailing padding. Each layout is frozen when storage is created.

Use binary.layout(fields[, packing]) to reuse a frozen record descriptor and optionally cap field alignment. The cap is a power of two. binary.fixed(element, count) describes an inline, fixed-length array and can appear as a record field or an element of another layout. Its count is positive and its storage has no per-element object overhead. Fixed arrays allow element updates and reject resizing.

An owning packed array can grow. A record selected from it is a view of the selected element. Nested record access and packed slicing also create views. A view keeps the allocation alive and follows buffer relocation during growth. Removal invalidates existing views. Native pointers obtained through ffi.address pin the buffer until the pointer is released. A borrowed C pointer from atom_packed_bytes is valid until the host performs an operation that relocates or invalidates storage.

The complete descriptor table lists storage widths, resource tags and conversion options. type(value) returns a built-in descriptor, while a frozen layout produced by binary.layout or binary.fixed describes a particular structure. These are first-class values. Atom remains dynamically typed. For example, a packed f32 slot always stores f32, while the variable holding the record can later hold a string.

Integer conversion accepts numeric values and base-10 numeric text. Integer targets reject fractions and out-of-range values. Floating conversion can round integers and f64 values to the target precision. Casting finite values to f32 checks the maximum finite magnitude. Infinity and NaN can come from runtime arithmetic or the math module. Conversion from text to floating point requires a finite parse. Character conversion accepts a signed integer scalar value or one scalar of text. convert(bool, value) applies truthiness. convert(string, value) uses Atom's display formatting. For other built-in descriptors, conversion is supported when the input already has the destination descriptor.

Packable scalar fields use the corresponding native C integer/float width and alignment, one byte for bool and a 32-bit scalar for char. Record fields are laid out in declaration order. Each starts at the next aligned offset, and the record size is rounded up to its maximum alignment. The resulting size is the stride of records in an owning array. A packing cap limits field alignment for its record level. Nested fields retain their defined internal layouts. Empty and cyclic layouts raise errors. Field names are text. Layouts can only contain packable scalar descriptors, record layouts or fixed arrays.

A packed scalar creation with no count gives a zero scalar. A packed array count is the initial length and is zero-initialized. A record created without a count is one record with all byte fields initialized to zero. Set fields by assignment. Assigning a compound field or array element requires matching packed layout bytes, including the correct fixed-array length. Text field assignment can convert only into supported scalar types. Indexed access checks bounds. Plain dictionary field initialization does not implicitly create packed subrecords.

Element and nested views share and keep the owning storage alive. Taking a view does not pin the memory. Adding elements can relocate the buffer. Existing views follow relocation. Removal shifts storage and invalidates old views. Direct native borrowing is limited to a synchronous call, or to the documented lifetime of the host C pointer. An explicit FFI pin blocks removal and growth that would move storage while pinned. copy(packed) creates independent storage. When serializing portable file formats, choose explicit endianness and field order rather than depending on host padding.

Use a nested layout when a field contains several values that belong together. The rectangle below stores its origin as a pair of coordinates.

Atom
use binary

vec2 = binary.layout({x = f32, y = f32})
rect = binary.layout({origin = vec2, width = f32, height = f32})
area = create(rect)
area.origin.x = 10.5
print(area.origin.x)
Output
10.5

Use an array of records when each item needs the same fields. This pixel layout stores four unsigned bytes per pixel. Assigning through pixel also updates pixels[3] because both refer to the same storage.

Atom
use binary

color = binary.layout({r = u8, g = u8, b = u8, a = u8})
pixels = create(color, 8 * 8)
pixel = pixels[3]
pixel.a = 255
print(binary.stride(color))
print(pixels[3].a)
Output
4
255

Use a flat packed array when each entry holds a single value. Multiply the row by the number of columns, then add the column to get an index.

Atom
tile_columns = 12
tiles = create(u16, tile_columns * 8)
tiles[2 * tile_columns + 4] = 7
print(tiles[28])
Output
7

Use a fixed array as a field when the item count is part of the layout. Each frame here has exactly two signed 16-bit samples.

Atom
use binary

frame = binary.layout({channels = binary.fixed(i16, 2)})
audio = create(frame, 480)
audio[0].channels[0] = 1200
audio[0].channels[1] = -1200
print(audio[0].channels[1])
Output
-1200