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