pulz-schedule 0.1.0-alpha2

For scheduling systems and managing their resources
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use std::{
    any::{Any, TypeId},
    borrow::Cow,
    collections::{BTreeMap, BTreeSet, btree_map::Entry},
    marker::PhantomData,
    ops::{Deref, DerefMut},
    ptr::NonNull,
};

use atomic_refcell::AtomicRefCell;

use super::{FromResourcesMut, Res, ResMut, ResourceId, Taken};
use crate::{
    atom::Atom,
    meta::{Meta, MetaMap},
    util::DirtyVersion,
};

struct ResourceData {
    name: Cow<'static, str>,
    type_id: TypeId,
    is_send: bool,
    value: AtomicRefCell<Option<Box<dyn Any>>>,
}

// not send: should be dropped in original thread
unsafe impl Sync for ResourceData {}

impl ResourceData {
    /// UNSAFE: When send is false, the user must ensure that the resource is not accessed in other threads, as where the resource was created
    #[inline]
    unsafe fn borrow_any(&self) -> Option<Res<'_, dyn Any>> {
        Res::filter_map(self.value.borrow(), |v| Some(Box::deref(v.as_ref()?)))
    }

    /// UNSAFE: When send is false, the user must ensure that the resource is not accessed in other threads, as where the resource was created
    #[inline]
    unsafe fn borrow_any_mut(&self) -> Option<ResMut<'_, dyn Any>> {
        ResMut::filter_map(self.value.borrow_mut(), |v| {
            Some(Box::deref_mut(v.as_mut()?))
        })
    }

    /// UNSAFE: When send is false, the user must ensure that the resource is not accessed in other threads, as where the resource was created
    #[inline]
    unsafe fn borrow<T>(&self) -> Option<Res<'_, T>>
    where
        T: 'static,
    {
        Res::filter_map(self.value.borrow(), |v| v.as_ref()?.downcast_ref::<T>())
    }

    /// UNSAFE: When send is false, the user must ensure that the resource is not accessed in other threads, as where the resource was created
    #[inline]
    unsafe fn borrow_mut<T>(&self) -> Option<ResMut<'_, T>>
    where
        T: 'static,
    {
        ResMut::filter_map(self.value.borrow_mut(), |v| v.as_mut()?.downcast_mut::<T>())
    }

    /// UNSAFE: When send is false, the user must ensure that the resource is not accessed in other threads, as where the resource was created
    #[inline]
    unsafe fn get_mut<T>(&mut self) -> Option<&mut T>
    where
        T: 'static,
    {
        unsafe { self.get_any()?.downcast_mut::<T>() }
    }

    /// UNSAFE: When send is false, the user must ensure that the resource is not accessed in other threads, as where the resource was created
    #[inline]
    unsafe fn get_any(&mut self) -> Option<&mut dyn Any> {
        Some(self.value.get_mut().as_mut()?.deref_mut())
    }

    /// UNSAFE: When send is false, the user must ensure that the resource is not accessed in other threads, as where the resource was created
    #[inline]
    unsafe fn take<T>(&mut self) -> Option<Box<T>>
    where
        T: 'static,
    {
        unsafe { self.take_any()?.downcast().ok() }
    }

    /// UNSAFE: When send is false, the user must ensure that the resource is not accessed in other threads, as where the resource was created
    #[inline]
    unsafe fn take_any(&mut self) -> Option<Box<dyn Any>> {
        self.value.get_mut().take()
    }

    /// UNSAFE: When send is false, the user must ensure that the resource is not accessed in other threads, as where the resource was created
    #[inline]
    unsafe fn put_back(&mut self, boxed: Box<dyn Any>) {
        let value = self.value.get_mut();
        assert!(value.is_none(), "Resource already has a value");
        *value = Some(boxed);
    }
}

pub struct ResourcesSend {
    resources: Vec<ResourceData>,
    by_type_id: BTreeMap<TypeId, ResourceId>,
    meta_by_type_id: MetaMap,
    modules: BTreeSet<TypeId>,
    atom: Atom,
    version: DirtyVersion,
}

#[repr(transparent)]
pub struct Resources(ResourcesSend, PhantomData<NonNull<()>>);

