• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facet-rs / facet / 19803020611

30 Nov 2025 06:23PM UTC coverage: 57.923% (-3.0%) from 60.927%
19803020611

push

github

fasterthanlime
Convert facet-kdl to use define_attr_grammar!

- Replace ~140 lines of hand-written macros with concise grammar DSL
- Enhance make_parse_attr.rs grammar compiler:
  - Add `ns "kdl";` syntax for namespace declaration
  - Add to_snake_case() helper (NodeName → node_name)
  - Generate __attr! macro with proper ExtensionAttr return
  - Use () data for unit variants, full dispatch for complex ones
  - Add #[repr(u8)] to generated enums for Facet derive
  - Use HashMap for O(1) struct lookup
- Update design diagrams with namespace fixes

50 of 53 new or added lines in 1 file covered. (94.34%)

3583 existing lines in 40 files now uncovered.

18788 of 32436 relevant lines covered (57.92%)

179.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.25
/facet-reflect/src/partial/partial_api/ptr.rs
1
use super::*;
2

3
////////////////////////////////////////////////////////////////////////////////////////////////////
4
// Smart pointers
5
////////////////////////////////////////////////////////////////////////////////////////////////////
6
impl Partial<'_> {
7
    /// Pushes a frame to initialize the inner value of a smart pointer (`Box<T>`, `Arc<T>`, etc.)
8
    pub fn begin_smart_ptr(mut self) -> Result<Self, ReflectError> {
51✔
9
        crate::trace!("begin_smart_ptr()");
10

11
        // Check that we have a SmartPointer and get necessary data
12
        let (smart_ptr_def, pointee_shape) = {
51✔
13
            let frame = self.frames().last().unwrap();
51✔
14

15
            match &frame.shape.def {
51✔
16
                Def::Pointer(smart_ptr_def) if smart_ptr_def.constructible_from_pointee() => {
51✔
17
                    let pointee_shape = match smart_ptr_def.pointee() {
51✔
18
                        Some(shape) => shape,
51✔
19
                        None => {
UNCOV
20
                            return Err(ReflectError::OperationFailed {
×
21
                                shape: frame.shape,
×
22
                                operation: "Smart pointer must have a pointee shape",
×
23
                            });
×
24
                        }
25
                    };
26
                    (*smart_ptr_def, pointee_shape)
51✔
27
                }
28
                _ => {
UNCOV
29
                    return Err(ReflectError::OperationFailed {
×
UNCOV
30
                        shape: frame.shape,
×
UNCOV
31
                        operation: "push_smart_ptr can only be called on compatible types",
×
UNCOV
32
                    });
×
33
                }
34
            }
35
        };
36

37
        // Handle re-initialization if the smart pointer is already initialized
38
        self.prepare_for_reinitialization();
51✔
39

40
        let frame = self.frames_mut().last_mut().unwrap();
51✔
41

42
        if pointee_shape.layout.sized_layout().is_ok() {
51✔
43
            // pointee is sized, we can allocate it — for `Arc<T>` we'll be allocating a `T` and
44
            // holding onto it. We'll build a new Arc with it when ending the smart pointer frame.
45

46
            frame.tracker = Tracker::SmartPointer;
27✔
47

48
            let inner_layout = match pointee_shape.layout.sized_layout() {
27✔
49
                Ok(layout) => layout,
27✔
50
                Err(_) => {
UNCOV
51
                    return Err(ReflectError::Unsized {
×
52
                        shape: pointee_shape,
×
53
                        operation: "begin_smart_ptr, calculating inner value layout",
×
54
                    });
×
55
                }
56
            };
57
            let inner_ptr: *mut u8 = unsafe { ::alloc::alloc::alloc(inner_layout) };
27✔
58
            let Some(inner_ptr) = NonNull::new(inner_ptr) else {
27✔
UNCOV
59
                return Err(ReflectError::OperationFailed {
×
60
                    shape: frame.shape,
×
61
                    operation: "failed to allocate memory for smart pointer inner value",
×
62
                });
×
63
            };
64

65
            // Push a new frame for the inner value
66
            self.frames_mut().push(Frame::new(
27✔
67
                PtrUninit::new(inner_ptr),
27✔
68
                pointee_shape,
27✔
69
                FrameOwnership::Owned,
27✔
70
            ));
71
        } else {
72
            // pointee is unsized, we only support a handful of cases there
73
            if pointee_shape == str::SHAPE {
24✔
74
                crate::trace!("Pointee is str");
75

76
                // Allocate space for a String
77
                let string_layout = String::SHAPE
11✔
78
                    .layout
11✔
79
                    .sized_layout()
11✔
80
                    .expect("String must have a sized layout");
11✔
81
                let string_ptr: *mut u8 = unsafe { ::alloc::alloc::alloc(string_layout) };
11✔
82
                let Some(string_ptr) = NonNull::new(string_ptr) else {
11✔
UNCOV
83
                    return Err(ReflectError::OperationFailed {
×
84
                        shape: frame.shape,
×
85
                        operation: "failed to allocate memory for string",
×
86
                    });
×
87
                };
88
                let frame = Frame::new(
11✔
89
                    PtrUninit::new(string_ptr),
11✔
90
                    String::SHAPE,
91
                    FrameOwnership::Owned,
11✔
92
                );
93
                // Frame::new already sets tracker = Scalar and is_init = false
94
                self.frames_mut().push(frame);
11✔
95
            } else if let Type::Sequence(SequenceType::Slice(_st)) = pointee_shape.ty {
13✔
96
                crate::trace!("Pointee is [{}]", _st.t);
97

98
                // Get the slice builder vtable
99
                let slice_builder_vtable = smart_ptr_def.vtable.slice_builder_vtable.ok_or(
13✔
100
                    ReflectError::OperationFailed {
13✔
101
                        shape: frame.shape,
13✔
102
                        operation: "smart pointer does not support slice building",
13✔
103
                    },
13✔
UNCOV
104
                )?;
×
105

106
                // Create a new builder
107
                let builder_ptr = (slice_builder_vtable.new_fn)();
13✔
108

109
                // Deallocate the original Arc allocation before replacing with slice builder
110
                if let FrameOwnership::Owned = frame.ownership {
13✔
111
                    if let Ok(layout) = frame.shape.layout.sized_layout() {
13✔
112
                        if layout.size() > 0 {
13✔
113
                            unsafe {
13✔
114
                                ::alloc::alloc::dealloc(frame.data.as_mut_byte_ptr(), layout)
13✔
115
                            };
13✔
116
                        }
13✔
UNCOV
117
                    }
×
118
                }
×
119

120
                // Update the current frame to use the slice builder
121
                frame.data = builder_ptr.as_uninit();
13✔
122
                frame.tracker = Tracker::SmartPointerSlice {
13✔
123
                    vtable: slice_builder_vtable,
13✔
124
                    building_item: false,
13✔
125
                };
13✔
126
                // The slice builder memory is managed by the vtable, not by us
127
                frame.ownership = FrameOwnership::ManagedElsewhere;
13✔
128
            } else {
UNCOV
129
                return Err(ReflectError::OperationFailed {
×
130
                    shape: frame.shape,
×
131
                    operation: "push_smart_ptr can only be called on pointers to supported pointee types",
×
132
                });
×
133
            }
134
        }
135

136
        Ok(self)
51✔
137
    }
51✔
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc