Skip to main content

facet_reflect/peek/
set.rs

1use super::Peek;
2use crate::{ReflectError, ReflectErrorKind};
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    fn err(&self, kind: ReflectErrorKind) -> ReflectError {
72        self.value.err(kind)
73    }
74
75    /// Returns true if the set is empty
76    #[inline]
77    pub fn is_empty(&self) -> bool {
78        self.len() == 0
79    }
80
81    /// Get the number of entries in the set
82    #[inline]
83    pub fn len(&self) -> usize {
84        unsafe { (self.def.vtable.len)(self.value.data()) }
85    }
86
87    /// Check if the set contains a value
88    #[inline]
89    pub fn contains_peek(&self, value: Peek<'_, 'facet>) -> Result<bool, ReflectError> {
90        if self.def.t() == value.shape {
91            return Ok(unsafe { (self.def.vtable.contains)(self.value.data(), value.data()) });
92        }
93
94        Err(self.err(ReflectErrorKind::WrongShape {
95            expected: self.def.t(),
96            actual: value.shape,
97        }))
98    }
99
100    /// Returns an iterator over the values in the set
101    #[inline]
102    pub fn iter(self) -> PeekSetIter<'mem, 'facet> {
103        let iter_init_with_value_fn = self.def.vtable.iter_vtable.init_with_value.unwrap();
104        let iter = unsafe { iter_init_with_value_fn(self.value.data()) };
105        PeekSetIter { set: self, iter }
106    }
107
108    /// Def getter
109    #[inline]
110    pub const fn def(&self) -> SetDef {
111        self.def
112    }
113}