dusk_wasmtime/runtime/gc.rs
1#[cfg(feature = "gc")]
2mod enabled;
3#[cfg(feature = "gc")]
4pub use enabled::*;
5
6#[cfg(not(feature = "gc"))]
7mod disabled;
8#[cfg(not(feature = "gc"))]
9pub use disabled::*;
10
11use std::ops::Deref;
12
13/// A common trait implemented by all garbage-collected reference types.
14///
15/// This is a sealed trait, and may not be implemented for any types outside of
16/// the `wasmtime` crate.
17pub trait GcRef: GcRefImpl {}
18
19impl<T> GcRef for T where T: GcRefImpl {}
20
21/// A trait implemented for GC references that are guaranteed to be rooted:
22///
23/// * [`Rooted<T>`][crate::Rooted]
24/// * [`ManuallyRooted<T>`][crate::ManuallyRooted]
25///
26/// You can use this to abstract over the different kinds of rooted GC
27/// references. Note that `Deref<Target = T>` is a supertrait for
28/// `RootedGcRef<T>`, so all rooted GC references deref to their underlying `T`,
29/// allowing you to call its methods.
30///
31/// This is a sealed trait, and may not be implemented for any types outside of
32/// the `wasmtime` crate.
33pub trait RootedGcRef<T>: RootedGcRefImpl<T> + Deref<Target = T>
34where
35 T: GcRef,
36{
37}
38
39impl<T, U> RootedGcRef<T> for U
40where
41 T: GcRef,
42 U: RootedGcRefImpl<T> + Deref<Target = T>,
43{
44}
45
46/// An error returned when attempting to allocate a GC-managed object, but the
47/// GC heap is out of memory.
48///
49/// This error wraps an inner `T` value -- which is the host value, if any, that
50/// was passed to [`ExternRef::new`][crate::ExternRef::new] -- and you can
51/// recover this value via the
52/// [`into_inner`][crate::GcHeapOutOfMemory::into_inner] method. This lets you
53/// try to allocate the `externref` again, after performing a GC to hopefully
54/// free up space in the heap, or otherwise do whatever you want with the inner
55/// value.
56///
57/// For errors that occur when attempting to allocate non-`externref` objects
58/// when the GC heap is at capacity, the `T` type parameter is just the unit
59/// type `()`.
60pub struct GcHeapOutOfMemory<T> {
61 inner: T,
62}
63
64impl<T> std::fmt::Debug for GcHeapOutOfMemory<T> {
65 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66 std::fmt::Display::fmt(self, f)
67 }
68}
69
70impl<T> std::fmt::Display for GcHeapOutOfMemory<T> {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 write!(f, "GC heap out of memory")
73 }
74}
75
76impl<T> std::error::Error for GcHeapOutOfMemory<T> {}
77
78impl<T> GcHeapOutOfMemory<T> {
79 pub(crate) fn new(inner: T) -> Self {
80 Self { inner }
81 }
82
83 /// Recover this error's inner host value.
84 pub fn into_inner(self) -> T {
85 self.inner
86 }
87}