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#

silk
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#

silk
pub struct Interned<T>

A handle to one value of type T that never changes and is never freed.

Implementation Interned<T>#

silk
impl<T> Interned<T>

Associated function make#

silk
pub effect<'env> fn make<'env>(value: T) -> Interned<T> ! OutOfMemoryError ? &mut Allocator

Allocates storage that is never freed and moves value into it.

Errors#

  • OutOfMemoryError if the Allocator cannot supply the storage. value is dropped.

Associated function discard#

silk
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#

silk
pub fn clone(self: &Self) -> Interned<T>

Returns another handle to the same value. It copies one pointer.

Method get#

silk
pub fn get<'a>(self: &'a Self) -> &'a T

Borrows the value for as long as this handle is borrowed.

Method with#

silk
pub fn with<A>(self: &Self, use: once fn(&T) -> A) -> A

Calls use one time with a shared borrow of the value and returns the result of use.

Method address#

silk
pub fn address(self: &Self) -> usize

The address of the value, which every handle to it shares.

Associated function same#

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

Whether 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.

View the source of this page