Skip to content

Commit 99219af

Browse files
authored
Merge pull request #73 from JoJoJet/bevy-0.15-update-redo
Redo bevy 0.15 update
2 parents 45df913 + 2675ba5 commit 99219af

File tree

11 files changed

+190
-98
lines changed

11 files changed

+190
-98
lines changed

Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ members = [
77

88
[workspace.dependencies]
99
tracing = "0.1"
10-
bevy_ecs = "0.14"
11-
bevy_app = "0.14"
12-
bevy_core = "0.14"
10+
bevy_ecs = "0.15"
11+
bevy_app = "0.15"
12+
bevy_core = "0.15"
1313

1414
# proc macro
1515
bevy-trait-query-impl = { version = "0.6.0", path = "./bevy-trait-query-impl" }
1616
proc-macro2 = "1"
1717
syn = { version = "2", features = ["full"] }
1818
quote = "1"
19-
proc-macro-crate = "1"
19+
proc-macro-crate = "3"
2020

2121
# dev deps
2222
criterion = "0.5"
23-
bevy = { version = "0.14", default-features = false }
23+
bevy = { version = "0.15", default-features = false }

bevy-trait-query-impl/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
250250
) -> bool {
251251
<#my_crate::All<&#trait_object> as #imports::WorldQuery>::matches_component_set(state, set_contains_id)
252252
}
253+
254+
#[inline]
255+
fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
256+
fetch
257+
}
253258
}
254259

255260
unsafe impl #impl_generics_with_lifetime #imports::QueryData for &'__a mut #trait_object
@@ -351,6 +356,11 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
351356
) -> bool {
352357
<#my_crate::All<&mut #trait_object> as #imports::WorldQuery>::matches_component_set(state, set_contains_id)
353358
}
359+
360+
#[inline]
361+
fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
362+
fetch
363+
}
354364
}
355365
};
356366

