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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use facet_core::{Def, EnumRepr, EnumType, Facet, FieldError};
use facet_path::Path;
use crate::{ReflectError, ReflectErrorKind, peek::VariantError};
use super::Poke;
/// Lets you mutate an enum's fields.
pub struct PokeEnum<'mem, 'facet> {
/// The internal data storage for the enum
///
/// Note that this stores both the discriminant and the variant data
/// (if any), and the layout depends on the enum representation.
pub(crate) value: Poke<'mem, 'facet>,
/// The definition of the enum.
pub(crate) ty: EnumType,
}
impl core::fmt::Debug for PokeEnum<'_, '_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self.value)
}
}
impl<'mem, 'facet> PokeEnum<'mem, 'facet> {
fn err(&self, kind: ReflectErrorKind) -> ReflectError {
ReflectError::new(kind, Path::new(self.value.shape))
}
/// Returns the enum definition
#[inline(always)]
pub const fn ty(&self) -> EnumType {
self.ty
}
/// Returns the enum representation
#[inline(always)]
pub const fn enum_repr(&self) -> EnumRepr {
self.ty.enum_repr
}
/// Returns the enum variants
#[inline(always)]
pub const fn variants(&self) -> &'static [facet_core::Variant] {
self.ty.variants
}
/// Returns the number of variants in this enum
#[inline(always)]
pub const fn variant_count(&self) -> usize {
self.ty.variants.len()
}
/// Returns the variant name at the given index
#[inline(always)]
pub fn variant_name(&self, index: usize) -> Option<&'static str> {
self.ty.variants.get(index).map(|variant| variant.name)
}
/// Returns the discriminant value for the current enum value
///
/// Note: For `RustNPO` (null pointer optimization) types, there is no explicit
/// discriminant stored in memory. In this case, 0 is returned. Use
/// [`variant_index()`](Self::variant_index) to determine the active variant for NPO types.
#[inline]
pub fn discriminant(&self) -> i64 {
// Read the discriminant based on the enum representation
match self.ty.enum_repr {
EnumRepr::Rust => {
panic!("cannot read discriminant from Rust enum with unspecified layout")
}
// For RustNPO types, there is no explicit discriminant stored in memory.
// The variant is determined by niche optimization (e.g., null pointer pattern).
// Return 0 since that's the declared discriminant for NPO variants.
// This also prevents UB when reading from zero-sized types.
EnumRepr::RustNPO => 0,
EnumRepr::U8 => unsafe { self.value.data().read::<u8>() as i64 },
EnumRepr::U16 => unsafe { self.value.data().read::<u16>() as i64 },
EnumRepr::U32 => unsafe { self.value.data().read::<u32>() as i64 },
EnumRepr::U64 => unsafe { self.value.data().read::<u64>() as i64 },
EnumRepr::USize => unsafe { self.value.data().read::<usize>() as i64 },
EnumRepr::I8 => unsafe { self.value.data().read::<i8>() as i64 },
EnumRepr::I16 => unsafe { self.value.data().read::<i16>() as i64 },
EnumRepr::I32 => unsafe { self.value.data().read::<i32>() as i64 },
EnumRepr::I64 => unsafe { self.value.data().read::<i64>() },
EnumRepr::ISize => unsafe { self.value.data().read::<isize>() as i64 },
}
}
/// Returns the variant index for this enum value
#[inline]
pub fn variant_index(&self) -> Result<usize, VariantError> {
// For Option<T> types, use the OptionVTable to correctly determine if the value is Some or None.
// This handles both RustNPO (niche-optimized) and Rust (non-niche) representations.
if let Def::Option(option_def) = self.value.shape.def {
let is_some = unsafe { (option_def.vtable.is_some)(self.value.data()) };
return Ok(self
.ty
.variants
.iter()
.position(|variant| {
let has_fields = !variant.data.fields.is_empty();
has_fields == is_some
})
.expect("No variant found matching Option state"));
}
if self.ty.enum_repr == EnumRepr::RustNPO {
// Fallback for other RustNPO types (e.g., Option<&T> where all-zeros means None)
let layout = self
.value
.shape
.layout
.sized_layout()
.expect("Unsized enums in NPO repr are unsupported");
let data = self.value.data();
let slice = unsafe { core::slice::from_raw_parts(data.as_byte_ptr(), layout.size()) };
let all_zero = slice.iter().all(|v| *v == 0);
Ok(self
.ty
.variants
.iter()
.position(|variant| {
// Find the maximum end bound
let mut max_offset = 0;
for field in variant.data.fields {
let offset = field.offset
+ field
.shape()
.layout
.sized_layout()
.map(|v| v.size())
.unwrap_or(0);
max_offset = core::cmp::max(max_offset, offset);
}
// If we are all zero, then find the enum variant that has no size,
// otherwise, the one with size.
if all_zero {
max_offset == 0
} else {
max_offset != 0
}
})
.expect("No variant found with matching discriminant"))
} else {
let discriminant = self.discriminant();
// Find the variant with matching discriminant using position method
Ok(self
.ty
.variants
.iter()
.position(|variant| variant.discriminant == Some(discriminant))
.expect("No variant found with matching discriminant"))
}
}
/// Returns the active variant
#[inline]
pub fn active_variant(&self) -> Result<&'static facet_core::Variant, VariantError> {
let index = self.variant_index()?;
Ok(&self.ty.variants[index])
}
/// Returns the name of the active variant for this enum value
#[inline]
pub fn variant_name_active(&self) -> Result<&'static str, VariantError> {
Ok(self.active_variant()?.name)
}
/// Returns a Poke handle to a field of a tuple or struct variant by index
pub fn field(&mut self, index: usize) -> Result<Option<Poke<'_, 'facet>>, VariantError> {
let variant = self.active_variant()?;
let fields = &variant.data.fields;
if index >= fields.len() {
return Ok(None);
}
let field = &fields[index];
let field_data = unsafe { self.value.data.field(field.offset) };
Ok(Some(unsafe {
Poke::from_raw_parts(field_data, field.shape())
}))
}
/// Returns the index of a field in the active variant by name
pub fn field_index(&self, field_name: &str) -> Result<Option<usize>, VariantError> {
let variant = self.active_variant()?;
Ok(variant
.data
.fields
.iter()
.position(|f| f.name == field_name))
}
/// Returns a Poke handle to a field of a tuple or struct variant by name
pub fn field_by_name(
&mut self,
field_name: &str,
) -> Result<Option<Poke<'_, 'facet>>, VariantError> {
let index_opt = self.field_index(field_name)?;
match index_opt {
Some(index) => self.field(index),
None => Ok(None),
}
}
/// Sets a field of the current variant by index.
///
/// Returns an error if:
/// - The parent enum is not POD
/// - The index is out of bounds
/// - The value type doesn't match the field type
pub fn set_field<T: Facet<'facet>>(
&mut self,
index: usize,
value: T,
) -> Result<(), ReflectError> {
// Check that the parent enum is POD before allowing field mutation
if !self.value.shape.is_pod() {
return Err(self.err(ReflectErrorKind::NotPod {
shape: self.value.shape,
}));
}
let variant = self.active_variant().map_err(|_| {
self.err(ReflectErrorKind::OperationFailed {
shape: self.value.shape,
operation: "get active variant",
})
})?;
let fields = &variant.data.fields;
let field = fields.get(index).ok_or_else(|| {
self.err(ReflectErrorKind::FieldError {
shape: self.value.shape,
field_error: FieldError::IndexOutOfBounds {
index,
bound: fields.len(),
},
})
})?;
let field_shape = field.shape();
if field_shape != T::SHAPE {
return Err(self.err(ReflectErrorKind::WrongShape {
expected: field_shape,
actual: T::SHAPE,
}));
}
unsafe {
let field_ptr = self.value.data.field(field.offset);
// Drop the old value and write the new one
field_shape.call_drop_in_place(field_ptr);
core::ptr::write(field_ptr.as_mut_byte_ptr() as *mut T, value);
}
Ok(())
}
/// Sets a field of the current variant by name.
///
/// Returns an error if:
/// - The parent enum is not POD
/// - No field with the given name exists
/// - The value type doesn't match the field type
pub fn set_field_by_name<T: Facet<'facet>>(
&mut self,
name: &str,
value: T,
) -> Result<(), ReflectError> {
let index = self.field_index(name).map_err(|_| {
self.err(ReflectErrorKind::OperationFailed {
shape: self.value.shape,
operation: "get active variant",
})
})?;
let index = index.ok_or_else(|| {
self.err(ReflectErrorKind::FieldError {
shape: self.value.shape,
field_error: FieldError::NoSuchField,
})
})?;
self.set_field(index, value)
}
/// Gets a read-only view of a field by index.
pub fn peek_field(
&self,
index: usize,
) -> Result<Option<crate::Peek<'_, 'facet>>, VariantError> {
let variant = self.active_variant()?;
let fields = &variant.data.fields;
if index >= fields.len() {
return Ok(None);
}
let field = &fields[index];
let field_data = unsafe { self.value.data.as_const().field(field.offset) };
Ok(Some(unsafe {
crate::Peek::unchecked_new(field_data, field.shape())
}))
}
/// Gets a read-only view of a field by name.
pub fn peek_field_by_name(
&self,
field_name: &str,
) -> Result<Option<crate::Peek<'_, 'facet>>, VariantError> {
let index_opt = self.field_index(field_name)?;
match index_opt {
Some(index) => self.peek_field(index),
None => Ok(None),
}
}
/// Converts this back into the underlying `Poke`.
#[inline]
pub const fn into_inner(self) -> Poke<'mem, 'facet> {
self.value
}
/// Returns a read-only `PeekEnum` view.
#[inline]
pub fn as_peek_enum(&self) -> crate::PeekEnum<'_, 'facet> {
crate::PeekEnum {
value: self.value.as_peek(),
ty: self.ty,
}
}
}