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
use pi_null::Null;
use pi_proc_macros::all_tuples;
use std::any::TypeId;
use std::borrow::Cow;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};

use crate::archetype::{
    Archetype, ArchetypeDepend, ArchetypeDependResult, ColumnIndex, Flags, Row,
};
use crate::column::Column;
use crate::dirty::ComponentDirty;
use crate::system::SystemMeta;
use crate::table::Table;
use crate::world::{Entity, SingleResource, World};

pub trait FetchComponents {
    /// The item returned by this [`FetchComponents`]
    type Item<'a>;
    /// ReadOnly
    type ReadOnly: FetchComponents;
    /// Per archetype/table state used by this [`FetchComponents`] to fetch [`Self::Item`](crate::query::FetchComponents::Item)
    type Fetch<'a>;

    /// State used to construct a [`Self::Fetch`](crate::query::FetchComponents::Fetch). This will be cached inside [`QueryState`](crate::query::QueryState),
    /// so it is best to move as much data / computation here as possible to reduce the cost of
    /// constructing [`Self::Fetch`](crate::query::FetchComponents::Fetch).
    type State: Send + Sync + Sized;

    /// initializes ReadWrite for this [`FetchComponents`] type.
    fn init_read_write(_world: &World, _meta: &mut SystemMeta) {}
    fn archetype_depend(_archetype: &Archetype, _result: &mut ArchetypeDependResult) {}
    fn res_depend(
        _res_tid: &TypeId,
        _res_name: &Cow<'static, str>,
        _single: bool,
        _result: &mut Flags,
    ) {
    }

    /// Creates and initializes a [`State`](FetchComponents::State) for this [`FetchComponents`] type.
    fn init_state(world: &World, archetype: &Archetype) -> Self::State;

    /// Creates a new instance of this fetch.
    ///
    /// # Safety
    ///
    /// - `world` must have permission to access any of the components specified in `Self::update_archetype_component_access`.
    /// - `state` must have been initialized (via [`FetchComponents::init_state`]) using the same `world` passed
    ///   in to this function.
    fn init_fetch<'w>(
        world: &'w World,
        archetype: &'w Archetype,
        state: &'w Self::State,
    ) -> Self::Fetch<'w>;

    /// Fetch [`Self::Item`](`FetchComponents::Item`) for either the given `entity` in the current [`Table`],
    /// or for the given `entity` in the current [`Archetype`]. This must always be called after
    /// [`FetchComponents::set_table`] with a `table_row` in the range of the current [`Table`] or after
    /// [`FetchComponents::set_archetype`]  with a `entity` in the current archetype.
    ///
    /// # Safety
    ///
    /// Must always be called _after_ [`FetchComponents::set_table`] or [`FetchComponents::set_archetype`]. `entity` and
    /// `table_row` must be in the range of the current table and archetype.
    ///
    /// If `update_component_access` includes any mutable accesses, then the caller must ensure
    /// that `fetch` is called no more than once for each `entity`/`table_row` in each archetype.
    /// If `Self` implements [`ReadOnlyFetchComponents`], then this can safely be called multiple times.
    fn fetch<'w>(fetch: &mut Self::Fetch<'w>, row: Row, e: Entity) -> Self::Item<'w>;
}

impl FetchComponents for Entity {
    type Fetch<'w> = &'w Table;
    type Item<'w> = Entity;
    type ReadOnly = Entity;
    type State = ();

    fn init_state(_world: &World, _archetype: &Archetype) -> Self::State {}

    fn init_fetch<'w>(
        _world: &'w World,
        archetype: &'w Archetype,
        _state: &'w Self::State,
    ) -> Self::Fetch<'w> {
        &archetype.table
    }

    #[inline(always)]
    fn fetch<'w>(_fetch: &mut Self::Fetch<'w>, _row: Row, e: Entity) -> Self::Item<'w> {
        e
    }
}

impl<T: 'static> FetchComponents for &T {
    type Fetch<'w> = &'w Column;
    type Item<'w> = &'w T;
    type ReadOnly = &'static T;
    type State = ColumnIndex;

    fn init_read_write(_world: &World, meta: &mut SystemMeta) {
        meta.cur_param
            .reads
            .insert(TypeId::of::<T>(), std::any::type_name::<T>().into());
    }
    fn archetype_depend(archetype: &Archetype, result: &mut ArchetypeDependResult) {
        result.merge(ArchetypeDepend::Flag(
            if archetype.get_column(&TypeId::of::<T>()).is_none() {
                Flags::WITHOUT
            } else {
                Flags::READ
            },
        ))
    }
    fn init_state(_world: &World, archetype: &Archetype) -> Self::State {
        archetype.get_column_index(&TypeId::of::<T>())
    }

    #[inline]
    fn init_fetch<'w>(
        _world: &'w World,
        archetype: &'w Archetype,
        state: &'w Self::State,
    ) -> Self::Fetch<'w> {
        &archetype.table.get_column_unchecked(*state)
    }

    #[inline(always)]
    fn fetch<'w>(fetch: &mut Self::Fetch<'w>, row: Row, _e: Entity) -> Self::Item<'w> {
        fetch.get(row)
    }
}

