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
use core::ptr::NonNull;
use facet_core::{FieldError, Opaque, OpaqueConst, OpaqueUninit, Shape, StructDef};
use super::{Guard, ISet, PokeValueUninit};
/// Allows poking a struct (setting fields, etc.)
pub struct PokeStruct<'mem> {
data: OpaqueUninit<'mem>,
shape: &'static Shape,
def: StructDef,
iset: ISet,
}
impl<'mem> PokeStruct<'mem> {
#[inline(always)]
/// Coerce back into a `PokeValue`
pub fn into_value(self) -> PokeValueUninit<'mem> {
unsafe { PokeValueUninit::new(self.data, self.shape) }
}
/// Shape getter
#[inline(always)]
pub fn shape(&self) -> &'static Shape {
self.shape
}
/// Creates a new PokeStruct
///
/// # Safety
///
/// The `data`, `shape`, and `def` must match
pub unsafe fn new(data: OpaqueUninit<'mem>, shape: &'static Shape, def: StructDef) -> Self {
Self {
data,
iset: Default::default(),
shape,
def,
}
}
/// Checks if all fields in the struct have been initialized.
/// Panics if any field is not initialized, providing details about the uninitialized field.
pub fn assert_all_fields_initialized(&self) {
for (i, field) in self.def.fields.iter().enumerate() {
if !self.iset.has(i) {
panic!(
"Field '{}' was not initialized. Complete schema:\n{:?}",
field.name, self.shape
);
}
}
}
/// Asserts that every field has been initialized and forgets the PokeStruct.
///
/// This method is only used when the origin is borrowed.
/// If this method is not called, all fields will be freed when the PokeStruct is dropped.
///
/// # Panics
///
/// This function will panic if any field is not initialized.
pub fn build_in_place(self) -> Opaque<'mem> {
// ensure all fields are initialized
self.assert_all_fields_initialized();
let data = unsafe { self.data.assume_init() };
// prevent field drops when the PokeStruct is dropped
core::mem::forget(self);
data
}
/// Builds a value of type `T` from the PokeStruct, then deallocates the memory
/// that this PokeStruct was pointing to.
///
/// # Panics
///
/// This function will panic if:
/// - Not all the fields have been initialized.
/// - The generic type parameter T does not match the shape that this PokeStruct is building.
pub fn build<T: crate::Facet>(self, guard: Option<Guard>) -> T {
let mut guard = guard;
let this = self;
// this changes drop order: guard must be dropped _after_ this.
this.assert_all_fields_initialized();
this.shape.assert_type::<T>();
if let Some(guard) = &guard {
guard.shape.assert_type::<T>();
}
let result = unsafe {
let ptr = this.data.as_mut_bytes() as *const T;
core::ptr::read(ptr)
};
guard.take(); // dealloc
core::mem::forget(this);
result
}
/// Build that PokeStruct into a boxed completed shape.
///
/// # Panics
///
/// This function will panic if:
/// - Not all the fields have been initialized.
/// - The generic type parameter T does not match the shape that this PokeStruct is building.
pub fn build_boxed<T: crate::Facet>(self) -> alloc::boxed::Box<T> {
self.assert_all_fields_initialized();
self.shape.assert_type::<T>();
let boxed = unsafe { alloc::boxed::Box::from_raw(self.data.as_mut_bytes() as *mut T) };
core::mem::forget(self);
boxed
}
/// Moves the contents of this `PokeStruct` into a target memory location.
///
/// # Safety
///
/// The target pointer must be valid and properly aligned,
/// and must be large enough to hold the value.
/// The caller is responsible for ensuring that the target memory is properly deallocated
/// when it's no longer needed.
pub unsafe fn move_into(self, target: NonNull<u8>, guard: Option<Guard>) {
self.assert_all_fields_initialized();
if let Some(guard) = &guard {
guard.shape.assert_shape(self.shape);
}
unsafe {
core::ptr::copy_nonoverlapping(
self.data.as_mut_bytes(),
target.as_ptr(),
self.shape.layout.size(),
);
}
core::mem::forget(self);
}
/// Gets a field, by name
pub fn field_by_name(
&self,
name: &str,
) -> Result<(usize, crate::PokeUninit<'mem>), FieldError> {
let index = self
.def
.fields
.iter()
.position(|f| f.name == name)
.ok_or(FieldError::NoSuchStaticField)?;
Ok((index, self.field(index)?))
}
/// Get a field writer for a field by index.
///
/// # Errors
///
/// Returns an error if:
/// - The shape doesn't represent a struct.
/// - The index is out of bounds.
pub fn field(&self, index: usize) -> Result<crate::PokeUninit<'mem>, FieldError> {
if index >= self.def.fields.len() {
return Err(FieldError::IndexOutOfBounds);
}
let field = &self.def.fields[index];
// Get the field's address
let field_addr = unsafe { self.data.field_uninit(field.offset) };
let field_shape = field.shape;
let poke = unsafe { crate::PokeUninit::unchecked_new(field_addr, field_shape) };
Ok(poke)
}
/// Sets a field's value by its index, directly copying raw memory.
///
/// # Safety
///
/// This is unsafe because it directly copies memory without checking types.
/// The caller must ensure that `value` is of the correct type for the field.
///
/// # Errors
///
/// Returns an error if:
/// - The index is out of bounds
/// - The field shapes don't match
pub unsafe fn unchecked_set(
&mut self,
index: usize,
value: OpaqueConst,
) -> Result<(), FieldError> {
if index >= self.def.fields.len() {
return Err(FieldError::IndexOutOfBounds);
}
let field = &self.def.fields[index];
let field_shape = field.shape;
unsafe {
core::ptr::copy_nonoverlapping(
value.as_ptr(),
self.data.field_uninit(field.offset).as_mut_bytes(),
field_shape.layout.size(),
);
self.iset.set(index);
}
Ok(())
}
/// Sets a field's value by its name, directly copying raw memory.
///
/// # Safety
///
/// This is unsafe because it directly copies memory without checking types.
/// The caller must ensure that `value` is of the correct type for the field.
///
/// # Errors
///
/// Returns an error if:
/// - The field name doesn't exist
/// - The field shapes don't match
pub unsafe fn unchecked_set_by_name(
&mut self,
name: &str,
value: OpaqueConst,
) -> Result<(), FieldError> {
let index = self
.def
.fields
.iter()
.position(|f| f.name == name)
.ok_or(FieldError::NoSuchStaticField)?;
unsafe { self.unchecked_set(index, value) }
}
/// Sets a field's value by its index in a type-safe manner.
///
/// This method takes ownership of the value and ensures proper memory management.
///
/// # Errors
///
/// Returns an error if:
/// - The index is out of bounds
/// - The field shapes don't match
pub fn set<T: crate::Facet>(&mut self, index: usize, value: T) -> Result<(), FieldError> {
let field_shape = self
.def
.fields
.get(index)
.ok_or(FieldError::IndexOutOfBounds)?
.shape;
field_shape.assert_type::<T>();
unsafe {
let opaque = OpaqueConst::new(&value);
let result = self.unchecked_set(index, opaque);
if result.is_ok() {
core::mem::forget(value);
}
result
}
}
/// Sets a field's value by its name in a type-safe manner.
///
/// This method takes ownership of the value and ensures proper memory management.
///
/// # Errors
///
/// Returns an error if:
/// - The field name doesn't exist
/// - The field shapes don't match
pub fn set_by_name<T: crate::Facet>(&mut self, name: &str, value: T) -> Result<(), FieldError> {
let index = self
.def
.fields
.iter()
.position(|f| f.name == name)
.ok_or(FieldError::NoSuchStaticField)?;
self.set(index, value)
}
/// Marks a field as initialized.
///
/// # Safety
///
/// The caller must ensure that the field is initialized. Only call this after writing to
/// an address gotten through [`Self::field`] or [`Self::field_by_name`].
pub unsafe fn mark_initialized(&mut self, index: usize) {
self.iset.set(index);
}
/// Gets the struct definition
pub fn def(&self) -> StructDef {
self.def
}
}
impl Drop for PokeStruct<'_> {
fn drop(&mut self) {
self.def
.fields
.iter()
.enumerate()
.filter_map(|(i, field)| {
if self.iset.has(i) {
Some((field, field.shape.vtable.drop_in_place?))
} else {
None
}
})
.for_each(|(field, drop_fn)| unsafe {
drop_fn(self.data.field_init(field.offset));
});
}
}