solverforge_scoring/stream/
collection_extract.rs1use std::marker::PhantomData;
11
12mod planning_id;
13
14#[doc(hidden)]
15pub use planning_id::{
16 AnalysisPlanningId, ModelPlanningIds, NoPlanningIds, RenderPlanningId, RenderPlanningIdDebug,
17 RenderPlanningIdDisplay, RenderPlanningIdNone, SourcePlanningIds,
18};
19
20#[diagnostic::on_unimplemented(
26 message = "`{Self}` is not a stream source",
27 label = "stream sources must come from the planning model",
28 note = "pass the generated accessor, e.g. `Plan::shifts()`"
29)]
30pub trait CollectionExtract<S>: Send + Sync {
31 type Item;
33
34 fn extract<'s>(&self, s: &'s S) -> &'s [Self::Item];
36
37 fn contains(&self, _s: &S, _item: &Self::Item) -> bool {
39 true
40 }
41
42 fn change_source(&self) -> ChangeSource;
46
47 fn planning_id(&self, _item: &Self::Item) -> Option<String> {
53 None
54 }
55}
56
57#[doc(hidden)]
61pub struct FilteredExtract<E, F> {
62 extractor: E,
63 filter: F,
64}
65
66impl<E, F> FilteredExtract<E, F> {
67 pub fn new(extractor: E, filter: F) -> Self {
68 Self { extractor, filter }
69 }
70}
71
72impl<S, E, F> CollectionExtract<S> for FilteredExtract<E, F>
73where
74 E: CollectionExtract<S>,
75 F: super::filter::UniFilter<S, E::Item>,
76{
77 type Item = E::Item;
78
79 fn extract<'s>(&self, s: &'s S) -> &'s [Self::Item] {
80 self.extractor.extract(s)
81 }
82
83 fn contains(&self, s: &S, item: &Self::Item) -> bool {
84 self.extractor.contains(s, item) && self.filter.test(s, item)
85 }
86
87 fn change_source(&self) -> ChangeSource {
88 self.extractor.change_source()
89 }
90
91 fn planning_id(&self, item: &Self::Item) -> Option<String> {
92 self.extractor.planning_id(item)
93 }
94}
95
96#[doc(hidden)]
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub enum ChangeSource {
99 Static,
100 Descriptor(usize),
101}
102
103impl ChangeSource {
104 #[inline]
105 pub fn owns_descriptor(self, descriptor_index: usize) -> bool {
106 matches!(self, Self::Descriptor(index) if index == descriptor_index)
107 }
108
109 #[inline]
110 pub fn same_index_domain(self, other: Self) -> bool {
111 matches!((self, other), (Self::Descriptor(left), Self::Descriptor(right)) if left == right)
112 }
113}
114
115pub trait FlattenExtract<P>: Send + Sync {
117 type Item;
119
120 fn extract<'s>(&self, parent: &'s P) -> &'s [Self::Item];
122}
123
124impl<P, B, F> FlattenExtract<P> for F
125where
126 F: for<'a> Fn(&'a P) -> &'a [B] + Send + Sync,
127{
128 type Item = B;
129
130 #[inline]
131 fn extract<'s>(&self, parent: &'s P) -> &'s [B] {
132 self(parent)
133 }
134}
135
136#[derive(Clone, Copy)]
138pub struct FlattenVecExtract<F>(pub F);
139
140impl<P, B, F> FlattenExtract<P> for FlattenVecExtract<F>
141where
142 F: for<'a> Fn(&'a P) -> &'a Vec<B> + Send + Sync,
143{
144 type Item = B;
145
146 #[inline]
147 fn extract<'s>(&self, parent: &'s P) -> &'s [B] {
148 (self.0)(parent).as_slice()
149 }
150}
151
152#[doc(hidden)]
156pub struct SourceExtract<E, I = NoPlanningIds> {
157 extractor: E,
158 change_source: ChangeSource,
159 planning_ids: PhantomData<fn() -> I>,
160}
161
162impl<E: Clone, I> Clone for SourceExtract<E, I> {
163 fn clone(&self) -> Self {
164 Self {
165 extractor: self.extractor.clone(),
166 change_source: self.change_source,
167 planning_ids: PhantomData,
168 }
169 }
170}
171
172impl<E: Copy, I> Copy for SourceExtract<E, I> {}
173
174impl<E, I> SourceExtract<E, I> {
175 pub fn new(extractor: E, change_source: ChangeSource) -> Self {
176 Self {
177 extractor,
178 change_source,
179 planning_ids: PhantomData,
180 }
181 }
182
183 pub fn extractor(&self) -> &E {
184 &self.extractor
185 }
186}
187
188impl<S, A, E, I> CollectionExtract<S> for SourceExtract<E, I>
189where
190 E: for<'a> Fn(&'a S) -> &'a [A] + Send + Sync,
191 I: SourcePlanningIds<A>,
192{
193 type Item = A;
194
195 #[inline]
196 fn extract<'s>(&self, s: &'s S) -> &'s [A] {
197 (self.extractor)(s)
198 }
199
200 fn change_source(&self) -> ChangeSource {
201 self.change_source
202 }
203
204 fn planning_id(&self, item: &A) -> Option<String> {
205 I::planning_id(item)
206 }
207}
208
209#[doc(hidden)]
210pub fn source<E>(extractor: E, change_source: ChangeSource) -> SourceExtract<E> {
211 SourceExtract::new(extractor, change_source)
212}
213
214#[doc(hidden)]
217pub fn model_source<E>(
218 extractor: E,
219 change_source: ChangeSource,
220) -> SourceExtract<E, ModelPlanningIds> {
221 SourceExtract::new(extractor, change_source)
222}