facet_reflect/peek/
option.rs

1use facet_core::{OptionDef, OptionVTable};
2
3/// Lets you read from an option (implements read-only option operations)
4#[derive(Clone, Copy)]
5pub struct PeekOption<'mem> {
6    /// the underlying value
7    pub(crate) value: crate::Peek<'mem>,
8
9    /// the definition of the option
10    pub(crate) def: OptionDef,
11}
12
13impl<'mem> PeekOption<'mem> {
14    /// Returns the option definition
15    #[inline(always)]
16    pub fn def(self) -> OptionDef {
17        self.def
18    }
19
20    /// Returns the option vtable
21    #[inline(always)]
22    pub fn vtable(self) -> &'static OptionVTable {
23        self.def.vtable
24    }
25
26    /// Returns whether the option is Some
27    #[inline]
28    pub fn is_some(self) -> bool {
29        unsafe { (self.vtable().is_some_fn)(self.value.data()) }
30    }
31
32    /// Returns whether the option is None
33    #[inline]
34    pub fn is_none(self) -> bool {
35        !self.is_some()
36    }
37
38    /// Returns the inner value as a Peek if the option is Some, None otherwise
39    pub fn value(self) -> Option<crate::Peek<'mem>> {
40        unsafe {
41            (self.vtable().get_value_fn)(self.value.data()).map(|inner_data| crate::Peek {
42                data: inner_data,
43                shape: self.def.t(),
44            })
45        }
46    }
47}