Skip to content

Commit

Permalink
Rename Q type parameter to D when referring to WorldQueryData (#…
Browse files Browse the repository at this point in the history
…10782)

# Objective

Since #10776 split `WorldQuery` to `WorldQueryData` and
`WorldQueryFilter`, it should be clear that the query is actually
composed of two parts. It is not factually correct to call "query" only
the data part. Therefore I suggest to rename the `Q` parameter to `D` in
`Query` and related items.

As far as I know, there shouldn't be breaking changes from renaming
generic type parameters.

## Solution

I used a combination of rust-analyzer go to reference and `Ctrl-F`ing
various patterns to catch as many cases as possible. Hopefully I got
them all. Feel free to check if you're concerned of me having missed
some.

## Notes

This and #10779 have many lines in common, so merging one will cause a
lot of merge conflicts to the other.

---------

Co-authored-by: Alice Cecile <[email protected]>
  • Loading branch information
Nilirad and alice-i-cecile authored Dec 13, 2023
1 parent 7538e27 commit 9c78128
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 271 deletions.
36 changes: 18 additions & 18 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ pub unsafe trait ReadOnlyQueryData: QueryData<ReadOnly = Self> {}
/// The item type returned when a [`WorldQuery`] is iterated over
pub type QueryItem<'w, Q> = <Q as WorldQuery>::Item<'w>;
/// The read-only variant of the item type returned when a [`QueryData`] is iterated over immutably
pub type ROQueryItem<'w, Q> = QueryItem<'w, <Q as QueryData>::ReadOnly>;
pub type ROQueryItem<'w, D> = QueryItem<'w, <D as QueryData>::ReadOnly>;

/// SAFETY:
/// `update_component_access` and `update_archetype_component_access` do nothing.
Expand Down Expand Up @@ -1399,27 +1399,27 @@ macro_rules! impl_anytuple_fetch {
all_tuples!(impl_tuple_query_data, 0, 15, F, S);
all_tuples!(impl_anytuple_fetch, 0, 15, F, S);

/// [`WorldQuery`] used to nullify queries by turning `Query<Q>` into `Query<NopWorldQuery<Q>>`
/// [`WorldQuery`] used to nullify queries by turning `Query<D>` into `Query<NopWorldQuery<D>>`
///
/// This will rarely be useful to consumers of `bevy_ecs`.
pub struct NopWorldQuery<Q: QueryData>(PhantomData<Q>);
pub struct NopWorldQuery<D: QueryData>(PhantomData<D>);

/// SAFETY:
/// `update_component_access` and `update_archetype_component_access` do nothing.
/// This is sound because `fetch` does not access components.
unsafe impl<Q: QueryData> WorldQuery for NopWorldQuery<Q> {
unsafe impl<D: QueryData> WorldQuery for NopWorldQuery<D> {
type Fetch<'w> = ();
type Item<'w> = ();
type State = Q::State;
type State = D::State;

fn shrink<'wlong: 'wshort, 'wshort>(_: ()) {}

const IS_DENSE: bool = Q::IS_DENSE;
const IS_DENSE: bool = D::IS_DENSE;

#[inline(always)]
unsafe fn init_fetch(
_world: UnsafeWorldCell,
_state: &Q::State,
_state: &D::State,
_last_run: Tick,
_this_run: Tick,
) {
Expand All @@ -1428,14 +1428,14 @@ unsafe impl<Q: QueryData> WorldQuery for NopWorldQuery<Q> {
#[inline(always)]
unsafe fn set_archetype(
_fetch: &mut (),
_state: &Q::State,
_state: &D::State,
_archetype: &Archetype,
_tables: &Table,
) {
}

#[inline(always)]
unsafe fn set_table<'w>(_fetch: &mut (), _state: &Q::State, _table: &Table) {}
unsafe fn set_table<'w>(_fetch: &mut (), _state: &D::State, _table: &Table) {}

#[inline(always)]
unsafe fn fetch<'w>(
Expand All @@ -1445,34 +1445,34 @@ unsafe impl<Q: QueryData> WorldQuery for NopWorldQuery<Q> {
) -> Self::Item<'w> {
}

fn update_component_access(_state: &Q::State, _access: &mut FilteredAccess<ComponentId>) {}
fn update_component_access(_state: &D::State, _access: &mut FilteredAccess<ComponentId>) {}

fn update_archetype_component_access(
_state: &Q::State,
_state: &D::State,
_archetype: &Archetype,
_access: &mut Access<ArchetypeComponentId>,
) {
}

fn init_state(world: &mut World) -> Self::State {
Q::init_state(world)
D::init_state(world)
}

fn matches_component_set(
state: &Self::State,
set_contains_id: &impl Fn(ComponentId) -> bool,
) -> bool {
Q::matches_component_set(state, set_contains_id)
D::matches_component_set(state, set_contains_id)
}
}

/// SAFETY: `Self::ReadOnly` is `Self`
unsafe impl<Q: QueryData> QueryData for NopWorldQuery<Q> {
unsafe impl<D: QueryData> QueryData for NopWorldQuery<D> {
type ReadOnly = Self;
}

/// SAFETY: `NopFetch` never accesses any data
unsafe impl<Q: QueryData> ReadOnlyQueryData for NopWorldQuery<Q> {}
unsafe impl<D: QueryData> ReadOnlyQueryData for NopWorldQuery<D> {}

/// SAFETY:
/// `update_component_access` and `update_archetype_component_access` do nothing.
Expand Down Expand Up @@ -1601,14 +1601,14 @@ mod tests {

#[derive(QueryData)]
#[query_data(mutable)]
pub struct Q {
pub struct D {
pub a: &'static mut A,
}
}

let _ = private::QReadOnly { a: &A };
let _ = private::DReadOnly { a: &A };

fn my_system(query: Query<private::Q>) {
fn my_system(query: Query<private::D>) {
for q in &query {
let _ = &q.a;
}
Expand Down
Loading

0 comments on commit 9c78128

Please sign in to comment.