silk.mem.interned.
Handles to values that never change and are never freed, so a handle is a plain pointer.
When to use#
Use Interned for values that live as long as the program phase that makes them, such as the
hash-consed types of one compiler check. Copying a handle touches no memory, and a value can be
read from any thread once its handle is published.
Gotchas#
The value is never dropped. Use silk.mem.frozen.Frozen for a value that should be freed.
Examples#
Copy a handle and read through both#
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.core.effect { Effect }
import silk.mem.interned { Interned }
pub effect fn main() -> i32
! OutOfMemoryError {
let mut allocator = Allocator.systemAllocatorProvider()
let first = run Interned<i32>.make(40) |> Effect.provideMut<Allocator>(&mut allocator)
let second = Interned<i32>.clone(&first)
let sum = Interned<i32>.get(&first).* + Interned<i32>.get(&second).*
if sum != 80 || !Interned<i32>.same(&first, &second) { return 1 }
return 0
}Import as Interned with import silk.mem.interned { Interned }.
Public declarations: 1.
Interned#
pub struct Interned<T>A handle to one value of type T that never changes and is never freed.
Implementation Interned<T>#
impl<T> Interned<T>Associated function make#
pub effect<'env> fn make<'env>(value: T) -> Interned<T> ! OutOfMemoryError ? &mut AllocatorAllocates storage that is never freed and moves value into it.
Errors#
OutOfMemoryErrorif theAllocatorcannot supply the storage.valueis dropped.
Associated function discard#
pub unsafe fn discard(handle: Interned<T>) -> ()Frees a value that no other handle refers to, such as one made only to look up an equal value that already has a handle.
Safety#
handle is the only handle to its value: it was never cloned.
Method clone#
pub fn clone(self: &Self) -> Interned<T>Returns another handle to the same value. It copies one pointer.
Method get#
pub fn get<'a>(self: &'a Self) -> &'a TBorrows the value for as long as this handle is borrowed.
Method with#
pub fn with<A>(self: &Self, use: once fn(&T) -> A) -> ACalls use one time with a shared borrow of the value and returns the result of use.
Method address#
pub fn address(self: &Self) -> usizeThe address of the value, which every handle to it shares.
Associated function same#
pub fn same(left: &Self, right: &Self) -> boolWhether both handles refer to the same value.
Category: Memory.
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.