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#
Follow a link from one row to the next row#
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#
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>#
impl<T> Copy for Id<T>Implementation Id<T>#
impl<T> Id<T>Associated function fromIndex#
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#
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#
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#
pub fn isNone(self: Self) -> boolReturns true if the id is Id.none.
Method index#
pub fn index(self: Self) -> usizeReturns 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#
pub fn raw(self: Self) -> u32Returns the number that the id holds. Id.none gives 4,294,967,295.
Implementation Equal for Id<T>#
impl<T> Equal for Id<T>Operation equals#
fn equals(left: &Self, right: &Self) -> boolImplementation Order for Id<T>#
impl<T> Order for Id<T>Operation compare#
fn compare(left: &Self, right: &Self) -> OrderingImplementation HashKey for Id<T>#
impl<T> HashKey for Id<T>Operation equals#
fn equals(left: &Self, right: &Self) -> boolOperation hash#
fn hash(value: &Self, seed: &HashSeed) -> u64Category: 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.