Byte order v0.1.0
TargetIsLittleEndian
pub func TargetIsLittleEndian() -> bool
Whether this target stores an integer's least significant byte first. Takes nothing and returns the answer by copy. It is established by writing a value whose bytes differ and reading back the first one, so it is right on any target rather than on a list of known ones. Cannot fail. Runs in constant time and allocates nothing.
Returns: whether the target stores the least significant byte first
ToLittleEndian<T>
pub func ToLittleEndian<T>(value: T) -> T
The value's bytes as a little-endian value would hold them.
value is read by value; the result is returned by copy. On a little-endian target this is the identity; on a
big-endian one it is a byte reversal. The result is a bit pattern to be stored, not a number to compute with.
Cannot fail. Costs nothing on a little-endian target and one byte reversal on a big-endian one, so it runs in time
linear in the type's width at worst and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: the value with its bytes in little-endian order
FromLittleEndian<T>
pub func FromLittleEndian<T>(value: T) -> T
The value a little-endian bit pattern stands for on this target.
value is read by value; the result is returned by copy. The exact inverse of ToLittleEndian, and the same
operation — a byte reversal is its own inverse.
Cannot fail. Costs exactly what ToLittleEndian costs and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: the value read from little-endian bytes, in the target's own order
ToBigEndian<T>
pub func ToBigEndian<T>(value: T) -> T
The value's bytes as a big-endian value would hold them.
value is read by value; the result is returned by copy. On a big-endian target this is the identity; on a
little-endian one it is a byte reversal. This is network byte order.
Cannot fail. Costs nothing on a big-endian target and one byte reversal on a little-endian one, so it runs in time
linear in the type's width at worst and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: the value with its bytes in big-endian order
FromBigEndian<T>
pub func FromBigEndian<T>(value: T) -> T
The value a big-endian bit pattern stands for on this target.
value is read by value; the result is returned by copy. The exact inverse of ToBigEndian, and the same
operation.
Cannot fail. Costs exactly what ToBigEndian costs and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: the value read from big-endian bytes, in the target's own order
StoreLittleEndian<T>
pub func StoreLittleEndian<T>(
value: T,
destination: *var byte
)
Writes an integer to raw bytes, least significant byte first.
value is read by value. destination is borrowed for the call and receives exactly sizeof(T) bytes; it is
never read. The bytes need no alignment, so a value may be written straight into the middle of a buffer.
Cannot fail: every value has a representation. Runs in time linear in the type's width and allocates nothing.
destination must be non-null and must have room for sizeof(T) bytes. Neither is checked.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
destination | *var byte | receives the bytes |
StoreBigEndian<T>
pub func StoreBigEndian<T>(value: T, destination: *var byte)
Writes an integer to raw bytes, most significant byte first.
value is read by value. destination is borrowed for the call and receives exactly sizeof(T) bytes; it is
never read. This is the order a network protocol asks for.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
destination must be non-null and must have room for sizeof(T) bytes. Neither is checked.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
destination | *var byte | receives the bytes |
LoadLittleEndian<T>
pub func LoadLittleEndian<T>(
source: *byte,
destination: *var T
)
Reads an integer from raw bytes holding it least significant byte first.
source is borrowed for the call and sizeof(T) bytes are read from it. destination names the type being read
and receives the value; whatever it held is overwritten. The bytes need no alignment.
Cannot fail: every byte sequence of the right length is a value. Runs in time linear in the type's width and
allocates nothing.
source must be non-null and readable for sizeof(T) bytes, and destination must be non-null. Neither is
checked.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
source | *byte | the bytes to read |
destination | *var T | receives the bytes |
LoadBigEndian<T>
pub func LoadBigEndian<T>(
source: *byte,
destination: *var T
)
Reads an integer from raw bytes holding it most significant byte first.
source is borrowed for the call and sizeof(T) bytes are read from it. destination names the type being read
and receives the value; whatever it held is overwritten.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
source must be non-null and readable for sizeof(T) bytes, and destination must be non-null. Neither is
checked.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
source | *byte | the bytes to read |
destination | *var T | receives the bytes |
Bit operations
Counting, rotating, reversing and testing the bits of an integer of any width, and a right shift that brings in zeros whatever the sign.
Checked conversion
Converting between primitive types with a report of whether the value survived, with wrapping on purpose, or clamped to the destination's range.