orx_parallel/infallible/
xap.rs1use crate::infallible::fun::Map;
2use crate::sizes::Size;
3use crate::sizes::{Bin, One};
4
5pub trait Xap: Copy + Send {
7 type I;
9
10 type O;
12
13 type Size: Size;
15
16 type Values: IntoIterator<Item = Self::O>;
18
19 fn xap(&self, i: Self::I) -> Self::Values;
21
22 fn map<Q, H>(self, h: H) -> MapOf<Self, Q, H>
26 where
27 H: Fn(Self::O) -> Q + Copy + Send,
28 {
29 <Self::Size as Size>::map(self, h)
30 }
31
32 fn inspect<H>(self, h: H) -> InsOf<Self, H>
34 where
35 H: Fn(&Self::O) + Copy + Send,
36 {
37 <Self::Size as Size>::inspect(self, h)
38 }
39
40 fn filter<H>(self, h: H) -> FilOf<Self, H>
42 where
43 H: Fn(&Self::O) -> bool + Copy + Send,
44 {
45 <Self::Size as Size>::filter(self, h)
46 }
47
48 fn filter_map<Q, H>(self, h: H) -> FilMapOf<Self, Q, H>
50 where
51 H: Fn(Self::O) -> Option<Q> + Copy + Send,
52 {
53 <Self::Size as Size>::filter_map(self, h)
54 }
55
56 fn flat_map<V, H>(self, h: H) -> FlatMapOf<Self, V, H>
58 where
59 V: IntoIterator,
60 H: Fn(Self::O) -> V + Copy + Send,
61 {
62 <Self::Size as Size>::flat_map(self, h)
63 }
64
65 fn flatten(self) -> FlattenOf<Self>
67 where
68 Self::O: IntoIterator,
69 {
70 <Self::Size as Size>::flatten(self)
71 }
72
73 fn mapped<M>(self, m: M) -> MappedOf<Self, M>
77 where
78 M: Map<I = Self::O>,
79 {
80 <Self::Size as Size>::mapped(self, m)
81 }
82}
83
84pub trait XapOne: Xap<Size = One> {
88 #[inline(always)]
89 fn one_value(&self, i: Self::I) -> Self::O {
91 unsafe { self.xap(i).into_iter().next().unwrap_unchecked() }
93 }
94}
95
96impl<X: Xap<Size = One>> XapOne for X {}
97
98pub trait XapBin: Xap<Size = Bin> {
102 #[inline(always)]
103 fn bin_value(&self, i: Self::I) -> Option<Self::O> {
105 self.xap(i).into_iter().next()
107 }
108}
109
110impl<X: Xap<Size = Bin>> XapBin for X {}
111
112pub type MapOf<X, Q, H> = <<X as Xap>::Size as Size>::Map<X, Q, H>;
116
117pub type InsOf<X, H> = <<X as Xap>::Size as Size>::Inspect<X, H>;
119
120pub type FilOf<X, H> = <<X as Xap>::Size as Size>::Filter<X, H>;
122
123pub type FilMapOf<X, Q, H> = <<X as Xap>::Size as Size>::FilterMap<X, Q, H>;
125
126pub type FlatMapOf<X, V, H> = <<X as Xap>::Size as Size>::FlatMap<X, V, H>;
128
129pub type FlattenOf<X> = <<X as Xap>::Size as Size>::Flatten<X>;
131
132pub type MappedOf<X, M> = <<X as Xap>::Size as Size>::Mapped<X, M>;