Unaligned and byte-order access v0.1.0

Source
Reading and writing values at any alignment and in either byte order.

LoadUnaligned<T>

pub func LoadUnaligned<T>(
    source: *opaque,
    destination: *var T
)

Reads a value of T from source, whatever source is aligned to. source is borrowed and sizeof(T) bytes are read from it; destination receives the value. Both must be non-null. The bytes are taken one at a time, so this is correct at any address on any machine — including the ones where a misaligned load faults rather than merely costing more. Cannot fail. Runs in time proportional to sizeof(T) and allocates nothing. source must be readable for sizeof(T) bytes. The bytes are taken as they are, so a value that is not a valid T stays invalid; this reinterprets rather than validating.

Source

Type parameters

NameDescription
Tthe type whose bytes are read

Parameters

NameTypeDescription
source*opaquethe address to read sizeof(T) bytes from, at any alignment
destination*var Treceives the value

StoreUnaligned<T>

pub func StoreUnaligned<T>(
    destination: *var opaque,
    value: *T
)

Writes a value of T to destination, whatever destination is aligned to. value is borrowed and read; destination is borrowed and receives sizeof(T) bytes. Both must be non-null. Cannot fail. Runs in time proportional to sizeof(T) and allocates nothing. destination must be writable for sizeof(T) bytes.

Source

Type parameters

NameDescription
Tthe type whose bytes are written

Parameters

NameTypeDescription
destination*var opaquethe address to write sizeof(T) bytes to, at any alignment
value*Tthe value to write

ReverseBlock

pub func ReverseBlock(buffer: *var opaque, length: uint)

Reverses the order of length bytes at buffer, in place. buffer is borrowed and rewritten. A length of zero or one changes nothing and accepts a null pointer at zero. This is the byte-order conversion for a run of bytes rather than for one value; Core::ReverseBytes is the one that takes an integer. Cannot fail. Runs in time proportional to length and allocates nothing. buffer must be readable and writable for length bytes.

Source

Parameters

NameTypeDescription
buffer*var opaquethe bytes to reverse in place
lengthuinthow many bytes; zero or one changes nothing

LoadLittleEndian<T>

pub func LoadLittleEndian<T>(
    source: *opaque,
    destination: *var T
)

Reads a T from source, taking the bytes as little-endian. source is borrowed and sizeof(T) bytes are read; destination receives the value in the target's own order. Both must be non-null. Needs no alignment. This is the pair of calls a file or wire format actually needs: the bytes are wherever the format put them, and their order is the format's rather than the machine's. Cannot fail. Runs in time proportional to sizeof(T) and allocates nothing. source must be readable for sizeof(T) bytes.

Source

Type parameters

NameDescription
Tthe type whose bytes are read

Parameters

NameTypeDescription
source*opaquethe address to read sizeof(T) little-endian bytes from
destination*var Treceives the value in the target's own order

LoadBigEndian<T>

pub func LoadBigEndian<T>(
    source: *opaque,
    destination: *var T
)

Reads a T from source, taking the bytes as big-endian. source is borrowed and sizeof(T) bytes are read; destination receives the value in the target's own order. Both must be non-null. Needs no alignment. This is network byte order. Cannot fail. Runs in time proportional to sizeof(T) and allocates nothing. source must be readable for sizeof(T) bytes.

Source

Type parameters

NameDescription
Tthe type whose bytes are read

Parameters

NameTypeDescription
source*opaquethe address to read sizeof(T) big-endian bytes from
destination*var Treceives the value in the target's own order

StoreLittleEndian<T>

pub func StoreLittleEndian<T>(
    destination: *var opaque,
    value: *T
)

Writes a T to destination as little-endian bytes. value is borrowed and read; destination receives sizeof(T) bytes. Both must be non-null. Needs no alignment. Cannot fail. Runs in time proportional to sizeof(T) and allocates nothing. destination must be writable for sizeof(T) bytes.

Source

Type parameters

NameDescription
Tthe type whose bytes are written

Parameters

NameTypeDescription
destination*var opaquereceives sizeof(T) little-endian bytes
value*Tthe value to write

StoreBigEndian<T>

pub func StoreBigEndian<T>(
    destination: *var opaque,
    value: *T
)

Writes a T to destination as big-endian bytes. value is borrowed and read; destination receives sizeof(T) bytes. Both must be non-null. Needs no alignment. Cannot fail. Runs in time proportional to sizeof(T) and allocates nothing. destination must be writable for sizeof(T) bytes.

Source

Type parameters

NameDescription
Tthe type whose bytes are written

Parameters

NameTypeDescription
destination*var opaquereceives sizeof(T) big-endian bytes
value*Tthe value to write