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

facet-rs / facet / 19992174439

06 Dec 2025 05:56PM UTC coverage: 58.742% (-0.005%) from 58.747%
19992174439

Pull #1118

github

web-flow
Merge d1d251ac8 into 45a8cb1c3
Pull Request #1118: Reduce/cordon bloat in facet, introduce bloatbench

1138 of 3103 new or added lines in 61 files covered. (36.67%)

540 existing lines in 30 files now uncovered.

24225 of 41240 relevant lines covered (58.74%)

502.5 hits per line

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

14.0
/facet-core/src/types/def/pointer.rs
1
use bitflags::bitflags;
2

3
use crate::{PtrConst, PtrMut, PtrUninit};
4

5
use super::Shape;
6

7
/// Describes a pointer — including a vtable to query and alter its state,
8
/// and the inner shape (the pointee type in the pointer).
9
#[derive(Clone, Copy, Debug)]
10
#[repr(C)]
11
pub struct PointerDef {
12
    /// vtable for interacting with the pointer
13
    pub vtable: &'static PointerVTable,
14

15
    /// shape of the inner type of the pointer, if not opaque
16
    pub pointee: Option<&'static Shape>,
17

18
    /// shape of the corresponding strong pointer, if this pointer is weak
19
    ///
20
    /// the layer of indirection is to break the strong <-> weak reference cycle,
21
    /// since consts may not have cycles in their definitions.
22
    pub weak: Option<fn() -> &'static Shape>,
23

24
    /// shape of the corresponding weak pointer, if this pointer is strong
25
    pub strong: Option<&'static Shape>,
26

27
    /// Flags representing various characteristics of the pointer
28
    pub flags: PointerFlags,
29

30
    /// An optional field to identify the kind of pointer
31
    pub known: Option<KnownPointer>,
32
}
33

34
impl PointerDef {
35
    /// Returns shape of the inner type of the pointer, if not opaque
36
    pub fn pointee(&self) -> Option<&'static Shape> {
292✔
37
        self.pointee
292✔
38
    }
292✔
39

40
    /// Returns shape of the corresponding strong pointer, if this pointer is weak
41
    pub fn weak(&self) -> Option<&'static Shape> {
×
42
        self.weak.map(|f| f())
×
43
    }
×
44

45
    /// Returns shape of the corresponding weak pointer, if this pointer is strong
46
    pub fn strong(&self) -> Option<&'static Shape> {
×
47
        self.strong
×
48
    }
×
49

50
    /// Whether a new pointer can be constructed from an owned value of its pointee type.
51
    pub const fn constructible_from_pointee(&self) -> bool {
51✔
52
        self.vtable.new_into_fn.is_some()
51✔
53
            || matches!(
×
54
                self.known,
24✔
55
                Some(KnownPointer::Box | KnownPointer::Rc | KnownPointer::Arc)
56
            )
57
    }
51✔
58
}
59

60
bitflags! {
61
    /// Flags to represent various characteristics of pointers
62
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
63
    pub struct PointerFlags: u8 {
64
        /// An empty set of flags
65
        const EMPTY = 0;
66

67
        /// Whether the pointer is weak (like [`std::sync::Weak`])
68
        const WEAK = 1 << 0;
69
        /// Whether the pointer is atomic (like [`std::sync::Arc`])
70
        const ATOMIC = 1 << 1;
71
        /// Whether the pointer is a lock (like [`std::sync::Mutex`])
72
        const LOCK = 1 << 2;
73
    }
74
}
75

76
/// Tries to upgrade the weak pointer to a strong one.
77
///
78
/// If the upgrade succeeds, initializes the pointer into the given `strong`, and returns a
79
/// copy of `strong`, which has been guaranteed to be initialized. If the upgrade fails, `None` is
80
/// returned and `strong` is not initialized.
81
///
82
/// `weak` is not moved out of.
83
///
84
/// # Safety
85
///
86
/// `weak` must be a valid weak pointer (like [`alloc::sync::Weak`] or [`alloc::rc::Weak`]).
87
///
88
/// `strong` must be allocated, and of the right layout for the corresponding pointer.
89
///
90
/// `strong` must not have been initialized yet.
91
pub type UpgradeIntoFn =
92
    for<'ptr> unsafe fn(weak: PtrMut<'ptr>, strong: PtrUninit<'ptr>) -> Option<PtrMut<'ptr>>;
93

94
/// Downgrades a strong pointer to a weak one.
95
///
96
/// Initializes the pointer into the given `weak`, and returns a copy of `weak`, which has
97
/// been guaranteed to be initialized.
98
///
99
/// Only strong pointers can be downgraded (like [`alloc::sync::Arc`] or [`alloc::rc::Rc`]).
100
///
101
/// # Safety
102
///
103
/// `strong` must be a valid strong pointer (like [`alloc::sync::Arc`] or [`alloc::rc::Rc`]).
104
///
105
/// `weak` must be allocated, and of the right layout for the corresponding weak pointer.
106
///
107
/// `weak` must not have been initialized yet.
108
pub type DowngradeIntoFn =
109
    for<'ptr> unsafe fn(strong: PtrMut<'ptr>, weak: PtrUninit<'ptr>) -> PtrMut<'ptr>;
