facet_reflect/peek/
owned.rs

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