bevy-trait-query/src/all/core/read.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,26 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for ReadTableTraitsIter<'a, Trait>
5252
fn next(&mut self) -> Option<Self::Item> {
5353
// Iterate the remaining table components that are registered,
5454
// until we find one that exists in the table.
55-
let (column, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
56-
.find_map(|(&component, meta)| self.table.get_column(component).zip(Some(meta)))?;
57-
// SAFETY: We have shared access to the entire column.
58-
let ptr = unsafe {
59-
column
60-
.get_data_ptr()
61-
.byte_add(self.table_row.as_usize() * meta.size_bytes)
62-
};
55+
let (ptr, component, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
56+
.find_map(|(&component, meta)| {
57+
// SAFETY: we know that the `table_row` is a valid index.
58+
let ptr = unsafe { self.table.get_component(component, self.table_row) }?;
59+
Some((ptr, component, meta))
60+
})?;
6361
let trait_object = unsafe { meta.dyn_ctor.cast(ptr) };
6462

65-
// SAFETY: we know that the `table_row` is a valid index.
63+
// SAFETY:
6664
// Read access has been registered, so we can dereference it immutably.
67-
let added_tick = unsafe { column.get_added_tick_unchecked(self.table_row).deref() };
68-
let changed_tick = unsafe { column.get_changed_tick_unchecked(self.table_row).deref() };
65+
let added_tick = unsafe {
66+
self.table
67+
.get_added_tick(component, self.table_row)?
68+
.deref()
69+
};
70+
let changed_tick = unsafe {
71+
self.table
72+
.get_changed_tick(component, self.table_row)?
73+
.deref()
74+
};
6975

7076
Some(Ref::new(
7177
trait_object,
@@ -94,13 +100,12 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for ReadSparseTraitsIter<'a, Trait
94100
fn next(&mut self) -> Option<Self::Item> {
95101
// Iterate the remaining sparse set components that are registered,
96102
// until we find one that exists in the archetype.
97-
let ((ptr, ticks_ptr), meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
103+
let (ptr, ticks_ptr, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
98104
.find_map(|(&component, meta)| {
99-
self.sparse_sets
100-
.get(component)
101-
.and_then(|set| set.get_with_ticks(self.entity))
102-
.zip(Some(meta))
103-
})?;
105+
let set = self.sparse_sets.get(component)?;
106+
let (ptr, ticks, _) = set.get_with_ticks(self.entity)?;
107+
Some((ptr, ticks, meta))
108+
})?;
104109
let trait_object = unsafe { meta.dyn_ctor.cast(ptr) };
105110
let added_tick = unsafe { ticks_ptr.added.deref() };
106111
let changed_tick = unsafe { ticks_ptr.changed.deref() };

bevy-trait-query/src/all/core/write.rs

+19-21
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for WriteTableTraitsIter<'a, Trait
5858
fn next(&mut self) -> Option<Self::Item> {
5959
// Iterate the remaining table components that are registered,
6060
// until we find one that exists in the table.
61-
let (column, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
62-
.find_map(|(&component, meta)| self.table.get_column(component).zip(Some(meta)))?;
63-
let ptr = unsafe {
64-
column
65-
.get_data_ptr()
66-
.byte_add(self.table_row.as_usize() * meta.size_bytes)
67-
};
61+
let (ptr, component, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
62+
.find_map(|(&component, meta)| {
63+
// SAFETY: we know that the `table_row` is a valid index.
64+
let ptr = unsafe { self.table.get_component(component, self.table_row) }?;
65+
Some((ptr, component, meta))
66+
})?;
6867
// SAFETY: The instance of `WriteTraits` that created this iterator
6968
// has exclusive access to all table components registered with the trait.
7069
//
@@ -74,10 +73,14 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for WriteTableTraitsIter<'a, Trait
7473
let trait_object = unsafe { meta.dyn_ctor.cast_mut(ptr) };
7574
// SAFETY: We have exclusive access to the component, so by extension
7675
// we have exclusive access to the corresponding `ComponentTicks`.
77-
let added = unsafe { column.get_added_tick_unchecked(self.table_row).deref_mut() };
76+
let added = unsafe {
77+
self.table
78+
.get_added_tick(component, self.table_row)?
79+
.deref_mut()
80+
};
7881
let changed = unsafe {
79-
column
80-
.get_changed_tick_unchecked(self.table_row)
82+
self.table
83+
.get_changed_tick(component, self.table_row)?
8184
.deref_mut()
8285
};
8386
Some(Mut::new(
@@ -108,13 +111,12 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for WriteSparseTraitsIter<'a, Trai
108111
fn next(&mut self) -> Option<Self::Item> {
109112
// Iterate the remaining sparse set components we have registered,
110113
// until we find one that exists in the archetype.
111-
let ((ptr, component_ticks), meta) =
114+
let (ptr, component_ticks, meta) =
112115
unsafe { zip_exact(&mut self.components, &mut self.meta) }.find_map(
113116
|(&component, meta)| {
114-
self.sparse_sets
115-
.get(component)
116-
.and_then(|set| set.get_with_ticks(self.entity))
117-
.zip(Some(meta))
117+
let set = self.sparse_sets.get(component)?;
118+
let (ptr, ticks, _) = set.get_with_ticks(self.entity)?;
119+
Some((ptr, ticks, meta))
118120
},
119121
)?;
120122

@@ -201,9 +203,7 @@ impl<'w, Trait: ?Sized + TraitQuery> IntoIterator for WriteTraits<'w, Trait> {
201203
}
202204
}
203205

204-
impl<'world, 'local, Trait: ?Sized + TraitQuery> IntoIterator
205-
for &'local WriteTraits<'world, Trait>
206-
{
206+
impl<'local, Trait: ?Sized + TraitQuery> IntoIterator for &'local WriteTraits<'_, Trait> {
207207
type Item = Ref<'local, Trait>;
208208
type IntoIter = CombinedReadTraitsIter<'local, Trait>;
209209
#[inline]
@@ -228,9 +228,7 @@ impl<'world, 'local, Trait: ?Sized + TraitQuery> IntoIterator
228228
}
229229
}
230230

231-
impl<'world, 'local, Trait: ?Sized + TraitQuery> IntoIterator
232-
for &'local mut WriteTraits<'world, Trait>
233-
{
231+
impl<'local, Trait: ?Sized + TraitQuery> IntoIterator for &'local mut WriteTraits<'_, Trait> {
234232
type Item = Mut<'local, Trait>;
235233
type IntoIter = CombinedWriteTraitsIter<'local, Trait>;
236234
#[inline]

bevy-trait-query/src/all/impls/all.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ use crate::{
2626
/// - `Query<&mut dyn Trait>` yields a [`WriteTraits`] object
2727
pub struct All<T: ?Sized>(T);
2828

29-
unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for All<&'a Trait> {
29+
unsafe impl<Trait: ?Sized + TraitQuery> QueryData for All<&Trait> {
3030
type ReadOnly = Self;
3131
}
32-
unsafe impl<'a, Trait: ?Sized + TraitQuery> ReadOnlyQueryData for All<&'a Trait> {}
32+
unsafe impl<Trait: ?Sized + TraitQuery> ReadOnlyQueryData for All<&Trait> {}
3333

3434
// SAFETY: We only access the components registered in the trait registry.
3535
// This is known to match the set of components in the TraitQueryState,
3636
// which is used to match archetypes and register world access.
37-
unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a Trait> {
37+
unsafe impl<Trait: ?Sized + TraitQuery> WorldQuery for All<&Trait> {
3838
type Item<'w> = ReadTraits<'w, Trait>;
3939
type Fetch<'w> = AllTraitsFetch<'w, Trait>;
4040
type State = TraitQueryState<Trait>;
@@ -109,18 +109,18 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a Trait> {
109109
let mut new_access = access.clone();
110110
for &component in &*state.components {
111111
assert!(
112-
!access.access().has_write(component),
112+
!access.access().has_component_write(component),
113113
"&{} conflicts with a previous access in this query. Shared access cannot coincide with exclusive access.",
114114
std::any::type_name::<Trait>(),
115115
);
116116
if not_first {
117117
let mut intermediate = access.clone();
118-
intermediate.add_read(component);
118+
intermediate.add_component_read(component);
119119
new_access.append_or(&intermediate);
120120
new_access.extend_access(&intermediate);
121121
} else {
122122
new_access.and_with(component);
123-
new_access.access_mut().add_read(component);
123+
new_access.access_mut().add_component_read(component);
124124
not_first = true;
125125
}
126126
}
@@ -145,6 +145,11 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a Trait> {
145145
) -> bool {
146146
state.matches_component_set_any(set_contains_id)
147147
}
148+
149+
#[inline]
150+
fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
151+
fetch
152+
}
148153
}
149154

150155
unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for All<&'a mut Trait> {
@@ -154,7 +159,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for All<&'a mut Trait> {
154159
// SAFETY: We only access the components registered in the trait registry.
155160
// This is known to match the set of components in the TraitQueryState,
156161
// which is used to match archetypes and register world access.
157-
unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
162+
unsafe impl<Trait: ?Sized + TraitQuery> WorldQuery for All<&mut Trait> {
158163
type Item<'w> = WriteTraits<'w, Trait>;
159164
type Fetch<'w> = AllTraitsFetch<'w, Trait>;
160165
type State = TraitQueryState<Trait>;
@@ -230,18 +235,18 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
230235
let mut new_access = access.clone();
231236
for &component in &*state.components {
232237
assert!(
233-
!access.access().has_write(component),
238+
!access.access().has_component_write(component),
234239
"&mut {} conflicts with a previous access in this query. Mutable component access must be unique.",
235240
std::any::type_name::<Trait>(),
236241
);
237242
if not_first {
238243
let mut intermediate = access.clone();
239-
intermediate.add_write(component);
244+
intermediate.add_component_write(component);
240245
new_access.append_or(&intermediate);
241246
new_access.extend_access(&intermediate);
242247
} else {
243248
new_access.and_with(component);
244-
new_access.access_mut().add_write(component);
249+
new_access.access_mut().add_component_write(component);
245250
not_first = true;
246251
}
247252
}
@@ -266,4 +271,9 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
266271
) -> bool {
267272
state.matches_component_set_any(set_contains_id)
268273
}
274+
275+
#[inline]
276+
fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
277+
fetch
278+
}
269279
}

bevy-trait-query/src/internal/register_ext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl RegisterExt for World {
2121
where
2222
(C,): TraitQueryMarker<Trait, Covered = C>,
2323
{
24-
let component_id = self.init_component::<C>();
24+
let component_id = self.register_component::<C>();
2525
let registry = self
2626
.get_resource_or_insert_with::<TraitImplRegistry<Trait>>(Default::default)
2727
.into_inner();

0 commit comments

Comments
 (0)