silk/slot
Explicit initialization-state transitions for one slot selected from raw storage.
When to use
Use these operations with silk.raw-buffer while implementing a container. Application code
should prefer an initialized collection, which maintains the slot state on the caller's behalf.
Details
write changes an uninitialized slot to initialized. take and dropValue change it
back to uninitialized, while copy leaves an initialized Copy value in place. Consuming a
Slot<T> prevents reusing the same selection accidentally, but the container must still keep
its own initialization map.
Gotchas
Selecting an out-of-bounds slot, writing twice, or reading an uninitialized slot violates the raw-storage contract. Debug execution may diagnose these mistakes; portable code must not rely on a runtime check.
Examples
Copy and then take one initialized value
import silk.allocator { Allocator }
import silk.allocator {Allocator}
import silk.effect { Effect }
import silk.layout as Layout
import silk.raw_buffer as RawBuffer
import silk.slot as Slot
effect fn build() -> i32
! Allocator.OutOfMemoryError {
let mut allocator = Allocator.systemAllocatorProvider()
let acquiring = Allocator.allocate(Layout.of<i32>())
|> Effect.provideMut<Allocator>(&mut allocator)
let allocation = run acquiring
unsafe {
let mut buffer = RawBuffer.from<i32>(move allocation, 1)
let written = Slot.write(RawBuffer.slot(&mut buffer, 0), 21)
let copied = Slot.copy(RawBuffer.slot(&mut buffer, 0))
let taken = Slot.take(RawBuffer.slot(&mut buffer, 0))
return copied + taken
}
return 0
}
effect fn recover(error: Allocator.OutOfMemoryError) -> i32 {
return 0
}
pub fn main() -> i32 {
return run Effect.catchAll(build(), recover)
}Import as Slot with import silk.slot.
Public declarations: 4.
write
pub fn write<T>(slot: silk/core.Slot<T>, value: T) -> ()Moves one value into a selected uninitialized raw slot.
Gotchas
The slot must be in bounds and uninitialized. A second write without a state transition is invalid.
take
pub fn take<T>(slot: silk/core.Slot<T>) -> TMoves a value out of a selected initialized raw slot and leaves the slot uninitialized.
Gotchas
The slot must be in bounds and initialized.
copy
pub fn copy<T>(slot: silk/core.Slot<T>) -> TCopies a Copy value from a selected initialized raw slot without changing its state.
Gotchas
The slot must be in bounds and initialized.
dropValue
pub fn dropValue<T>(slot: silk/core.Slot<T>) -> ()Drops a value in a selected initialized raw slot and leaves the slot uninitialized.
Gotchas
The slot must be in bounds and initialized.