facet_reflect/peek/
owned.rs

1use crate::trace;
2use core::marker::PhantomData;
3use facet_core::{PtrMut, Shape};
4
5use super::Peek;
6
7/// An Owned version of a Peek used for custom serialization
8///
9/// Should be held onto until the serialization of the type
10/// is completed.
11pub struct OwnedPeek<'mem> {
12    pub(crate) data: PtrMut,
13    pub(crate) shape: &'static Shape,
14    pub(crate) _phantom: PhantomData<&'mem ()>,
15}
16
17impl<'mem, 'facet> OwnedPeek<'mem> {
18    /// returns the shape of the peek
19    pub fn shape(&self) -> &'static Shape {
20        self.shape
21    }
22
23    /// returns a borrowed version of the peek
24    pub fn as_peek(&'mem self) -> Peek<'mem, 'facet> {
25        unsafe { Peek::unchecked_new(self.data.as_const(), self.shape) }
26    }
27}
28
29impl<'mem> Drop for OwnedPeek<'mem> {
30    fn drop(&mut self) {
31        trace!("Dropping owned peek of shape '{}'", self.shape);
32        unsafe { self.shape.call_drop_in_place(self.data) };
33        let _ = unsafe { self.shape.deallocate_mut(self.data) };
34    }
35}