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, 'facet> {
6    /// the underlying value
7    pub(crate) value: crate::Peek<'mem, 'facet>,
8
9    /// the definition of the option
10    pub(crate) def: OptionDef,
11}
12
13impl<'mem, 'facet> PeekOption<'mem, 'facet> {
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().thin().unwrap()) }
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    #[inline]
40    pub fn value(self) -> Option<crate::Peek<'mem, 'facet>> {
41        unsafe {
42            (self.vtable().get_value_fn)(self.value.data().thin().unwrap())
43                .map(|inner_data| crate::Peek::unchecked_new(inner_data, self.def.t()))
44        }
45    }
46}