compose_library/foundations/
boxed.rs1use crate::gc::HeapRef;
2use crate::UntypedRef;
3use compose_library::gc::Heap;
4use compose_library::vm::Vm;
5use compose_library::Value;
6use compose_macros::func;
7use compose_macros::{scope, ty};
8use std::fmt::{Debug, Display, Formatter};
9
10#[ty(scope, cast, name = "box")]
11#[derive(Clone, Debug, PartialEq)]
12pub struct Boxed(HeapRef<Value>);
13
14impl Boxed {
15 pub fn get<'a>(&self, heap: &'a Heap) -> Option<&'a Value> {
16 heap.get(self.0)
17 }
18
19 pub fn get_mut<'a>(&self, heap: &'a mut Heap) -> Option<&'a mut Value> {
20 heap.get_mut(self.0)
21 }
22
23 pub fn key(&self) -> UntypedRef {
24 self.0.key()
25 }
26}
27
28#[scope]
29impl Boxed {
30 #[func]
31 pub fn new(vm: &mut dyn Vm, value: Value) -> Self {
32 Self(vm.heap_mut().alloc(value))
33 }
34
35 #[func]
36 pub fn shallow_clone(&self, vm: &mut dyn Vm) -> Self {
37 let inner = self.0.get_unwrap(vm.heap()).clone();
38
39 Self(vm.heap_mut().alloc(inner))
40 }
41}
42
43impl Display for Boxed {
44 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45 write!(f, "box({:?})", self.0)
46 }
47}