Skip to main content

facet_reflect/poke/
pointer.rs

1use facet_core::PointerDef;
2
3use super::Poke;
4
5/// Poke-side wrapper over a pointer value, for symmetry with [`PeekPointer`](crate::PeekPointer).
6///
7/// The current [`PointerVTable`](facet_core::PointerVTable) exposes no mutating operations
8/// on the pointee, so this type is effectively read-only: it offers
9/// [`borrow_inner`](Self::borrow_inner) (returning a [`Peek`](crate::Peek)) plus conversions
10/// back to [`Poke`] / [`PeekPointer`](crate::PeekPointer). Mutating methods will be added
11/// once the core vtable grows the corresponding hooks.
12pub struct PokePointer<'mem, 'facet> {
13    pub(crate) value: Poke<'mem, 'facet>,
14    pub(crate) def: PointerDef,
15}
16
17impl<'mem, 'facet> core::fmt::Debug for PokePointer<'mem, 'facet> {
18    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19        f.debug_struct("PokePointer").finish_non_exhaustive()
20    }
21}
22
23impl<'mem, 'facet> PokePointer<'mem, 'facet> {
24    /// Returns a reference to the pointer definition.
25    #[must_use]
26    #[inline]
27    pub const fn def(&self) -> &PointerDef {
28        &self.def
29    }
30
31    /// Borrows the inner value of the pointer as a read-only `Peek`.
32    ///
33    /// Returns `None` if the pointer doesn't have a borrow function or pointee shape.
34    #[inline]
35    pub fn borrow_inner(&self) -> Option<crate::Peek<'_, 'facet>> {
36        let borrow_fn = self.def.vtable.borrow_fn?;
37        let pointee_shape = self.def.pointee()?;
38        let inner_ptr = unsafe { borrow_fn(self.value.data()) };
39        Some(unsafe { crate::Peek::unchecked_new(inner_ptr, pointee_shape) })
40    }
41
42    /// Converts this back into the underlying `Poke`.
43    #[inline]
44    pub const fn into_inner(self) -> Poke<'mem, 'facet> {
45        self.value
46    }
47
48    /// Returns a read-only `PeekPointer` view.
49    #[inline]
50    pub fn as_peek_pointer(&self) -> crate::PeekPointer<'_, 'facet> {
51        crate::PeekPointer {
52            value: self.value.as_peek(),
53            def: self.def,
54        }
55    }
56}