110

111
/// Tries to obtain a reference to the inner value of the pointer.
112
///
113
/// This can only be used with strong pointers (like [`alloc::sync::Arc`] or [`alloc::rc::Rc`]).
114
///
115
/// # Safety
116
///
117
/// `this` must be a valid strong pointer (like [`alloc::sync::Arc`] or [`alloc::rc::Rc`]).
118
pub type BorrowFn = for<'ptr> unsafe fn(this: PtrConst<'ptr>) -> PtrConst<'ptr>;
119

120
/// Creates a new pointer wrapping the given value.
121
///
122
/// Initializes the pointer into the given `this`, and returns a copy of `this`, which has
123
/// been guaranteed to be initialized.
124
///
125
/// This can only be used with strong pointers (like [`alloc::sync::Arc`] or [`alloc::rc::Rc`]).
126
///
127
/// # Safety
128
///
129
/// `this` must be allocated, and of the right layout for the corresponding pointer.
130
///
131
/// `this` must not have been initialized yet.
132
///
133
/// `ptr` must point to a value of type `T`.
134
///
135
/// `ptr` is moved out of (with [`core::ptr::read`]) — it should be deallocated afterwards (e.g.
136
/// with [`core::mem::forget`]) but NOT dropped).
137
pub type NewIntoFn = for<'ptr> unsafe fn(this: PtrUninit<'ptr>, ptr: PtrMut<'ptr>) -> PtrMut<'ptr>;
138

139
/// Type-erased result of locking a mutex-like pointer
140
pub struct LockResult<'ptr> {
141
    /// The data that was locked
142
    data: PtrMut<'ptr>,
143
    /// The guard that protects the data
144
    guard: PtrConst<'ptr>,
145
    /// The vtable for the guard
146
    guard_vtable: &'static LockGuardVTable,
147
}
148

149
impl<'ptr> LockResult<'ptr> {
150
    /// Returns a reference to the locked data
151
    #[must_use]
152
    pub fn data(&self) -> &PtrMut<'ptr> {
×
153
        &self.data
×
154
    }
×
155
}
156

157
impl Drop for LockResult<'_> {
158
    fn drop(&mut self) {
×
159
        unsafe {
×
160
            (self.guard_vtable.drop_in_place)(self.guard);
×
161
        }
×
162
    }
×
163
}
164

165
/// Functions for manipulating a guard
166
pub struct LockGuardVTable {
167
    /// Drops the guard in place
168
    pub drop_in_place: for<'ptr> unsafe fn(guard: PtrConst<'ptr>),
169
}
170

171
/// Acquires a lock on a mutex-like pointer
172
pub type LockFn = for<'ptr> unsafe fn(opaque: PtrConst<'ptr>) -> Result<LockResult<'ptr>, ()>;
173

174
/// Acquires a read lock on a reader-writer lock-like pointer
175
pub type ReadFn = for<'ptr> unsafe fn(opaque: PtrConst<'ptr>) -> Result<LockResult<'ptr>, ()>;
176

177
/// Acquires a write lock on a reader-writer lock-like pointer
178
pub type WriteFn = for<'ptr> unsafe fn(opaque: PtrConst<'ptr>) -> Result<LockResult<'ptr>, ()>;
179

180
/// Creates a new slice builder for a pointer: ie. for `Arc<[u8]>`, it builds a
181
/// `Vec<u8>` internally, to which you can push, and then turn into an `Arc<[u8]>` at
182
/// the last stage.
183
///
184
/// This works for any `U` in `Arc<[U]>` because those have separate concrete implementations, and
185
/// thus, separate concrete vtables.
186
pub type SliceBuilderNewFn = fn() -> PtrMut<'static>;
187

188
/// Pushes a value into a slice builder.
189
///
190
/// # Safety
191
///
192
/// Item must point to a valid value of type `U` and must be initialized.
193
/// Function is infallible as it uses the global allocator.
194
pub type SliceBuilderPushFn = unsafe fn(builder: PtrMut, item: PtrMut);
195

