bevy_trait_query/one/core/
fetch.rs

1use std::{cell::UnsafeCell, panic::Location};
2
3use bevy_ecs::{
4    change_detection::MaybeLocation,
5    component::Tick,
6    ptr::{Ptr, ThinSlicePtr},
7    storage::{ComponentSparseSet, SparseSets},
8};
9
10use crate::TraitImplMeta;
11
12pub struct OneTraitFetch<'w, Trait: ?Sized> {
13    // While we have shared access to all sparse set components,
14    // in practice we will only access the components specified in the `FetchState`.
15    // These accesses have been registered, which prevents runtime conflicts.
16    pub(crate) sparse_sets: &'w SparseSets,
17    // After `Fetch::set_archetype` or `set_table` has been called,
18    // this will carry the component data and metadata for the first trait impl found in the archetype.
19    pub(crate) storage: FetchStorage<'w, Trait>,
20    pub(crate) last_run: Tick,
21    pub(crate) this_run: Tick,
22}
23
24impl<Trait: ?Sized> Clone for OneTraitFetch<'_, Trait> {
25    fn clone(&self) -> Self {
26        *self
27    }
28}
29
30impl<Trait: ?Sized> Copy for OneTraitFetch<'_, Trait> {}
31
32pub(crate) enum FetchStorage<'w, Trait: ?Sized> {
33    Uninit,
34    Table {
35        /// This points to one of the component table columns,
36        /// corresponding to one of the `ComponentId`s in the fetch state.
37        /// The fetch impl registers access for all of these components,
38        /// so there will be no runtime conflicts.
39        column: Ptr<'w>,
40        added_ticks: ThinSlicePtr<'w, UnsafeCell<Tick>>,
41        changed_ticks: ThinSlicePtr<'w, UnsafeCell<Tick>>,
42        location: MaybeLocation<&'w UnsafeCell<&'static Location<'static>>>,
43        meta: TraitImplMeta<Trait>,
44    },
45    SparseSet {
46        /// This gives us access to one of the components implementing the trait.
47        /// The fetch impl registers access for all components implementing the trait,
48        /// so there will not be any runtime conflicts.
49        components: &'w ComponentSparseSet,
50        meta: TraitImplMeta<Trait>,
51    },
52}
53
54impl<Trait: ?Sized> Clone for FetchStorage<'_, Trait> {
55    fn clone(&self) -> Self {
56        *self
57    }
58}
59impl<Trait: ?Sized> Copy for FetchStorage<'_, Trait> {}