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