|
| 1 | +// revisions:cfail1 cfail2 |
| 2 | +// compile-flags: --crate-type=lib --edition=2021 |
| 3 | +// build-pass |
| 4 | + |
| 5 | +use core::any::Any; |
| 6 | +use core::marker::PhantomData; |
| 7 | + |
| 8 | +struct DerefWrap<T>(T); |
| 9 | + |
| 10 | +impl<T> core::ops::Deref for DerefWrap<T> { |
| 11 | + type Target = T; |
| 12 | + fn deref(&self) -> &Self::Target { |
| 13 | + &self.0 |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +struct Storage<T, D> { |
| 18 | + phantom: PhantomData<(T, D)>, |
| 19 | +} |
| 20 | + |
| 21 | +type ReadStorage<T> = Storage<T, DerefWrap<MaskedStorage<T>>>; |
| 22 | + |
| 23 | +pub trait Component { |
| 24 | + type Storage; |
| 25 | +} |
| 26 | + |
| 27 | +struct VecStorage; |
| 28 | + |
| 29 | +struct Pos; |
| 30 | + |
| 31 | +impl Component for Pos { |
| 32 | + type Storage = VecStorage; |
| 33 | +} |
| 34 | + |
| 35 | +struct GenericComp<T> { |
| 36 | + _t: T, |
| 37 | +} |
| 38 | + |
| 39 | +impl<T: 'static> Component for GenericComp<T> { |
| 40 | + type Storage = VecStorage; |
| 41 | +} |
| 42 | +struct ReadData { |
| 43 | + pos_interpdata: ReadStorage<GenericComp<Pos>>, |
| 44 | +} |
| 45 | + |
| 46 | +trait System { |
| 47 | + type SystemData; |
| 48 | + |
| 49 | + fn run(data: Self::SystemData, any: Box<dyn Any>); |
| 50 | +} |
| 51 | + |
| 52 | +struct Sys; |
| 53 | + |
| 54 | +impl System for Sys { |
| 55 | + type SystemData = (ReadData, ReadStorage<Pos>); |
| 56 | + |
| 57 | + fn run((data, pos): Self::SystemData, any: Box<dyn Any>) { |
| 58 | + <ReadStorage<GenericComp<Pos>> as SystemData>::setup(any); |
| 59 | + |
| 60 | + ParJoin::par_join((&pos, &data.pos_interpdata)); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +trait ParJoin { |
| 65 | + fn par_join(self) |
| 66 | + where |
| 67 | + Self: Sized, |
| 68 | + { |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<'a, T, D> ParJoin for &'a Storage<T, D> |
| 73 | +where |
| 74 | + T: Component, |
| 75 | + D: core::ops::Deref<Target = MaskedStorage<T>>, |
| 76 | + T::Storage: Sync, |
| 77 | +{ |
| 78 | +} |
| 79 | + |
| 80 | +impl<A, B> ParJoin for (A, B) |
| 81 | +where |
| 82 | + A: ParJoin, |
| 83 | + B: ParJoin, |
| 84 | +{ |
| 85 | +} |
| 86 | + |
| 87 | +pub trait SystemData { |
| 88 | + fn setup(any: Box<dyn Any>); |
| 89 | +} |
| 90 | + |
| 91 | +impl<T: 'static> SystemData for ReadStorage<T> |
| 92 | +where |
| 93 | + T: Component, |
| 94 | +{ |
| 95 | + fn setup(any: Box<dyn Any>) { |
| 96 | + let storage: &MaskedStorage<T> = any.downcast_ref().unwrap(); |
| 97 | + |
| 98 | + <dyn Any as CastFrom<MaskedStorage<T>>>::cast(&storage); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +pub struct MaskedStorage<T: Component> { |
| 103 | + _inner: T::Storage, |
| 104 | +} |
| 105 | + |
| 106 | +pub unsafe trait CastFrom<T> { |
| 107 | + fn cast(t: &T) -> &Self; |
| 108 | +} |
| 109 | + |
| 110 | +unsafe impl<T> CastFrom<T> for dyn Any |
| 111 | +where |
| 112 | + T: Any + 'static, |
| 113 | +{ |
| 114 | + fn cast(t: &T) -> &Self { |
| 115 | + t |
| 116 | + } |
| 117 | +} |
0 commit comments