impl<T: 'static> FetchComponents for &mut T {
    type Fetch<'w> = &'w Column;
    type Item<'w> = Mut<'w, T>;
    type ReadOnly = &'static T;
    type State = ColumnIndex;

    fn init_read_write(_world: &World, meta: &mut SystemMeta) {
        meta.cur_param
            .writes
            .insert(TypeId::of::<T>(), std::any::type_name::<T>().into());
    }
    fn archetype_depend(archetype: &Archetype, result: &mut ArchetypeDependResult) {
        result.merge(ArchetypeDepend::Flag(
            if archetype.get_column(&TypeId::of::<T>()).is_none() {
                Flags::WITHOUT
            } else {
                Flags::WRITE
            },
        ))
    }
    fn init_state(_world: &World, archetype: &Archetype) -> Self::State {
        archetype.get_column_index(&TypeId::of::<T>())
    }

    #[inline]
    fn init_fetch<'w>(
        _world: &'w World,
        archetype: &'w Archetype,
        state: &'w Self::State,
    ) -> Self::Fetch<'w> {
        &archetype.table.get_column_unchecked(*state)
    }

    #[inline(always)]
    fn fetch<'w>(fetch: &mut Self::Fetch<'w>, row: Row, e: Entity) -> Self::Item<'w> {
        Mut {
            value: fetch.get_mut(row),
            dirty: &fetch.changed,
            e,
            row,
        }
    }
}

impl<T: 'static> FetchComponents for Option<&T> {
    type Fetch<'w> = Option<&'w Column>;
    type Item<'w> = Option<&'w T>;
    type ReadOnly = Option<&'static T>;
    type State = ColumnIndex;

    fn init_read_write(_world: &World, meta: &mut SystemMeta) {
        meta.cur_param
            .reads
            .insert(TypeId::of::<T>(), std::any::type_name::<T>().into());
    }
    fn archetype_depend(archetype: &Archetype, result: &mut ArchetypeDependResult) {
        result.merge(ArchetypeDepend::Flag(
            if archetype.get_column(&TypeId::of::<T>()).is_none() {
                Flags::empty()
            } else {
                Flags::READ
            },
        ))
    }
    fn init_state(_world: &World, archetype: &Archetype) -> Self::State {
        archetype.get_column_index(&TypeId::of::<T>())
    }

    #[inline]
    fn init_fetch<'w>(
        _world: &'w World,
        archetype: &'w Archetype,
        state: &'w Self::State,
    ) -> Self::Fetch<'w> {
        (!state.is_null()).then_some(&archetype.table.get_column_unchecked(*state))
    }

    #[inline(always)]
    fn fetch<'w>(fetch: &mut Self::Fetch<'w>, row: Row, _e: Entity) -> Self::Item<'w> {
        fetch.and_then(|c| Some(c.get(row)))
    }
}

impl<T: 'static> FetchComponents for Option<&mut T> {
    type Fetch<'w> = Option<&'w Column>;
    type Item<'w> = Option<Mut<'w, T>>;
    type ReadOnly = Option<&'static T>;
    type State = ColumnIndex;

    fn init_read_write(_world: &World, meta: &mut SystemMeta) {
        meta.cur_param
            .writes
            .insert(TypeId::of::<T>(), std::any::type_name::<T>().into());
    }
    fn archetype_depend(archetype: &Archetype, result: &mut ArchetypeDependResult) {
        result.merge(ArchetypeDepend::Flag(
            if archetype.get_column(&TypeId::of::<T>()).is_none() {
                Flags::empty()
            } else {
                Flags::WRITE
            },
        ))
    }
    fn init_state(_world: &World, archetype: &Archetype) -> Self::State {
        archetype.get_column_index(&TypeId::of::<T>())
    }

    #[inline]
    fn init_fetch<'w>(
        _world: &'w World,
        archetype: &'w Archetype,
        state: &'w Self::State,
    ) -> Self::Fetch<'w> {
        (!state.is_null()).then_some(&archetype.table.get_column_unchecked(*state))
    }

    #[inline(always)]
    fn fetch<'w>(fetch: &mut Self::Fetch<'w>, row: Row, e: Entity) -> Self::Item<'w> {
        fetch.and_then(|c| {
            Some(Mut {
                value: c.get_mut(row),
                dirty: &c.changed,
                e,
                row,
            })
        })
    }
}
/// 不存在T时,使用默认值。
/// 默认值取Res<DefaultValue<T>>
/// DefaultValue<T>默认为DefaultValue::from_world的返回值,也可被应用程序覆盖
pub struct OrDefault<T: 'static>(PhantomData<T>);
impl<T: 'static> FetchComponents for OrDefault<T> {
    type Fetch<'w> = Result<&'w Column, &'w T>;
    type Item<'w> = &'w T;
    type ReadOnly = OrDefault<T>;
    type State = Result<ColumnIndex, SingleResource>;

    fn init_read_write(_world: &World, meta: &mut SystemMeta) {
        let name: Cow<'static, str> = std::any::type_name::<T>().into();
        meta.res_read(TypeId::of::<T>(), name.clone());
        meta.cur_param.reads.insert(TypeId::of::<T>(), name);
    }
    fn archetype_depend(archetype: &Archetype, result: &mut ArchetypeDependResult) {
        result.merge(ArchetypeDepend::Flag(
            if archetype.get_column(&TypeId::of::<T>()).is_none() {
                Flags::empty()
            } else {
                Flags::READ
            },
        ))
    }
    fn res_depend(
        res_tid: &TypeId,
        _res_name: &Cow<'static, str>,
        single: bool,
        result: &mut Flags,
    ) {
        if single && &TypeId::of::<T>() == res_tid {
            result.set(Flags::WRITE, true)
        }
    }

    fn init_state(world: &World, archetype: &Archetype) -> Self::State {
        let index = archetype.get_column_index(&TypeId::of::<T>());
        if index.is_null() {
            Err(world.get_single_res_any(&TypeId::of::<T>()).unwrap())
        } else {
            Ok(index)
        }
    }

    #[inline]
    fn init_fetch<'w>(
        _world: &'w World,
        archetype: &'w Archetype,
        state: &'w Self::State,
    ) -> Self::Fetch<'w> {
        match state {
            Ok(s) => Ok(&archetype.table.get_column_unchecked(*s)),
            Err(r) => Err(unsafe { &mut *r.downcast::<T>() }),
        }
    }

    #[inline(always)]
    fn fetch<'w>(fetch: &mut Self::Fetch<'w>, row: Row, _e: Entity) -> Self::Item<'w> {
        match fetch {
            Ok(c) => c.get(row),
            Err(r) => r,
        }
    }
}

