1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#![allow(missing_docs, missing_debug_implementations)]
use std::any::TypeId;
mod tags {
use std::marker::PhantomData;
pub trait Type<'a>: Sized + 'static {
type Reified: 'a;
}
pub trait MaybeSizedType<'a>: Sized + 'static {
type Reified: 'a + ?Sized;
}
impl<'a, T: Type<'a>> MaybeSizedType<'a> for T {
type Reified = T::Reified;
}
#[derive(Debug)]
pub struct Value<T: 'static>(PhantomData<T>);
impl<T: 'static> Type<'_> for Value<T> {
type Reified = T;
}
#[derive(Debug)]
pub struct MaybeSizedValue<T: ?Sized + 'static>(PhantomData<T>);
impl<T: ?Sized + 'static> MaybeSizedType<'_> for MaybeSizedValue<T> {
type Reified = T;
}
#[derive(Debug)]
pub struct Ref<I>(PhantomData<I>);
impl<'a, I: MaybeSizedType<'a>> Type<'a> for Ref<I> {
type Reified = &'a I::Reified;
}
#[derive(Debug)]
pub struct RefMut<I>(PhantomData<I>);
impl<'a, I: MaybeSizedType<'a>> Type<'a> for RefMut<I> {
type Reified = &'a mut I::Reified;
}
}
pub trait Provider {
fn provide<'a>(&'a self, demand: &mut Demand<'a>);
fn provide_mut<'a>(&'a mut self, _demand: &mut Demand<'a>) {}
}
pub trait SlottedProvider {
fn provide<'a>(&'a self, slot: usize, demand: &mut Demand<'a>);
fn provide_mut<'a>(&'a mut self, _slot: usize, _demand: &mut Demand<'a>) {}
}
pub struct ProviderArray<const N: usize> {
pub providers: [Option<Box<dyn Provider>>; N],
}
impl<const N: usize> ProviderArray<N> {
pub fn set<T: 'static + Provider>(&mut self, slot: usize, provider: T) {
if slot < N {
self.providers[slot] = Some(Box::new(provider));
} else {
panic!("slot out of range");
}
}
}
impl<const N: usize> Default for ProviderArray<N> {
fn default() -> Self {
const NONE: Option<Box<dyn Provider>> = None;
Self {
providers: [NONE; N],
}
}
}
impl<const N: usize> SlottedProvider for ProviderArray<N> {
fn provide<'a>(&'a self, slot: usize, demand: &mut Demand<'a>) {
if slot < N {
if let Some(provider) = &self.providers[slot] {
provider.provide(demand);
}
}
}
fn provide_mut<'a>(&'a mut self, slot: usize, demand: &mut Demand<'a>) {
if slot < N {
if let Some(provider) = &mut self.providers[slot] {
provider.provide_mut(demand);
}
}
}
}
#[repr(transparent)]
pub struct Demand<'a>(dyn Erased<'a> + 'a);
pub struct Receiver<'a, 'b, I: tags::Type<'a>>(&'b mut TaggedOption<'a, I>);
impl<'a, I: tags::Type<'a>> Receiver<'a, '_, I> {
#[inline]
pub fn provide(&mut self, value: I::Reified) {
self.0 .0 = Some(value)
}
}
impl<'a> Demand<'a> {
fn new<'b>(erased: &'b mut (dyn Erased<'a> + 'a)) -> &'b mut Demand<'a> {
unsafe { &mut *(erased as *mut dyn Erased<'a> as *mut Demand<'a>) }
}
pub fn provide_value<T, F>(&mut self, fulfil: F) -> &mut Self
where
T: 'static,
F: FnOnce() -> T,
{
self.provide_with::<tags::Value<T>, F>(fulfil)
}
pub fn provide_ref<T: ?Sized + 'static>(&mut self, value: &'a T) -> &mut Self {
self.provide::<tags::Ref<tags::MaybeSizedValue<T>>>(value)
}
pub fn provide_mut<T: ?Sized + 'static>(&mut self, value: &'a mut T) -> &mut Self {
self.provide::<tags::RefMut<tags::MaybeSizedValue<T>>>(value)
}
pub fn maybe_provide_mut<T: ?Sized + 'static>(
&mut self,
) -> Option<Receiver<'a, '_, tags::RefMut<tags::MaybeSizedValue<T>>>> {
self.0
.downcast_mut::<tags::RefMut<tags::MaybeSizedValue<T>>>()
.map(Receiver)
}
fn provide<I>(&mut self, value: I::Reified) -> &mut Self
where
I: tags::Type<'a>,
{
if let Some(res @ TaggedOption(None)) = self.0.downcast_mut::<I>() {
res.0 = Some(value);
}
self
}
fn provide_with<I, F>(&mut self, fulfil: F) -> &mut Self
where
I: tags::Type<'a>,
F: FnOnce() -> I::Reified,
{
if let Some(res @ TaggedOption(None)) = self.0.downcast_mut::<I>() {
res.0 = Some(fulfil());
}
self
}
}
impl std::fmt::Debug for Demand<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Demand").finish_non_exhaustive()
}
}
unsafe trait Erased<'a>: 'a {
fn tag_id(&self) -> TypeId;
}
unsafe impl<'a, I: tags::Type<'a>> Erased<'a> for TaggedOption<'a, I> {
fn tag_id(&self) -> TypeId {
TypeId::of::<I>()
}
}
impl<'a> dyn Erased<'a> + 'a {
#[inline]
fn downcast_mut<I>(&mut self) -> Option<&mut TaggedOption<'a, I>>
where
I: tags::Type<'a>,
{
if self.tag_id() == TypeId::of::<I>() {
Some(unsafe { &mut *(self as *mut Self).cast::<TaggedOption<'a, I>>() })
} else {
None
}
}
}
#[repr(transparent)]
struct TaggedOption<'a, I: tags::Type<'a>>(Option<I::Reified>);
impl<'a, I: tags::Type<'a>> TaggedOption<'a, I> {
fn as_demand(&mut self) -> &mut Demand<'a> {
Demand::new(self as &mut (dyn Erased<'a> + 'a))
}
}
pub fn request_ref<'a, T, P>(provider: &'a P) -> Option<&'a T>
where
T: 'static + ?Sized,
P: Provider + ?Sized,
{
request_by_type_tag::<'a, tags::Ref<tags::MaybeSizedValue<T>>, P>(provider)
}
pub fn request_ref_by_slot<'a, T, P>(provider: &'a P, slot: usize) -> Option<&'a T>
where
T: 'static + ?Sized,
P: SlottedProvider + ?Sized,
{
let mut tagged = TaggedOption::<'a, tags::Ref<tags::MaybeSizedValue<T>>>(None);
provider.provide(slot, tagged.as_demand());
tagged.0
}
pub fn request_mut<'a, T, P>(provider: &'a mut P) -> Option<&'a mut T>
where
T: 'static + ?Sized,
P: Provider + ?Sized,
{
let mut tagged = TaggedOption::<'a, tags::RefMut<tags::MaybeSizedValue<T>>>(None);
provider.provide_mut(tagged.as_demand());
tagged.0
}
fn request_by_type_tag<'a, I, P>(provider: &'a P) -> Option<I::Reified>
where
I: tags::Type<'a>,
P: Provider + ?Sized,
{
let mut tagged = TaggedOption::<'a, I>(None);
provider.provide(tagged.as_demand());
tagged.0
}
impl Provider for () {
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
demand.provide_ref(self);
}
fn provide_mut<'a>(&'a mut self, demand: &mut Demand<'a>) {
demand.provide_mut(self);
}
}