facet_reflect/peek/smartptr.rs
1use facet_core::SmartPointerDef;
2
3use super::Peek;
4
5/// Represents a smart pointer that can be peeked at during memory inspection.
6///
7/// This struct holds the value being pointed to and the definition of the smart pointer type.
8pub struct PeekSmartPointer<'mem, 'facet, 'shape> {
9 /// The value being pointed to by this smart pointer.
10 pub(crate) value: Peek<'mem, 'facet, 'shape>,
11
12 /// The definition of this smart pointer type.
13 pub(crate) def: SmartPointerDef<'shape>,
14}
15
16impl<'mem, 'facet, 'shape> PeekSmartPointer<'mem, 'facet, 'shape> {
17 /// Returns a reference to the smart pointer definition.
18 #[must_use]
19 pub fn def(&self) -> &SmartPointerDef<'shape> {
20 &self.def
21 }
22
23 /// Borrows the inner value of the smart pointer.
24 ///
25 /// Returns `None` if the smart pointer doesn't have a borrow function or pointee shape.
26 pub fn borrow_inner(&self) -> Option<Peek<'mem, 'facet, 'shape>> {
27 let borrow_fn = self.def.vtable.borrow_fn?;
28 let pointee_shape = self.def.pointee()?;
29
30 // SAFETY: We have a valid smart pointer and borrow_fn is provided by the vtable
31 let inner_ptr = unsafe { borrow_fn(self.value.data.thin().unwrap()) };
32
33 // SAFETY: The borrow_fn returns a valid pointer to the inner value with the correct shape
34 let inner_peek = unsafe { Peek::unchecked_new(inner_ptr, pointee_shape) };
35
36 Some(inner_peek)
37 }
38}