Silk

silk/u8

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.

Eight-bit unsigned integers for bytes, compact counters, and exact binary representations.

When to use

Use u8 for an individual byte or a field specified as eight unsigned bits. Prefer a wider integer for general counting unless the 0 through 255 range is intentional.

Details

Ordinary arithmetic, narrowing conversions, division by zero, and invalid shift counts trap. checked* returns Option for invalid arithmetic, wrapping* computes modulo 2^8, and saturating* clamps at MIN or MAX. Right shift inserts zero bits.

Decimal parse accepts only a complete unsigned representation; a leading minus sign is malformed rather than an out-of-range unsigned value. Use Format with Format.display for defaults or Format.displayWith for explicit options. Both operations stream through a mutable Writer without Allocator.

Examples

Choose an overflow policy instead of relying on a trap

import silk.option { Option }

import silk.u8

pub fn main() -> i32 {
  let checked = u8.checkedAdd(255, 1)
  let recovered = move checked
    |> Option.unwrapOr<u8>(42)
  if recovered != 42 {
    return 1
  }
  if u8.wrappingAdd(255, 1) != 0 {
    return 2
  }
  if u8.saturatingAdd(255, 1) != 255 {
    return 3
  }
  return 42
}

See also

Use the silk.char module when a value is a Unicode scalar rather than an arbitrary byte.

Import as u8 with import silk.u8.

Public declarations: 56.

MAX

pub const MAX: u8

The largest u8 value.

MIN

pub const MIN: u8

The smallest u8 value.

BITS

pub const BITS: u32

The fixed width of u8, in bits.

toU8

pub fn toU8(value: u8) -> u8

Returns value unchanged as u8. Use this function when generic conversion code can select u8 as both source and destination.

checkedToU8

pub fn checkedToU8(value: u8) -> silk/option.Option<u8>

Returns Some with value unchanged as u8. Use this function when generic checked-conversion code can select the same source and destination type.

toU16

pub fn toU16(value: u8) -> u16

Converts value exactly to u16. Every u8 value is representable.

checkedToU16

pub fn checkedToU16(value: u8) -> silk/option.Option<u16>

Converts value exactly to u16 and returns Some. Every u8 value is representable.

toU32

pub fn toU32(value: u8) -> u32

Converts value exactly to u32. Every u8 value is representable.

checkedToU32

pub fn checkedToU32(value: u8) -> silk/option.Option<u32>

Converts value exactly to u32 and returns Some. Every u8 value is representable.

toU64

pub fn toU64(value: u8) -> u64

Converts value exactly to u64. Every u8 value is representable.

checkedToU64

pub fn checkedToU64(value: u8) -> silk/option.Option<u64>

Converts value exactly to u64 and returns Some. Every u8 value is representable.

toUsize

pub fn toUsize(value: u8) -> usize

Converts value exactly to usize. Every u8 value is representable.

checkedToUsize

pub fn checkedToUsize(value: u8) -> silk/option.Option<usize>

Converts value exactly to usize and returns Some. Every u8 value is representable.

toI8

pub fn toI8(value: u8) -> i8

Converts value to i8. Traps if value is outside the i8 range. Use this function when an out-of-range value is a program error.

checkedToI8

pub fn checkedToI8(value: u8) -> silk/option.Option<i8>

Converts value to i8, or returns None if value is outside the i8 range. Use this function when an out-of-range value is input data.

toI16

pub fn toI16(value: u8) -> i16

Converts value exactly to i16. Every u8 value is representable.

checkedToI16

pub fn checkedToI16(value: u8) -> silk/option.Option<i16>

Converts value exactly to i16 and returns Some. Every u8 value is representable.

toI32

pub fn toI32(value: u8) -> i32

Converts value exactly to i32. Every u8 value is representable.

checkedToI32

pub fn checkedToI32(value: u8) -> silk/option.Option<i32>

Converts value exactly to i32 and returns Some. Every u8 value is representable.

toI64

pub fn toI64(value: u8) -> i64

Converts value exactly to i64. Every u8 value is representable.

checkedToI64

pub fn checkedToI64(value: u8) -> silk/option.Option<i64>

Converts value exactly to i64 and returns Some. Every u8 value is representable.

toIsize

pub fn toIsize(value: u8) -> isize

Converts value exactly to isize. Every u8 value is representable.

checkedToIsize

pub fn checkedToIsize(value: u8) -> silk/option.Option<isize>

Converts value exactly to isize and returns Some. Every u8 value is representable.

toF32

pub fn toF32(value: u8) -> f32

Converts value to the nearest f32 value, with ties to even.

toF64

pub fn toF64(value: u8) -> f64

Converts value to the nearest f64 value, with ties to even.

add

pub fn add(left: u8, right: u8) -> u8

Returns left + right and traps if the result is outside the u8 range. Use this function when overflow is a program error.

