facet_reflect/partial/partial_api/
build.rs1use super::*;
2
3impl<'facet, const BORROW: bool> Partial<'facet, BORROW> {
7 pub fn build(mut self) -> Result<HeapValue<'facet, BORROW>, ReflectError> {
9 if self.frames().len() != 1 {
10 return Err(ReflectError::InvariantViolation {
11 invariant: "Partial::build() expects a single frame — call end() until that's the case",
12 });
13 }
14
15 let frame = self.frames_mut().pop().unwrap();
16
17 if let Err(e) = frame.require_full_initialization() {
19 self.frames_mut().push(frame);
21 return Err(e);
22 }
23
24 let value_ptr = unsafe { frame.data.assume_init().as_const() };
27 if let Some(result) = unsafe { frame.allocated.shape().call_invariants(value_ptr) } {
28 match result {
29 Ok(()) => {
30 }
32 Err(message) => {
33 let shape = frame.allocated.shape();
35 self.frames_mut().push(frame);
36 return Err(ReflectError::UserInvariantFailed { message, shape });
37 }
38 }
39 }
40
41 self.state = PartialState::Built;
43
44 match frame
45 .allocated
46 .shape()
47 .layout
48 .sized_layout()
49 .map_err(|_layout_err| ReflectError::Unsized {
50 shape: frame.allocated.shape(),
51 operation: "build (final check for sized layout)",
52 }) {
53 Ok(layout) => {
54 let should_dealloc = frame.ownership.needs_dealloc();
56
57 Ok(HeapValue {
58 guard: Some(Guard {
59 ptr: unsafe { NonNull::new_unchecked(frame.data.as_mut_byte_ptr()) },
60 layout,
61 should_dealloc,
62 }),
63 shape: frame.allocated.shape(),
64 phantom: PhantomData,
65 })
66 }
67 Err(e) => {
68 self.frames_mut().push(frame);
70 Err(e)
71 }
72 }
73 }
74}