silk.collections.id.

Typed 32-bit row numbers.

When to use#

Use Id for a row number that code stores: a link from one row to another row, or a key that names a row of one collection. An Id<Fragment> is a different type from an Id<Box>, so the compiler rejects a row number that is used with rows of the wrong type. An Id is 4 bytes, half of a usize on a 64-bit target. Use a plain usize for a loop counter.

Details#

An Id holds one u32. Id.none is the value that names no row. It replaces a sentinel such as usize.MAX. silk.collections.column_list.ColumnList accepts an Id<T> as the index of its T rows, as in fragments[id].size.

Gotchas#

An Id is not checked when it is made. A row index traps when its Id names no row.

Examples#

silk
import silk.collections.column_list { ColumnList }
import silk.collections.id { Id }
import silk.core.effect { Effect }
import silk.core.ownership { Copy }
import silk.mem.allocator { Allocator, OutOfMemoryError }

struct Step {
  pub next: Id<Step>,
  pub cost: u32,
}

impl Copy for Step {}

effect fn total() -> u32
  ! OutOfMemoryError
  ? &mut Allocator {
  let mut steps = ColumnList<Step>.make()
  let last = run steps.appendId(Step { next: Id<Step>.none(), cost: 5 })
  let first = run steps.appendId(Step { next: last, cost: 2 })
  let mut sum: u32 = 0
  let mut current = first
  while !current.isNone() {
    sum = sum + steps[current].cost
    current = steps[current].next
  }
  return sum
}

effect fn recover(error: OutOfMemoryError) -> u32 {
  return 0
}

pub fn main() -> i32 {
  let mut allocator = Allocator.systemAllocatorProvider()
  let sum = run total()
    |> Effect.provideMut<Allocator>(&mut allocator)
    |> Effect.catchAll(recover)
  if sum != 7 {
    return 1
  }
  return 0
}

Import as Id with import silk.collections.id { Id }.

Public declarations: 1.

Id#

silk
pub struct Id<T>

The number of a row of T, in 32 bits.

Details#

The type parameter only names the rows that the number belongs to. An Id<T> is Copy, and two of them compare by their numbers. Id.none names no row.

Implementation Copy for Id<T>#

silk
impl<T> Copy for Id<T>

Implementation Id<T>#

silk
impl<T> Id<T>

Associated function fromIndex#

silk
pub fn fromIndex(index: usize) -> Id<T>

Returns the id of the row at index.

Gotchas#

It traps if index is 4,294,967,295 or more, because that number is Id.none and larger numbers do not fit in 32 bits.

Associated function fromRaw#

silk
pub fn fromRaw(value: u32) -> Id<T>

Returns the id that has the number value. The number 4,294,967,295 gives Id.none.

Associated function none#

silk
pub fn none() -> Id<T>

Returns the id that names no row.

When to use#

Use it as the value of a link that has no target, such as the next sibling of the last child. Test for it with Id.isNone.

Method isNone#

silk
pub fn isNone(self: Self) -> bool

Returns true if the id is Id.none.

Method index#

silk
pub fn index(self: Self) -> usize

Returns the row index that the id names.

Gotchas#

Id.none gives 4,294,967,295, which is not the index of a row.

Method raw#

silk
pub fn raw(self: Self) -> u32

Returns the number that the id holds. Id.none gives 4,294,967,295.

Implementation Equal for Id<T>#

silk
impl<T> Equal for Id<T>

Operation equals#

silk
fn equals(left: &Self, right: &Self) -> bool

Implementation Order for Id<T>#

silk
impl<T> Order for Id<T>

Operation compare#

silk
fn compare(left: &Self, right: &Self) -> Ordering

Implementation HashKey for Id<T>#

silk
impl<T> HashKey for Id<T>

Operation equals#

silk
fn equals(left: &Self, right: &Self) -> bool

Operation hash#

silk
fn hash(value: &Self, seed: &HashSeed) -> u64

Category: Collections.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page