1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::;
/// A sealed numeric ABI used by generated operation shims.
///
/// The implementations are `i32`, `u32`, `i64`, `f32`, and `f64`.
/// [`GcValue::Abi`] selects one of them; it does not use Rust's in-memory
/// representation of a field type.
abi!;
abi!;
abi!;
abi!;
abi!;
/// A value storable in GC fields and arrays and passable across Wasm boundaries.
///
/// Implementations describe a Wasm storage kind and convert between Rust values
/// and the numeric ABI used by generated shims. The postprocessor replaces the
/// shims with GC instructions and host adapters.
///
/// The built-in scalar implementations are `i8`, `u8`, `i16`, `u16`, `i32`,
/// `u32`, `i64`, `u64`, `f32`, `f64`, and `bool`. Rooted values use [`crate::Gc<T>`],
/// `Option<Gc<T>>`, or [`crate::ExternRef`]. The [`crate::GcArray<T>`] alias is
/// also a supported reference value. A struct's Rust construction data
/// implements [`crate::GcStruct`]; its rooted reference implements `GcValue`.
///
/// Custom types can implement this trait by lowering to an existing storage kind.
/// GC storage cannot run Rust destructors or retain an ordinary Rust allocation
/// merely by storing its address.
///
/// # Safety
///
/// An implementation must uphold all of the following:
///
/// - `KIND` and `Abi` agree on storage and calling convention. Packed integers,
/// 32-bit integers, and booleans use `i32` or `u32`; 64-bit integers use `i64`;
/// floats use their corresponding numeric type. References use `u32` roots
/// from the correct table, with the declared target schema and nullability.
/// - [`to_abi`](Self::to_abi) and [`setter_abi`](Self::setter_abi) produce valid
/// borrowed representations. Keeping the source value alive must keep every
/// required root or resource alive until the generated operation finishes.
/// These conversions must not consume a root still owned by their source.
/// - [`into_abi`](Self::into_abi) consumes the value. For a reference it must
/// transfer one live root with exclusive ownership, without releasing it.
/// The recipient can then decode the representation exactly once.
/// - [`from_abi`](Self::from_abi) accepts every value obtainable from the declared
/// storage or function boundary, including storage defaults and values not
/// produced by this implementation's encoding methods. It takes ownership of
/// a transferred reference root exactly once.
/// - Stored values remain valid after construction fields and setter values
/// are dropped. Nullable GC references encode null as zero. External references
/// use a nonzero owned slot even when their host value is null.
///
/// # Examples
///
/// A numeric wrapper can reuse `f64` storage without changing its Rust API.
/// Compile for a Wasm target and postprocess before execution.
///
/// ```no_run
/// use wasmgc::{Gc, GcValue, __private::FieldKind, wasmgc};
///
/// struct Distance(f64);
///
/// // SAFETY: Distance accepts every f64; its conversions use F64 storage and
/// // carry no roots or other resources across the numeric ABI.
/// unsafe impl GcValue for Distance {
/// type Abi = f64;
/// type SetterValue<'a> = Self;
/// const KIND: FieldKind = FieldKind::F64;
///
/// fn to_abi(&self) -> f64 { self.0 }
///
/// fn setter_abi(value: &Self) -> f64 { value.0 }
///
/// fn into_abi(self) -> f64 { self.0 }
///
/// unsafe fn from_abi(value: f64) -> Self { Self(value) }
/// }
///
/// #[wasmgc]
/// struct Position {
/// distance: Distance,
/// }
///
/// let position = Gc::new(Position { distance: Distance(1.0) });
/// position.set_distance(Distance(2.5));
/// assert_eq!(position.distance().0, 2.5);
/// ```
pub unsafe
scalar!;
scalar!;
scalar!;
scalar!;
scalar!;
scalar!;
scalar!;
scalar!;
scalar!;
scalar!;
// SAFETY: BOOL uses the i32 ABI. Encoding emits 0 or 1, and decoding maps
// every i32 to a valid bool without reinterpreting its Rust memory representation.
unsafe