hanki

u8

stdlib/u8.hk: inherent conversion methods on u8.

impl u8

to_int

def to_int(self) -> int

Widen to the arbitrary-precision int tier. Total and exact.

7u8.to_int() => 7

_parse

def _parse(s: string) -> Option<u8>

The runtime seam under [FromString]: the raw yes-or-no parse, with no room to say why. parse below is the surface, and turns the None into a reason.

_reason

def _reason(s: string) -> string

A fixed-width parse fails two ways and the reason has to say which. int is the arbitrary-precision tier, and it accepts the texts that denote a whole number, which is the discriminator.

The sign is tested first, and not folded into the range arm: an unsigned width refuses a leading - outright, and "-0" fails even though the value it denotes is in range.

bit_and

def bit_and(self, other: u8) -> u8

Per-bit AND with another u8. Total (HANKI.md §3).

12u8.bit_and(10u8) => 8u8

bit_or

def bit_or(self, other: u8) -> u8

Per-bit OR.

12u8.bit_or(10u8) => 14u8

bit_xor

def bit_xor(self, other: u8) -> u8

Per-bit XOR.

12u8.bit_xor(10u8) => 6u8

bit_not

def bit_not(self) -> u8

Per-bit complement (one's complement): every bit flips, and 0 becomes all-ones, 255 for an unsigned width.

0u8.bit_not() => 255u8

bit_shl

def bit_shl(self, n: u32) -> u8

Left shift by n bits, zero-filling the low end. Total: n is taken mod the 8-bit width, and an over-shift wraps in place of trapping.

1u8.bit_shl(3u32) => 8u8

bit_shr

def bit_shr(self, n: u32) -> u8

Right shift by n bits, logical: the high bits are zero-filled. Total: n is taken mod the 8-bit width.

12u8.bit_shr(1u32) => 6u8

checked_add

def checked_add(self, other: u8) -> Option<u8>

Checked arithmetic (HANKI.md §3): the bare operators wrap; these twins return None on overflow (u8 / // % also on a zero divisor) so a caller can detect it as a value.

100u8.checked_add(23u8)  => Some(123u8)
200u8.checked_add(100u8) => None

checked_sub

def checked_sub(self, other: u8) -> Option<u8>

Checked subtraction. None where the difference would underflow below 0 (u8 is unsigned and has no negative values).

5u8.checked_sub(3u8) => Some(2u8)
0u8.checked_sub(1u8) => None

checked_mul

def checked_mul(self, other: u8) -> Option<u8>

Checked multiplication. None where the true product overflows u8.

10u8.checked_mul(20u8) => Some(200u8)
200u8.checked_mul(2u8) => None

checked_neg

def checked_neg(self) -> Option<u8>

Checked negation. For unsigned, only 0 negates in range; any nonzero value is None.

0u8.checked_neg() => Some(0u8)
5u8.checked_neg() => None

checked_div

def checked_div(self, other: u8) -> Option<u8>

Checked truncating division. None only on a zero divisor (u8 is non-negative, and there is no MIN / -1 case).

10u8.checked_div(3u8) => Some(3u8)
10u8.checked_div(0u8) => None

checkedfloordiv

def checked_floor_div(self, other: u8) -> Option<u8>

Checked floor division. Every unsigned operand is non-negative and this equals checked_div; None only on a zero divisor.

10u8.checked_floor_div(3u8) => Some(3u8)
10u8.checked_floor_div(0u8) => None

checked_mod

def checked_mod(self, other: u8) -> Option<u8>

Checked modulo. None only on a zero divisor.

7u8.checked_mod(3u8) => Some(1u8)
7u8.checked_mod(0u8) => None

to_u16

def to_u16(self) -> u16

Widens to u16. Always exact.

255u8.to_u16() => 255u16

to_u32

def to_u32(self) -> u32

Widens to u32. Always exact.

255u8.to_u32() => 255u32

to_u64

def to_u64(self) -> u64

Widens to u64. Always exact.

255u8.to_u64() => 255u64

to_bytes

def to_bytes(self) -> bytes

A one-byte bytes holding this raw byte, the pure inverse of bytes.get. Unlike string.to_bytes (UTF-8 encode), this is the byte itself, and it round-trips every value 0..=255.

65u8.to_bytes().get(0) => Some(65u8)
0u8.to_bytes().length => 1

impl FromString<u8>

parse

def parse(s: string) -> Result<u8, ParseError>

Parses a base-10 u8. A value outside 0 ..= 255 is refused rather than wrapped, and a leading - is refused outright. Surrounding whitespace is trimmed.

u8.parse("42")   => Ok(42u8)
u8.parse("255") => Ok(255u8)
u8.parse("256") => Err(ParseError(input="256", reason="out of range for u8 (0 ..= 255)"))
u8.parse("-1")   => Err(ParseError(input="-1", reason="negative, and u8 is unsigned"))
u8.parse("nope") => Err(ParseError(input="nope", reason="not a base-10 integer"))