#[allow(unused_doc_comments)]
macro_rules! impl_getters {
    (
        // UNSAFE: either T: Send + Sync, or Self=Resources (which is NOT Send+Sync)
        // User should provide a comment about safety
        unsafe $self:ident $(where T: $b1:ident $(+ $b2:ident)*)? => $resources_field:expr
    ) => {

        #[inline]
        pub fn borrow_res<T>(&self) -> Option<Res<'_, T>>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            self.borrow_res_id(self.id::<T>()?)
        }

        pub fn borrow_res_id<T>(&$self, resource_id: ResourceId<T>) -> Option<Res<'_, T>>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            // SAFETY: either T: Send + Sync, or Self=Resources (which is NOT Send+Sync)
            unsafe {
                $resources_field.get(resource_id.0)?.borrow()
            }
        }

        #[inline]
        pub fn borrow_res_mut<T>(&self) -> Option<ResMut<'_, T>>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            self.borrow_res_mut_id(self.id::<T>()?)
        }

        pub fn borrow_res_mut_id<T>(&$self, resource_id: ResourceId<T>) -> Option<ResMut<'_, T>>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            // SAFETY: either T: Send + Sync, or Self=Resources (which is NOT Send+Sync)
            unsafe {
                $resources_field.get(resource_id.0)?.borrow_mut()
            }
        }

        #[inline]
        pub fn get_mut<T>(&mut self) -> Option<&'_ mut T>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            self.get_mut_id(self.id::<T>()?)
        }

        pub fn get_mut_id<T>(&mut $self, resource_id: ResourceId<T>) -> Option<&'_ mut T>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            // SAFETY: either T: Send + Sync, or Self=Resources (which is NOT Send+Sync)
            unsafe {
                $resources_field.get_mut(resource_id.0)?.get_mut()
            }
        }

        #[inline]
        pub fn get_copy<T>(&self) -> Option<T>
        where
            T: Copy + $($b1 $(+ $b2)* +)? 'static,
        {
            self.get_copy_id(self.id::<T>()?)
        }

        pub fn get_copy_id<T>(&self, resource_id: ResourceId<T>) -> Option<T>
        where
            T: Copy + $($b1 $(+ $b2)* +)? 'static,
        {
            Some(*self.borrow_res_id(resource_id)?)
        }

        #[inline]
        pub fn get_clone<T>(&self) -> Option<T>
        where
            T: Clone + $($b1 $(+ $b2)* +)? 'static,
        {
            self.get_clone_id(self.id::<T>()?)
        }

        pub fn get_clone_id<T>(&self, resource_id: ResourceId<T>) -> Option<T>
        where
            T: Clone + $($b1 $(+ $b2)* +)? 'static,
        {
            Some(self.borrow_res_id(resource_id)?.clone())
        }

        #[inline]
        pub fn take<T>(&mut self) -> Option<Taken<T>>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            self.take_id(self.id::<T>()?)
        }

        pub fn take_id<T>(&mut $self, resource_id: ResourceId<T>) -> Option<Taken<T>>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            // SAFETY: either T: Send + Sync, or Self=Resources (which is NOT Send+Sync)
            let boxed = unsafe {
                $resources_field.get_mut(resource_id.0)?.take()?
            };
            Some(Taken {
                value: boxed,
                id: resource_id.untyped(),
                #[cfg(debug_assertions)]
                atom: $self.atom,
            })
        }

        pub fn put_back<T>(&mut $self, taken: Taken<T>)
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            // SAFETY: either T: Send + Sync, or Self=Resources (which is NOT Send+Sync)
            #[cfg(debug_assertions)]
            if $self.atom != taken.atom {
                panic!("put_taken called with a different atom than the one used to take the resource");
            }
            unsafe {
                $resources_field.get_mut(taken.id.0).expect("resource not defined").put_back(taken.value);
            }
        }

        #[inline]
        pub fn take_and<T, R>(&mut self, f: impl FnOnce(&mut T, &mut Self) -> R) -> Option<R>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            self.take_id_and(self.id::<T>()?, f)
        }

        #[inline]
        pub fn take_id_and<T, R>(&mut self, resource_id: ResourceId<T>, f: impl FnOnce(&mut T, &mut Self) -> R) -> Option<R>
        where
            T: $($b1 $(+ $b2)* +)? 'static,
        {
            use std::panic;
            let mut taken = self.take_id(resource_id)?;
            let r = panic::catch_unwind(panic::AssertUnwindSafe(|| f(&mut taken.value, self)));
            self.put_back(taken);
            match r {
                Err(e) => panic::resume_unwind(e),
                Ok(r) => Some(r)
            }
        }
    };
}

impl Resources {
    #[inline]
    pub fn new() -> Self {
        let mut res = Self(
            ResourcesSend {
                resources: Vec::new(),
                by_type_id: BTreeMap::new(),
                meta_by_type_id: BTreeMap::new(),
                modules: BTreeSet::new(),
                atom: Atom::new(),
                version: DirtyVersion::new(),
            },
            PhantomData,
        );
        res.init_core();
        res
    }

