solverforge_scoring/stream/
collection_extract.rs1#[diagnostic::on_unimplemented(
16 message = "`{Self}` is not a stream source",
17 label = "stream sources must come from the planning model",
18 note = "pass the generated accessor, e.g. `Plan::shifts()`"
19)]
20pub trait CollectionExtract<S>: Send + Sync {
21 type Item;
23
24 fn extract<'s>(&self, s: &'s S) -> &'s [Self::Item];
26
27 fn contains(&self, _s: &S, _item: &Self::Item) -> bool {
29 true
30 }
31
32 fn change_source(&self) -> ChangeSource;
36}
37
38#[doc(hidden)]
42pub struct FilteredExtract<E, F> {
43 extractor: E,
44 filter: F,
45}
46
47impl<E, F> FilteredExtract<E, F> {
48 pub fn new(extractor: E, filter: F) -> Self {
49 Self { extractor, filter }
50 }
51}
52
53impl<S, E, F> CollectionExtract<S> for FilteredExtract<E, F>
54where
55 E: CollectionExtract<S>,
56 F: super::filter::UniFilter<S, E::Item>,
57{
58 type Item = E::Item;
59
60 fn extract<'s>(&self, s: &'s S) -> &'s [Self::Item] {
61 self.extractor.extract(s)
62 }
63
64 fn contains(&self, s: &S, item: &Self::Item) -> bool {
65 self.extractor.contains(s, item) && self.filter.test(s, item)
66 }
67
68 fn change_source(&self) -> ChangeSource {
69 self.extractor.change_source()
70 }
71}
72
73#[doc(hidden)]
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub enum ChangeSource {
76 Static,
77 Descriptor(usize),
78}
79
80impl ChangeSource {
81 #[inline]
82 pub fn owns_descriptor(self, descriptor_index: usize) -> bool {
83 matches!(self, Self::Descriptor(index) if index == descriptor_index)
84 }
85
86 #[inline]
87 pub fn same_index_domain(self, other: Self) -> bool {
88 matches!((self, other), (Self::Descriptor(left), Self::Descriptor(right)) if left == right)
89 }
90}
91
92pub trait FlattenExtract<P>: Send + Sync {
94 type Item;
96
97 fn extract<'s>(&self, parent: &'s P) -> &'s [Self::Item];
99}
100
101impl<P, B, F> FlattenExtract<P> for F
102where
103 F: for<'a> Fn(&'a P) -> &'a [B] + Send + Sync,
104{
105 type Item = B;
106
107 #[inline]
108 fn extract<'s>(&self, parent: &'s P) -> &'s [B] {
109 self(parent)
110 }
111}
112
113#[derive(Clone, Copy)]
115pub struct FlattenVecExtract<F>(pub F);
116
117impl<P, B, F> FlattenExtract<P> for FlattenVecExtract<F>
118where
119 F: for<'a> Fn(&'a P) -> &'a Vec<B> + Send + Sync,
120{
121 type Item = B;
122
123 #[inline]
124 fn extract<'s>(&self, parent: &'s P) -> &'s [B] {
125 (self.0)(parent).as_slice()
126 }
127}
128
129#[doc(hidden)]
130#[derive(Clone, Copy)]
131pub struct SourceExtract<E> {
132 extractor: E,
133 change_source: ChangeSource,
134}
135
136impl<E> SourceExtract<E> {
137 pub fn new(extractor: E, change_source: ChangeSource) -> Self {
138 Self {
139 extractor,
140 change_source,
141 }
142 }
143
144 pub fn extractor(&self) -> &E {
145 &self.extractor
146 }
147}
148
149impl<S, A, E> CollectionExtract<S> for SourceExtract<E>
150where
151 E: for<'a> Fn(&'a S) -> &'a [A] + Send + Sync,
152{
153 type Item = A;
154
155 #[inline]
156 fn extract<'s>(&self, s: &'s S) -> &'s [A] {
157 (self.extractor)(s)
158 }
159
160 fn change_source(&self) -> ChangeSource {
161 self.change_source
162 }
163}
164
165#[doc(hidden)]
166pub fn source<E>(extractor: E, change_source: ChangeSource) -> SourceExtract<E> {
167 SourceExtract::new(extractor, change_source)
168}