Skip to content

bytes

import bytes
FunctionSignatureDescription
len(Bytes) -> IntReturn the number of bytes.
get(Bytes, Int) -> Option[Int]Get a byte value (0-255) at an index.
get_or(Bytes, Int, Int) -> IntGet a byte value at an index, or a default if out of bounds.
set(Bytes, Int, Int) -> BytesSet a byte value at an index (mutates in place for var bindings).
slice(Bytes, Int, Int) -> BytesExtract a sub-range of bytes [start, end).
from_list(List[Int]) -> BytesCreate Bytes from a list of integers (each masked to 0-255).
to_list(Bytes) -> List[Int]Convert Bytes to a list of integers (0-255).
is_empty(Bytes) -> BoolCheck if byte buffer is empty.
concat(Bytes, Bytes) -> BytesConcatenate two byte buffers.
repeat(Bytes, Int) -> BytesRepeat a byte buffer n times.
new(Int) -> BytesCreate a zero-filled byte buffer of given length.
push(Bytes, Int) -> UnitAppend a single byte (0-255) to the buffer (mutates in place).
clear(Bytes) -> UnitRemove all bytes from the buffer (mutates in place, keeps capacity).
from_string(String) -> BytesConvert a string to its UTF-8 byte representation.

Return the number of bytes.

bytes.len(b) // => 3

bytes.get(b: Bytes, i: Int) -> Option[Int]

Section titled “bytes.get(b: Bytes, i: Int) -> Option[Int]”

Get a byte value (0-255) at an index.

bytes.get(b, 0) // => some(72)

bytes.get_or(b: Bytes, i: Int, default: Int) -> Int

Section titled “bytes.get_or(b: Bytes, i: Int, default: Int) -> Int”

Get a byte value at an index, or a default if out of bounds.

bytes.get_or(b, 0, 0) // => 72

bytes.set(b: Bytes, i: Int, val: Int) -> Bytes

Section titled “bytes.set(b: Bytes, i: Int, val: Int) -> Bytes”

Set a byte value at an index (mutates in place for var bindings).

bytes.set(b, 0, 255)

bytes.slice(b: Bytes, start: Int, end: Int) -> Bytes

Section titled “bytes.slice(b: Bytes, start: Int, end: Int) -> Bytes”

Extract a sub-range of bytes [start, end).

bytes.slice(b, 1, 3)

Create Bytes from a list of integers (each masked to 0-255).

bytes.from_list([72, 105]) // => <bytes>

Convert Bytes to a list of integers (0-255).

bytes.to_list(b) // => [72, 105]

Check if byte buffer is empty.

bytes.is_empty?([]) // => true

Concatenate two byte buffers.

bytes.concat(a, b)

Repeat a byte buffer n times.

bytes.repeat(b, 3)

Create a zero-filled byte buffer of given length.

bytes.new(1024) // => <1024 zero bytes>

Append a single byte (0-255) to the buffer (mutates in place).

bytes.push(buf, 65)

Remove all bytes from the buffer (mutates in place, keeps capacity).

bytes.clear(buf)

Convert a string to its UTF-8 byte representation.

bytes.from_string("Hello") // => <72, 101, 108, 108, 111>

bytes.write_i64_be(b: Bytes, val: Int) -> Unit

Section titled “bytes.write_i64_be(b: Bytes, val: Int) -> Unit”

Append an Int as 8 bytes big-endian.

bytes.write_i64_be(buf, 42)

bytes.write_f64_be(b: Bytes, val: Float) -> Unit

Section titled “bytes.write_f64_be(b: Bytes, val: Float) -> Unit”

Append a Float as 8 bytes big-endian.

bytes.write_f64_be(buf, 3.14)

bytes.write_u32_be(b: Bytes, val: Int) -> Unit

Section titled “bytes.write_u32_be(b: Bytes, val: Int) -> Unit”

Append an Int as 4 bytes big-endian (u32).

bytes.write_u32_be(buf, 256)

bytes.write_u8(b: Bytes, val: Int) -> Unit

Section titled “bytes.write_u8(b: Bytes, val: Int) -> Unit”

Append a single byte.

bytes.write_u8(buf, 0xFF)

bytes.write_string_be(b: Bytes, s: String) -> Unit

Section titled “bytes.write_string_be(b: Bytes, s: String) -> Unit”

Append a length-prefixed UTF-8 string (u32 length + bytes).

bytes.write_string_be(buf, "hello")

bytes.write_bool(b: Bytes, val: Bool) -> Unit

Section titled “bytes.write_bool(b: Bytes, val: Bool) -> Unit”

Append a boolean as 1 byte (0 or 1).

bytes.write_bool(buf, true)

bytes.read_i64_be(b: Bytes, pos: Int) -> Int

Section titled “bytes.read_i64_be(b: Bytes, pos: Int) -> Int”

Read an Int from 8 bytes big-endian at position.

bytes.read_i64_be(buf, 0) // => 42

bytes.read_f64_be(b: Bytes, pos: Int) -> Float

Section titled “bytes.read_f64_be(b: Bytes, pos: Int) -> Float”

Read a Float from 8 bytes big-endian at position.

bytes.read_f64_be(buf, 0) // => 3.14

bytes.read_u32_be(b: Bytes, pos: Int) -> Int

Section titled “bytes.read_u32_be(b: Bytes, pos: Int) -> Int”

Read an Int from 4 bytes big-endian (u32) at position.

bytes.read_u32_be(buf, 0) // => 256

Read a single byte at position.

bytes.read_u8(buf, 0) // => 255

bytes.read_bool(b: Bytes, pos: Int) -> Bool

Section titled “bytes.read_bool(b: Bytes, pos: Int) -> Bool”

Read a boolean from 1 byte at position.

bytes.read_bool(buf, 0) // => true

bytes.read_string_be(b: Bytes, pos: Int) -> String

Section titled “bytes.read_string_be(b: Bytes, pos: Int) -> String”

Read a length-prefixed UTF-8 string at position.

bytes.read_string_be(buf, 0) // => "hello"

Get raw pointer to buffer data (for C FFI).

bytes.as_ptr(buf)

Get mutable raw pointer to buffer data (for C FFI).

bytes.as_mut_ptr(buf)

bytes.from_raw_ptr(ptr: RawPtr, len: Int) -> Bytes

Section titled “bytes.from_raw_ptr(ptr: RawPtr, len: Int) -> Bytes”

Create Bytes from a raw pointer and length (unsafe, for C FFI bridge callee).

bytes.from_raw_ptr(ptr, 64)

bytes.copy_to_ptr(b: Bytes, ptr: RawPtr, cap: Int) -> Int

Section titled “bytes.copy_to_ptr(b: Bytes, ptr: RawPtr, cap: Int) -> Int”

Copy Bytes to a raw pointer. Returns bytes written.

bytes.copy_to_ptr(buf, ptr, 1024)