flax/fetch/
as_deref.rs

1use super::{FetchAccessData, FmtQuery, PreparedFetch, RandomFetch};
2use crate::{query::ArchetypeSearcher, system::Access, Fetch, FetchItem};
3use alloc::vec::Vec;
4use core::{fmt, ops::Deref};
5
6/// Dereferences the fetch item
7pub struct AsDeref<F>(pub F);
8
9impl<'q, F, V> FetchItem<'q> for AsDeref<F>
10where
11    F: FetchItem<'q, Item = &'q V>,
12    V: 'static + Deref,
13{
14    type Item = &'q V::Target;
15}
16
17impl<'w, F, V> Fetch<'w> for AsDeref<F>
18where
19    F: Fetch<'w>,
20    F: for<'q> FetchItem<'q, Item = &'q V>,
21    V: 'static + Deref,
22{
23    const MUTABLE: bool = F::MUTABLE;
24
25    type Prepared = AsDeref<F::Prepared>;
26
27    #[inline]
28    fn prepare(&'w self, data: super::FetchPrepareData<'w>) -> Option<Self::Prepared> {
29        Some(AsDeref(self.0.prepare(data)?))
30    }
31
32    #[inline]
33    fn filter_arch(&self, data: FetchAccessData) -> bool {
34        self.0.filter_arch(data)
35    }
36
37    #[inline]
38    fn describe(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        write!(f, "deref {:?}", FmtQuery(&self.0))
40    }
41
42    #[inline]
43    fn access(&self, data: FetchAccessData, dst: &mut Vec<Access>) {
44        self.0.access(data, dst)
45    }
46
47    #[inline]
48    fn searcher(&self, searcher: &mut ArchetypeSearcher) {
49        self.0.searcher(searcher)
50    }
51}
52
53impl<'q, F, V> PreparedFetch<'q> for AsDeref<F>
54where
55    F: PreparedFetch<'q, Item = &'q V>,
56    V: 'static + Deref,
57{
58    type Item = &'q V::Target;
59    type Chunk = F::Chunk;
60
61    const HAS_FILTER: bool = F::HAS_FILTER;
62
63    unsafe fn filter_slots(&mut self, slots: crate::archetype::Slice) -> crate::archetype::Slice {
64        self.0.filter_slots(slots)
65    }
66
67    unsafe fn create_chunk(&'q mut self, slots: crate::archetype::Slice) -> Self::Chunk {
68        self.0.create_chunk(slots)
69    }
70
71    #[inline]
72    unsafe fn fetch_next(chunk: &mut Self::Chunk) -> Self::Item {
73        F::fetch_next(chunk)
74    }
75}
76
77impl<'q, F, V> RandomFetch<'q> for AsDeref<F>
78where
79    F: RandomFetch<'q, Item = &'q V>,
80    V: 'static + Deref,
81{
82    unsafe fn fetch_shared(&'q self, slot: crate::archetype::Slot) -> Self::Item {
83        self.0.fetch_shared(slot)
84    }
85
86    unsafe fn fetch_shared_chunk(chunk: &Self::Chunk, slot: crate::archetype::Slot) -> Self::Item {
87        F::fetch_shared_chunk(chunk, slot)
88    }
89}