196
/// Converts a slice builder into a pointer. This takes ownership of the builder
197
/// and frees the backing storage.
198
///
199
/// # Safety
200
///
201
/// The builder must be valid and must not be used after this function is called.
202
/// Function is infallible as it uses the global allocator.
203
pub type SliceBuilderConvertFn = unsafe fn(builder: PtrMut<'static>) -> PtrConst<'static>;
204

205
/// Frees a slice builder without converting it into a pointer
206
///
207
/// # Safety
208
///
209
/// The builder must be valid and must not be used after this function is called.
210
pub type SliceBuilderFreeFn = unsafe fn(builder: PtrMut<'static>);
211

212
/// Functions for creating and manipulating slice builders.
213
#[derive(Debug, Clone, Copy)]
214
pub struct SliceBuilderVTable {
215
    /// See [`SliceBuilderNewFn`]
216
    pub new_fn: SliceBuilderNewFn,
217
    /// See [`SliceBuilderPushFn`]
218
    pub push_fn: SliceBuilderPushFn,
219
    /// See [`SliceBuilderConvertFn`]
220
    pub convert_fn: SliceBuilderConvertFn,
221
    /// See [`SliceBuilderFreeFn`]
222
    pub free_fn: SliceBuilderFreeFn,
223
}
224

225
impl SliceBuilderVTable {
226
    /// Const ctor for slice builder vtable; all hooks required.
227
    #[must_use]
NEW
228
    pub const fn new(
×
NEW
229
        new_fn: SliceBuilderNewFn,
×
NEW
230
        push_fn: SliceBuilderPushFn,
×
NEW
231
        convert_fn: SliceBuilderConvertFn,
×
NEW
232
        free_fn: SliceBuilderFreeFn,
×
NEW
233
    ) -> Self {
×
234
        Self {
×
NEW
235
            new_fn,
×
NEW
236
            push_fn,
×
NEW
237
            convert_fn,
×
NEW
238
            free_fn,
×
239
        }
×
240
    }
×
241
}
242

243
/// Functions for interacting with a pointer
244
#[derive(Debug, Clone, Copy)]
245
pub struct PointerVTable {
246
    /// See [`UpgradeIntoFn`]
247
    pub upgrade_into_fn: Option<UpgradeIntoFn>,
248

249
    /// See [`DowngradeIntoFn`]
250
    pub downgrade_into_fn: Option<DowngradeIntoFn>,
251

252
    /// See [`BorrowFn`]
253
    pub borrow_fn: Option<BorrowFn>,
254

255
    /// See [`NewIntoFn`]
256
    pub new_into_fn: Option<NewIntoFn>,
257

258
    /// See [`LockFn`]
259
    pub lock_fn: Option<LockFn>,
260

261
    /// See [`ReadFn`]
262
    pub read_fn: Option<ReadFn>,
263

264
    /// See [`WriteFn`]
265
    pub write_fn: Option<WriteFn>,
266

267
    /// See [`SliceBuilderVTable`]
268
    pub slice_builder_vtable: Option<&'static SliceBuilderVTable>,
269
}
270

271
impl Default for PointerVTable {
NEW
272
    fn default() -> Self {
×
NEW
273
        Self::new()
×
UNCOV
274
    }
×
275
}
276

277
impl PointerVTable {
278
    /// Const ctor with all entries set to `None`.
279
    #[must_use]
UNCOV
280
    pub const fn new() -> Self {
×
281
        Self {
×
282
            upgrade_into_fn: None,
×
283
            downgrade_into_fn: None,
×
284
            borrow_fn: None,
×
285
            new_into_fn: None,
×
286
            lock_fn: None,
×
287
            read_fn: None,
×
288
            write_fn: None,
×
289
            slice_builder_vtable: None,
×
290
        }
×
291
    }
×
292
}
293

294
/// Represents common standard library pointer kinds
295
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
296
pub enum KnownPointer {
297
    /// [`Box<T>`](alloc::boxed::Box), heap-allocated values with single ownership
298
    Box,
299
    /// [`Rc<T>`](alloc::rc::Rc), reference-counted values with multiple ownership
300
    Rc,
301
    /// [`Weak<T>`](alloc::rc::Weak), a weak reference to an `Rc`-managed value
302
    RcWeak,
303
    /// [`Arc<T>`](alloc::sync::Arc), thread-safe reference-counted values with multiple ownership
304
    Arc,
305
    /// [`Weak<T>`](alloc::sync::Weak), a weak reference to an `Arc`-managed value
306
    ArcWeak,
307
    /// [`Cow<'a, T>`](alloc::borrow::Cow), a clone-on-write smart pointer
308
    Cow,
309
    /// [`Pin<P>`](core::pin::Pin), a type that pins values behind a pointer
310
    Pin,
311
    /// [`Cell<T>`](core::cell::Cell), a mutable memory location with interior mutability
312
    Cell,
313
    /// [`RefCell<T>`](core::cell::RefCell), a mutable memory location with dynamic borrowing rules
314
    RefCell,
315
    /// [`OnceCell<T>`](core::cell::OnceCell), a cell that can be written to only once
316
    OnceCell,
317
    /// `Mutex<T>`, a mutual exclusion primitive (requires std)
318
    Mutex,
319
    /// `RwLock<T>`, a reader-writer lock (requires std)
320
    RwLock,
321
    /// [`NonNull<T>`](core::ptr::NonNull), a wrapper around a raw pointer that is not null
322
    NonNull,
323
    /// `&T`
324
    SharedReference,
325
    /// `&mut T`
326
    ExclusiveReference,
327
}
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