silk.mem.any_box.

Owned heap storage for a value behind an erased interface contract.

When to use#

Use AnyBox when a struct, collection, or task must own a heterogeneous value satisfying an interface contract (any Interface, optionally with + Transferable and + Concurrent), and a borrowed &any Interface or &mut any Interface reference cannot live long enough. Use silk.mem.box.Box when the concrete type is fixed at compile time.

Details#

AnyBox.make allocates space for one concrete value C through Allocator, moves C into that allocation, and returns AnyBox<E> holding the concrete type's runtime tag and owned allocation. get and getMut borrow the stored value as &E and &mut E for interface dispatch. narrow converts an AnyBox<From> into an AnyBox<To> in place when From satisfies To without reallocating.

AnyBox<E> is affine (never Copy). Dropping an AnyBox<E> runs the stored concrete type's Drop cleanup before releasing its allocation. AnyBox<E> is Transferable if and only if E carries + Transferable, and Concurrent if and only if E carries + Concurrent.

Errors#

AnyBox.make fails with OutOfMemoryError if Allocator.allocate cannot supply storage for the concrete value.

Examples#

Store different implementations of an interface in one list#

silk
import silk.collections.list { List }
import silk.core.effect { Effect }
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.mem.any_box { AnyBox }

interface Named {
  fn code(self: &Self) -> i32
}

struct Left {}
struct Right { offset: i32 }

impl Named for Left {
  fn code(self: &Self) -> i32 {
    return 20
  }
}

impl Named for Right {
  fn code(self: &Self) -> i32 {
    return 20 + self.offset
  }
}

effect fn program() -> i32
  ! OutOfMemoryError {
  let mut allocator = Allocator.systemAllocatorProvider()
  let mut items = List<AnyBox<any Named>>.make()
  run List<AnyBox<any Named>>.append(
    &mut items,
    run AnyBox<any Named>.make(Left {}) |> Effect.provideMut<Allocator>(&mut allocator),
  ) |> Effect.provideMut<Allocator>(&mut allocator)
  run List<AnyBox<any Named>>.append(
    &mut items,
    run AnyBox<any Named>.make(Right { offset: 2 }) |> Effect.provideMut<Allocator>(&mut allocator),
  ) |> Effect.provideMut<Allocator>(&mut allocator)
  let slice = List<AnyBox<any Named>>.asSlice(&items)
  let total = slice[0].get().code() + slice[1].get().code()
  if total != 42 {
    return 1
  }
  return 0
}

effect fn outOfMemory(error: OutOfMemoryError) -> i32 {
  return 1
}

pub fn main() -> i32 {
  return run Effect.catchAll(program(), outOfMemory)
}

Import as AnyBox with import silk.mem.any_box { AnyBox }.

Public declarations: 1.

AnyBox#

silk
pub struct AnyBox<E>

An owned heap box holding one value erased behind interface contract E.

When to use#

Use AnyBox<E> when you need to move, store, or return an owned heterogeneous value whose type implements E (such as any Component or any Task + Transferable).

Details#

E must be an erased interface contract (any I, with optional + Transferable and + Concurrent). An AnyBox<E> holds a tag identifying the concrete type and one allocation storing that value. When the AnyBox<E> is dropped, it runs the concrete value's Drop logic before releasing the allocation.

AnyBox<E> is never Copy. It satisfies Transferable when E requires + Transferable, and Concurrent when E requires + Concurrent.

Gotchas#

AnyBox<E> rejects non-erased type arguments (such as AnyBox<i32>). The concrete type passed to AnyBox.make must satisfy all interface and thread-safety bounds named in E.

Implementation AnyBox<E>#

silk
impl<E> AnyBox<E>

Associated function make#

silk
pub effect fn make<C>(value: C) -> AnyBox<E>
    ! OutOfMemoryError
    ? &mut Allocator

Allocates space through Allocator and moves value into a new AnyBox<E>.

Details#

Requests Layout.of<C>() from &mut Allocator and stores value alongside the runtime tag for C under erased contract E. Dropping the returned AnyBox<E> drops value and releases the allocation.

Errors#

  • OutOfMemoryError when the allocator cannot supply storage for C. No value is leaked.

Gotchas#

C must implement every interface in E, plus Transferable and Concurrent if E includes those bounds. Calling AnyBox<E>.make with a type C that does not satisfy E fails compilation.

Method get#

silk
pub fn get(self: &Self) -> &E

Borrows the boxed value as a shared erased interface reference &E.

Details#

The returned reference shares self's borrow lifetime and dispatches interface methods dynamically to the boxed concrete value without allocating.

Method getMut#

silk
pub fn getMut(self: &mut Self) -> &mut E

Borrows the boxed value as an exclusive erased interface reference &mut E.

Details#

The returned reference borrows self exclusively so interface methods taking &mut Self can mutate the boxed value in place without reallocating.

Method narrow#

silk
pub fn narrow<To>(self: Self) -> AnyBox<To>

Narrows this AnyBox<E> into an AnyBox<To> without moving the underlying allocation.

Details#

Preserves the concrete value's tag and heap allocation while changing the static contract from E to To. To must be an erased interface contract whose interfaces and thread-safety bounds are already required by E.

Gotchas#

Narrowing to a contract To with requirements that E does not guarantee fails compilation.

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