facet_core/types/def/
iter.rs1use crate::{PtrConst, PtrMut};
2
3pub type IterInitWithValueFn = for<'value> unsafe fn(value: PtrConst<'value>) -> PtrMut<'value>;
9
10pub type IterNextFn<T> =
16 for<'iter> unsafe fn(iter: PtrMut<'iter>) -> Option<<T as IterItem>::Item<'iter>>;
17
18pub type IterNextBackFn<T> =
25 for<'iter> unsafe fn(iter: PtrMut<'iter>) -> Option<<T as IterItem>::Item<'iter>>;
26
27pub type IterSizeHintFn =
33 for<'iter> unsafe fn(iter: PtrMut<'iter>) -> Option<(usize, Option<usize>)>;
34
35pub type IterDeallocFn = for<'iter> unsafe fn(iter: PtrMut<'iter>);
41
42#[derive(Clone, Copy, Debug)]
44#[repr(C)]
45pub struct IterVTable<T: IterItem> {
46 pub init_with_value: Option<IterInitWithValueFn>,
48
49 pub next: IterNextFn<T>,
51
52 pub next_back: Option<IterNextBackFn<T>>,
54
55 pub size_hint: Option<IterSizeHintFn>,
57
58 pub dealloc: IterDeallocFn,
60}
61
62impl<T: IterItem> IterVTable<T> {
63 pub const fn builder() -> IterVTableBuilder<T> {
65 IterVTableBuilder::new()
66 }
67}
68
69pub struct IterVTableBuilder<T: IterItem> {
71 init_with_value: Option<IterInitWithValueFn>,
72 next: Option<IterNextFn<T>>,
73 next_back: Option<IterNextBackFn<T>>,
74 size_hint: Option<IterSizeHintFn>,
75 dealloc: Option<IterDeallocFn>,
76}
77
78impl<T: IterItem> IterVTableBuilder<T> {
79 #[allow(clippy::new_without_default)]
81 pub const fn new() -> Self {
82 Self {
83 init_with_value: None,
84 next: None,
85 next_back: None,
86 size_hint: None,
87 dealloc: None,
88 }
89 }
90
91 pub const fn init_with_value(mut self, f: IterInitWithValueFn) -> Self {
93 self.init_with_value = Some(f);
94 self
95 }
96
97 pub const fn next(mut self, f: IterNextFn<T>) -> Self {
99 self.next = Some(f);
100 self
101 }
102
103 pub const fn next_back(mut self, f: IterNextBackFn<T>) -> Self {
105 self.next_back = Some(f);
106 self
107 }
108
109 pub const fn dealloc(mut self, f: IterDeallocFn) -> Self {
111 self.dealloc = Some(f);
112 self
113 }
114
115 pub const fn build(self) -> IterVTable<T> {
121 assert!(self.init_with_value.is_some());
122 IterVTable {
123 init_with_value: self.init_with_value,
124 next: self.next.unwrap(),
125 next_back: self.next_back,
126 size_hint: self.size_hint,
127 dealloc: self.dealloc.unwrap(),
128 }
129 }
130}
131
132pub trait IterItem {
138 type Item<'a>;
140}
141
142impl IterItem for PtrConst<'_> {
143 type Item<'a> = PtrConst<'a>;
144}
145
146impl<T, U> IterItem for (T, U)
147where
148 T: IterItem,
149 U: IterItem,
150{
151 type Item<'a> = (T::Item<'a>, U::Item<'a>);
152}