subtract

pub fn subtract(left: u8, right: u8) -> u8

Returns left - right and traps if the result is outside the u8 range. Use this function when overflow is a program error.

multiply

pub fn multiply(left: u8, right: u8) -> u8

Returns left * right and traps if the result is outside the u8 range. Use this function when overflow is a program error.

divide

pub fn divide(left: u8, right: u8) -> u8

Returns left / right. Traps if right is zero. Use this function when a zero divisor is a program error.

remainder

pub fn remainder(left: u8, right: u8) -> u8

Returns the remainder of left / right. Traps if right is zero. Use this function when a zero divisor is a program error.

bitAnd

pub fn bitAnd(left: u8, right: u8) -> u8

Returns the bitwise AND of left and right.

bitOr

pub fn bitOr(left: u8, right: u8) -> u8

Returns the bitwise OR of left and right.

bitXor

pub fn bitXor(left: u8, right: u8) -> u8

Returns the bitwise exclusive OR of left and right.

bitNot

pub fn bitNot(value: u8) -> u8

Returns value with each bit inverted.

shiftLeft

pub fn shiftLeft(left: u8, right: u8) -> u8

Shifts left bits left by right positions. Traps if right is not less than BITS.

shiftRight

pub fn shiftRight(left: u8, right: u8) -> u8

Shifts left bits right by right positions and inserts zero bits. Traps if right is not less than BITS.

rotateLeft

pub fn rotateLeft(left: u8, right: u8) -> u8

Rotates the bits of left left by right positions.

rotateRight

pub fn rotateRight(left: u8, right: u8) -> u8

Rotates the bits of left right by right positions.

wrappingAdd

pub fn wrappingAdd(left: u8, right: u8) -> u8

Returns left + right, wrapped to the u8 range. Use this function for deliberate modulo arithmetic.

wrappingSubtract

pub fn wrappingSubtract(left: u8, right: u8) -> u8

Returns left - right, wrapped to the u8 range. Use this function for deliberate modulo arithmetic.

wrappingMultiply

pub fn wrappingMultiply(left: u8, right: u8) -> u8

Returns left * right, wrapped to the u8 range. Use this function for deliberate modulo arithmetic.

saturatingAdd

pub fn saturatingAdd(left: u8, right: u8) -> u8

Returns left + right, clamped to MIN or MAX. Use this function when a boundary value is the required overflow result.

saturatingSubtract

pub fn saturatingSubtract(left: u8, right: u8) -> u8

Returns left - right, clamped to MIN or MAX. Use this function when a boundary value is the required overflow result.

saturatingMultiply

pub fn saturatingMultiply(left: u8, right: u8) -> u8

Returns left * right, clamped to MIN or MAX. Use this function when a boundary value is the required overflow result.

checkedAdd

pub fn checkedAdd(left: u8, right: u8) -> silk/option.Option<u8>

Returns Some with left + right, or None if the result is outside the u8 range. Use this function when overflow is input data.

checkedSubtract

pub fn checkedSubtract(left: u8, right: u8) -> silk/option.Option<u8>

Returns Some with left - right, or None if the result is outside the u8 range. Use this function when overflow is input data.

checkedMultiply

pub fn checkedMultiply(left: u8, right: u8) -> silk/option.Option<u8>

Returns Some with left * right, or None if the result is outside the u8 range. Use this function when overflow is input data.

checkedDivide

pub fn checkedDivide(left: u8, right: u8) -> silk/option.Option<u8>

Returns Some with left / right, or None if right is zero. Use this function when a zero divisor is input data.

checkedRemainder

pub fn checkedRemainder(left: u8, right: u8) -> silk/option.Option<u8>

Returns Some with the remainder, or None if right is zero. Use this function when a zero divisor is input data.

equals

pub fn equals(left: u8, right: u8) -> bool

Returns true when left and right are equal.

notEquals

pub fn notEquals(left: u8, right: u8) -> bool

Returns true when left and right are not equal.

lessThan

pub fn lessThan(left: u8, right: u8) -> bool

Returns true when left is less than right.

lessOrEqual

pub fn lessOrEqual(left: u8, right: u8) -> bool

Returns true when left is less than or equal to right.

greaterThan

pub fn greaterThan(left: u8, right: u8) -> bool

Returns true when left is greater than right.

greaterOrEqual

pub fn greaterOrEqual(left: u8, right: u8) -> bool

Returns true when left is greater than or equal to right.

parse

pub fn parse<'life0>(text: string<'life0>) -> silk/result.Result<u8, silk/format.ParseError>

Reads the complete text as an unsigned decimal u8.

Details

A failure contains silk.format.NotANumber for empty text, a sign, a non-digit, or trailing bytes. It contains silk.format.OutOfRange if the number is outside the u8 range.

On this page