Module conv
Expand
Conversions between types
This module implements various conversions between types in the language. Most conversions
are done using to_xx or as_xx methods on the source types.
to_xx()
to_xx is used for conversions that preserve the underlying value. For example,
casting an unsigned integer to a signed integer requires adding an additional bit
to ensure the original value is still representable. Therefore uint::to_int
outputs a value with one more bit than the input.
as_xx()
as_xx on the other hand is used to re-interpret the original bits as a value of the new
type. uint::as_int when used on a value which does not fit in in an integer of the same
size results in a negative number.
Functions
- bits_to_int
Casts a an array of
boolto a signed integer by reinterpreting its bits.- bits_to_uint
Casts an array of
boolto an unsigned integer by reinterpreting its bits.- concat_arrays
Creates an array by concatenating the elements of two existing ones.
- flip_array
Reverses the elements of an array.
- int_to_bits
Casts a signed integer to an array of
boolby reinterpreting its bits.- int_to_uint
Casts a signed integer to an unsigned integer by reinterpreting its bits.
- tri_to_bool
Casts a
trito aboolby reinterpreting its bit.- uint_to_bits
Casts an unsigned integer to an array of
boolby reinterpreting its bits.- uint_to_int
Casts an unsigned integer to a signed integer by reinterpreting its bits.
Entities
- bool_to_clock
Casts a
boolto a clock signal by reinterpreting its bit.- clock_to_bool
Casts a clock signal to a
boolby reinterpreting its bit.
Implementations
impl<#uint N> [bool; N]
pub fn as_int(self) -> int<N>
Casts a an array of bool to a signed integer by reinterpreting its bits.
Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.
Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.
Examples
assert [true, true, true, false].as_int() == 0b0111i4;
assert [true, true, true, true].as_int() == -0b0001i4;pub fn as_uint(self) -> uint<N>
Casts an array of bool to an unsigned integer by reinterpreting its bits.
Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.
Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.
Examples
assert [true, false, true, true].as_uint() == 0b1101u4;pub fn to_int(self) -> int<N>
Casts a an array of bool to a signed integer by reinterpreting its bits.
Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.
Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.
Examples
assert [true, true, true, false].to_int() == 0b0111i4;
assert [true, true, true, true].to_int() == -0b0001i4;pub fn to_uint(self) -> uint<N>
Casts an array of bool to an unsigned integer by reinterpreting its bits.
Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.
Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.
Examples
assert empty.to_uint() == 0u0;
assert [true, false, true, true].to_uint() == 0b1101u4;impl bool
pub unsafe fn as_clock(self) -> clock
Casts a bool to a clock signal by reinterpreting its bit.
This function must be used with caution. Using clock signals as part of logic expressions may lead to unexpected issues depending on the target.
pub fn as_uint(self) -> uint<1>
Casts a bool to an unsigned 1-bit integer by reinterpreting its bits.
Examples
assert false.as_uint() == 0u1;
assert true.as_uint() == 1u1;pub unsafe fn to_clock(self) -> clock
Casts a bool to a clock signal by reinterpreting its bit.
This function must be used with caution. Using clock signals as part of logic expressions may lead to unexpected issues depending on the target.
pub fn to_uint(self) -> uint<1>
Casts a bool to an unsigned 1-bit integer by reinterpreting its bits.
Examples
assert false.as_uint() == 0u1;
assert true.as_uint() == 1u1;impl clock
pub unsafe fn as_bool(self) -> bool
Casts a clock signal to a bool by reinterpreting its bit.
This function must be used with caution. Using clock signals as part of logic expressions may lead to unexpected issues depending on the target.
pub unsafe fn to_bool(self) -> bool
Casts a clock signal to a bool by reinterpreting its bit.
This function must be used with caution. Using clock signals as part of logic expressions may lead to unexpected issues depending on the target.
impl tri
pub fn as_bool(self) -> bool
Casts a tri to a bool by reinterpreting its bit.
LOW and HIGH map to true and false respectively, but the result for HIGHIMP is
undefined. In practice, in a simulator, it remains a high impedance value.
Examples
assert LOW.as_bool() == false;
assert HIGH.as_bool() == true;pub fn to_bool(self) -> bool
Casts a tri to a bool by reinterpreting its bit.
LOW and HIGH map to true and false respectively, but the result for HIGHIMP is
undefined. In practice, in a simulator, it remains a high impedance value.
Examples
assert LOW.to_bool() == false;
assert HIGH.to_bool() == true;impl<#uint N> int<N>
pub fn as_uint(self) -> uint<N>
Casts a signed integer to an unsigned integer by reinterpreting its bits.
This does not preserve the original numeric value if the value is negative.
Examples
assert 13i8.as_uint() == 13u8;
assert (-5i8).as_uint() == 251u8;pub fn as_bits(self) -> [bool; N]
Casts a signed integer to an array of bool by reinterpreting its bits.
Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.
Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.
Examples
assert 0i0.as_bits() == [];
assert 0b0111i4.as_bits() == [true, true, true, false];
assert (-0b0001i4).as_bits() == [true, true, true, true];pub fn to_bits(self) -> [bool; N]
Casts a signed integer to an array of bool by reinterpreting its bits.
Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.
Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.
Examples
assert 0i0.to_bits() == [];
assert 0b0111i4.to_bits() == [true, true, true, false];
assert (-0b0001i4).to_bits() == [true, true, true, true];impl<#uint N> uint<N>
pub fn as_int(self) -> int<N>
Casts an unsigned integer to a signed integer by reinterpreting its bits.
Note that the numeric value is not preserved. A value that is greater than 2^N will
result in a negative value. To preserve the numeric representation, use to_int.
Examples
assert 42u8.as_int() == 42i8;
assert 255u8.as_int() == -1i8;pub fn as_bits(self) -> [bool; N]
Casts an unsigned integer to an array of bool by reinterpreting its bits.
Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.
Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.
Examples
assert 0u0.as_bits() == [];
assert 0b1101u4.as_bits() == [true, false, true, true];pub fn to_int(self) -> int<{ N + 1 }>
Convert an unsigned integer to a signed integer.
The output has one additional bits in order to ensure that the original value
fits. If you want to re-interpret the bits instead, use as_int.
Examples
assert 42u8.to_int() == 42i9;
assert 255u8.to_int() == 255i9;pub fn to_bits(self) -> [bool; N]
Casts an unsigned integer to an array of bool by reinterpreting its bits.
Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.
Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.
Examples
assert 0u0.to_bits() == [];
assert 0b1101u4.to_bits() == [true, false, true, true];impl<#uint N> uint<N>
where
N == N / 8 * 8 else "Integer cannot be divided into bytes",
fn to_le_bytes(self) -> [uint<8>; { N / 8 }]
Converts self into an array of bytes in little-endian order.
Examples
let bytes = 0x12345678u32.to_le_bytes();
assert bytes == [0x78, 0x56, 0x34, 0x12];fn to_be_bytes(self) -> [uint<8>; { N / 8 }]
Converts self into an array of bytes in big-endian order.
Examples
let bytes = 0x12345678u32.to_be_bytes();
assert bytes == [0x12, 0x34, 0x56, 0x78];fn to_german_endian_bytes(self) -> [uint<8>; { N / 8 }]
Converts self into an array of bytes in the order that germans say digits.
Examples
assert 0x12u8.to_german_endian_bytes() == [0x12];
assert 0x1234u16.to_german_endian_bytes() == [0x34, 0x12];
assert 0x12345678u32.to_german_endian_bytes() == [0x12, 0x34, 0x78, 0x56];