facet_reflect/partial/partial_api/
build.rs

1use super::*;
2
3////////////////////////////////////////////////////////////////////////////////////////////////////
4// Build
5////////////////////////////////////////////////////////////////////////////////////////////////////
6impl<'facet, const BORROW: bool> Partial<'facet, BORROW> {
7    /// Builds the value, consuming the Partial.
8    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        // Check initialization before proceeding
18        if let Err(e) = frame.require_full_initialization() {
19            // Put the frame back so Drop can handle cleanup properly
20            self.frames_mut().push(frame);
21            return Err(e);
22        }
23
24        // Check invariants if present
25        // Safety: The value is fully initialized at this point (we just checked with require_full_initialization)
26        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                    // Invariants passed
31                }
32                Err(message) => {
33                    // Put the frame back so Drop can handle cleanup properly
34                    let shape = frame.allocated.shape();
35                    self.frames_mut().push(frame);
36                    return Err(ReflectError::UserInvariantFailed { message, shape });
37                }
38            }
39        }
40
41        // Mark as built to prevent Drop from cleaning up the value
42        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                // Determine if we should deallocate based on ownership
55                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                // Put the frame back for proper cleanup
69                self.frames_mut().push(frame);
70                Err(e)
71            }
72        }
73    }
74}