    fn init_core(&mut self) {
        self.init_unsend::<crate::schedule::Schedule>();
        // TODO
    }

    #[inline(always)]
    pub fn as_send(&self) -> &ResourcesSend {
        &self.0
    }

    #[inline]
    pub(crate) fn atom(&self) -> Atom {
        self.0.atom
    }

    #[inline]
    pub(crate) fn version_mut(&mut self) -> &mut DirtyVersion {
        &mut self.0.version
    }

    pub(crate) fn insert_module(&mut self, type_id: TypeId) -> bool {
        if self.0.modules.insert(type_id) {
            self.0.version.dirty();
            true
        } else {
            false
        }
    }

    /// # Safety
    /// User must ensure, that the value is Send + Sync, when is_send is true.
    unsafe fn insert_impl<T>(&mut self, is_send: bool, value: Option<Box<T>>) -> ResourceId<T>
    where
        T: 'static,
    {
        let type_id = TypeId::of::<T>();
        let name = Cow::Borrowed(std::any::type_name::<T>());
        unsafe {
            self.0
                ._insert_impl(type_id, name, is_send, value.map(|v| -> Box<dyn Any> { v }))
                .cast()
        }
    }

    #[inline]
    pub fn insert<T>(&mut self, value: T) -> ResourceId<T>
    where
        T: Send + Sync + 'static,
    {
        self.insert_box(Box::new(value))
    }

    #[inline]
    pub fn insert_box<T>(&mut self, value: Box<T>) -> ResourceId<T>
    where
        T: Send + Sync + 'static,
    {
        // SAFETY: T is Send + Sync
        unsafe { self.insert_impl(true, Some(value)) }
    }

    #[inline]
    pub fn define<T>(&mut self) -> ResourceId<T>
    where
        T: Send + Sync + 'static,
    {
        // SAFETY: T is Send + Sync
        unsafe { self.insert_impl(true, None) }
    }

    #[inline]
    pub fn insert_unsend<T>(&mut self, value: T) -> ResourceId<T>
    where
        T: 'static,
    {
        self.insert_box_unsend(Box::new(value))
    }

    #[inline]
    pub fn insert_box_unsend<T>(&mut self, value: Box<T>) -> ResourceId<T>
    where
        T: 'static,
    {
        // safety: is_send is false
        unsafe { self.insert_impl(false, Some(value)) }
    }

    #[inline]
    pub fn define_unsend<T>(&mut self) -> ResourceId<T>
    where
        T: 'static,
    {
        // safety: is_send is false
        unsafe { self.insert_impl(false, None) }
    }

    pub fn try_init<T>(&mut self) -> Result<ResourceId<T>, ResourceId<T>>
    where
        T: Send + Sync + FromResourcesMut + 'static,
    {
        if let Some(id) = self.0.id::<T>() {
            Err(id)
        } else {
            let value = T::from_resources_mut(self);
            Ok(self.insert(value))
        }
    }

    #[inline]
    pub fn init<T>(&mut self) -> ResourceId<T>
    where
        T: Send + Sync + FromResourcesMut + 'static,
    {
        match self.try_init() {
            Ok(id) | Err(id) => id,
        }
    }

    pub fn try_init_unsend<T>(&mut self) -> Result<ResourceId<T>, ResourceId<T>>
    where
        T: FromResourcesMut + 'static,
    {
        if let Some(id) = self.0.id::<T>() {
            Err(id)
        } else {
            let value = T::from_resources_mut(self);
            Ok(self.insert_unsend(value))
        }
    }

    #[inline]
    pub fn init_unsend<T>(&mut self) -> ResourceId<T>
    where
        T: FromResourcesMut + 'static,
    {
        match self.try_init_unsend() {
            Ok(id) | Err(id) => id,
        }
    }

    pub fn clear(&mut self) {
        self.0.resources.clear();
        self.0.by_type_id.clear();
        self.0.meta_by_type_id.clear();
        self.0.modules.clear();
        self.init_core();
    }

    impl_getters!(
        // SAFETY: Self=Resources is not Send + Sync
        unsafe self => self.0.resources
    );

