Skip to main content

facet_reflect/peek/
set.rs

1use super::Peek;
2use crate::ReflectError;
3use facet_core::{PtrMut, SetDef};
4
5/// Iterator over values in a `PeekSet`
6pub struct PeekSetIter<'mem, 'facet> {
7    set: PeekSet<'mem, 'facet>,
8    iter: PtrMut,
9}
10
11impl<'mem, 'facet> Iterator for PeekSetIter<'mem, 'facet> {
12    type Item = Peek<'mem, 'facet>;
13
14    #[inline]
15    fn next(&mut self) -> Option<Self::Item> {
16        unsafe {
17            let next = (self.set.def.vtable.iter_vtable.next)(self.iter)?;
18            Some(Peek::unchecked_new(next, self.set.def.t()))
19        }
20    }
21}
22
23impl<'mem, 'facet> Drop for PeekSetIter<'mem, 'facet> {
24    #[inline]
25    fn drop(&mut self) {
26        unsafe { (self.set.def.vtable.iter_vtable.dealloc)(self.iter) }
27    }
28}
29
30impl<'mem, 'facet> IntoIterator for &'mem PeekSet<'mem, 'facet> {
31    type Item = Peek<'mem, 'facet>;
32    type IntoIter = PeekSetIter<'mem, 'facet>;
33
34    #[inline]
35    fn into_iter(self) -> Self::IntoIter {
36        self.iter()
37    }
38}
39
40/// Lets you read from a set
41#[derive(Clone, Copy)]
42pub struct PeekSet<'mem, 'facet> {
43    value: Peek<'mem, 'facet>,
44
45    def: SetDef,
46}
47
48impl<'mem, 'facet> core::fmt::Debug for PeekSet<'mem, 'facet> {
49    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
50        f.debug_struct("PeekSet").finish_non_exhaustive()
51    }
52}
53
54impl<'mem, 'facet> PeekSet<'mem, 'facet> {
55    /// Constructor
56    ///
57    /// # Safety
58    ///
59    /// The caller must ensure that `def` contains valid vtable function pointers that:
60    /// - Correctly implement the set operations for the actual type
61    /// - Do not cause undefined behavior when called
62    /// - Return pointers within valid memory bounds
63    /// - Match the element type specified in `def.t()`
64    ///
65    /// Violating these requirements can lead to memory safety issues.
66    #[inline]
67    pub const unsafe fn new(value: Peek<'mem, 'facet>, def: SetDef) -> Self {
68        Self { value, def }
69    }
70
71    /// Returns true if the set is empty
72    #[inline]
73    pub fn is_empty(&self) -> bool {
74        self.len() == 0
75    }
76
77    /// Get the number of entries in the set
78    #[inline]
79    pub fn len(&self) -> usize {
80        unsafe { (self.def.vtable.len)(self.value.data()) }
81    }
82
83    /// Check if the set contains a value
84    #[inline]
85    pub fn contains_peek(&self, value: Peek<'_, 'facet>) -> Result<bool, ReflectError> {
86        if self.def.t() == value.shape {
87            return Ok(unsafe { (self.def.vtable.contains)(self.value.data(), value.data()) });
88        }
89
90        Err(ReflectError::WrongShape {
91            expected: self.def.t(),
92            actual: value.shape,
93        })
94    }
95
96    /// Returns an iterator over the values in the set
97    #[inline]
98    pub fn iter(self) -> PeekSetIter<'mem, 'facet> {
99        let iter_init_with_value_fn = self.def.vtable.iter_vtable.init_with_value.unwrap();
100        let iter = unsafe { iter_init_with_value_fn(self.value.data()) };
101        PeekSetIter { set: self, iter }
102    }
103
104    /// Def getter
105    #[inline]
106    pub const fn def(&self) -> SetDef {
107        self.def
108    }
109}