pub struct Has<T: 'static>(PhantomData<T>);
impl<T: 'static> FetchComponents for Has<T> {
    type Fetch<'w> = bool;
    type Item<'w> = bool;
    type ReadOnly = Has<T>;
    type State = bool;

    fn init_state(_world: &World, archetype: &Archetype) -> Self::State {
        archetype.get_column(&TypeId::of::<T>()).is_some()
    }

    #[inline]
    fn init_fetch<'w>(
        _world: &'w World,
        _archetype: &'w Archetype,
        state: &'w Self::State,
    ) -> Self::Fetch<'w> {
        *state
    }

    #[inline(always)]
    fn fetch<'w>(fetch: &mut Self::Fetch<'w>, _row: Row, _e: Entity) -> Self::Item<'w> {
        *fetch
    }
}

/// Unique mutable borrow of an entity's component
#[derive(Debug)]
pub struct Mut<'a, T: ?Sized> {
    pub(crate) value: &'a mut T,
    pub(crate) dirty: &'a ComponentDirty,
    pub(crate) e: Entity,
    pub(crate) row: Row,
}
impl<'a, T: ?Sized> Mut<'a, T> {
    #[inline(always)]
    pub fn entity(&self) -> Entity {
        self.e
    }
}
impl<'a, T: ?Sized> Deref for Mut<'a, T> {
    type Target = T;
    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}
impl<'a, T: ?Sized> DerefMut for Mut<'a, T> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.dirty.record(self.e, self.row);
        self.value
    }
}

macro_rules! impl_tuple_fetch {
    ($(($name: ident, $state: ident)),*) => {
        #[allow(non_snake_case)]
        #[allow(clippy::unused_unit)]

        impl<$($name: FetchComponents),*> FetchComponents for ($($name,)*) {
            type Fetch<'w> = ($($name::Fetch<'w>,)*);
            type Item<'w> = ($($name::Item<'w>,)*);
            type ReadOnly = ($($name::ReadOnly,)*);
            type State = ($($name::State,)*);

            fn init_read_write(_world: &World, _meta: &mut SystemMeta) {
                ($($name::init_read_write(_world, _meta),)*);
            }
            fn archetype_depend(_archetype: &Archetype, _result: &mut ArchetypeDependResult) {
                ($($name::archetype_depend(_archetype, _result),)*);
            }
            fn res_depend(_res_tid: &TypeId, _res_name: &Cow<'static, str>, _single: bool, _result: &mut Flags) {
                ($($name::res_depend(_res_tid, _res_name, _single, _result),)*);
            }

            fn init_state(_world: &World, _archetype: &Archetype) -> Self::State {
                ($(
                    $name::init_state(_world, _archetype),
                )*)
            }

            #[inline(always)]
            #[allow(clippy::unused_unit)]
            fn init_fetch<'w>(_world: &'w World, _archetype: &'w Archetype, _state: &'w Self::State) -> Self::Fetch<'w> {
                let ($($state,)*) = _state;
                ($($name::init_fetch(_world, _archetype, $state),)*)
            }

            #[inline(always)]
            #[allow(clippy::unused_unit)]
            fn fetch<'w>(
                _fetch: &mut Self::Fetch<'w>,
                _row: Row,
                _e: Entity,
            ) -> Self::Item<'w> {
                let ($($name,)*) = _fetch;
                ($($name::fetch($name, _row, _e),)*)
            }
        }

    };
}
all_tuples!(impl_tuple_fetch, 0, 15, F, S);