    pub(crate) fn get_meta<T: ?Sized + 'static>(&self) -> Option<&Meta<T>> {
        Self::get_meta_from_map(&self.0.meta_by_type_id)
    }

    pub(crate) fn get_meta_mut<T: ?Sized + 'static>(&mut self) -> &mut Meta<T> {
        Self::get_meta_mut_from_map(&mut self.0.meta_by_type_id)
    }

    pub fn borrow_res_meta<T>(&self, resource_id: ResourceId<T>) -> Option<Res<'_, T>>
    where
        T: ?Sized + 'static,
    {
        let r = self.0.resources.get(resource_id.0)?;
        let meta = self.get_meta::<T>()?;
        // SAFETY: Self=Resources is not Send + Sync
        unsafe { Res::filter_map(r.borrow_any()?, |v| meta.convert_ref(v)) }
    }

    pub fn borrow_res_any(&self, resource_id: ResourceId) -> Option<Res<'_, dyn Any>> {
        // SAFETY: Self=Resources is not Send + Sync
        unsafe { self.resources.get(resource_id.0)?.borrow_any() }
    }

    pub fn borrow_res_mut_meta<T>(&self, resource_id: ResourceId<T>) -> Option<ResMut<'_, T>>
    where
        T: ?Sized + 'static,
    {
        let r = self.0.resources.get(resource_id.0)?;
        let meta = self.get_meta::<T>()?;
        // SAFETY: Self=Resources is not Send + Sync
        unsafe { ResMut::filter_map(r.borrow_any_mut()?, |v| meta.convert_mut(v)) }
    }

    pub fn borrow_res_any_mut(&self, resource_id: ResourceId) -> Option<ResMut<'_, dyn Any>> {
        // SAFETY: Self=Resources is not Send + Sync
        unsafe { self.0.resources.get(resource_id.0)?.borrow_any_mut() }
    }

    pub fn get_mut_any(&mut self, resource_id: ResourceId) -> Option<&'_ mut dyn Any> {
        // SAFETY: Self=Resources is not Send + Sync
        unsafe { self.0.resources.get_mut(resource_id.0)?.get_any() }
    }
}

impl Deref for Resources {
    type Target = ResourcesSend;

    #[inline]
    fn deref(&self) -> &ResourcesSend {
        &self.0
    }
}

impl AsRef<ResourcesSend> for Resources {
    #[inline]
    fn as_ref(&self) -> &ResourcesSend {
        &self.0
    }
}

impl ResourcesSend {
    /// # Safety
    /// User must not access unsend resources in other threads.
    #[inline]
    pub unsafe fn as_unsend(&self) -> &Resources {
        let self_ptr: *const Self = self;
        // SAFETY: cast is allowed because it is a newtype-struct with #[repr(transparent)].
        // Send -> Unsend is unsafe (see doc)
        unsafe { &*(self_ptr as *const Resources) }
    }

    #[inline]
    pub fn id<T>(&self) -> Option<ResourceId<T>>
    where
        T: 'static,
    {
        let type_id = TypeId::of::<T>();
        self.by_type_id.get(&type_id).copied().map(ResourceId::cast)
    }

    #[inline]
    pub fn expect_id<T>(&self) -> ResourceId<T>
    where
        T: 'static,
    {
        let Some(id) = self.id::<T>() else {
            panic!("resource {} not initialized", std::any::type_name::<T>());
        };
        id
    }

    #[inline]
    pub fn name<T>(&self, id: ResourceId<T>) -> Option<&str> {
        self.resources.get(id.0).map(|r| r.name.as_ref())
    }

    #[inline]
    pub fn type_id<T>(&self, id: ResourceId<T>) -> Option<TypeId> {
        self.resources.get(id.0).map(|r| r.type_id)
    }

    /// # Safety
    /// User must ensure, that the value is Send + Sync, when is_send is true.
    unsafe fn _insert_impl(
        &mut self,
        type_id: TypeId,
        name: Cow<'static, str>,
        is_send: bool,
        value: Option<Box<dyn Any>>,
    ) -> ResourceId {
        let resources = &mut self.resources;
        match self.by_type_id.entry(type_id) {
            Entry::Occupied(entry) => {
                let id = *entry.get();
                let r = &mut resources[id.0];
                r.is_send = is_send;
                if value.is_some() {
                    r.value = AtomicRefCell::new(value);
                }
                id
            }
            Entry::Vacant(entry) => {
                self.version.dirty();
                let id = ResourceId::new(resources.len()); // keep positive => dense
                resources.push(ResourceData {
                    name,
                    type_id,
                    is_send,
                    value: AtomicRefCell::new(value),
                });
                entry.insert(id);
                self.version.dirty();
                id
            }
        }
    }

    impl_getters!(
        // SAFETY: T is restricted to be Send+Sync
        unsafe self where T: Send + Sync => self.resources
    );
}

impl Default for Resources {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}